Spring 多次调用控制器方法
Spring controller method is getting called multiple times
我有一个 spring 引导应用程序,它充当离子应用程序的服务,每当我调用 api 时,控制器方法都会被调用两次,例如:
@RestController
@CrossOrigin
@RequestMapping("/sendSMS")
public class SendSMSController {
@Autowired
SendSMSService sendSMSService;
protected final Log logger = LogFactory.getLog(getClass());
@PostMapping(value = "/sendMessage", produces = "application/json",consumes = "application/json")
public Map<String, Object> sendMessage(@RequestBody UserRegistration userRegistration) {
logger.info("Api sendMessage test");
return sendSMSService.sendMessage(userRegistration);
}
}
当我从邮递员调用 http://localhost:8080/CEPMobileService/sendSMS/sendMessage 时,api /sendMessage 被调用两次,如日志中所示
15/10/2020 09:32:29 PM | INFO | http-nio-8080-exec-4 | SendSMSController | Api sendMessage test
15/10/2020 09:32:29 PM | INFO | http-nio-8080-exec-4 | SendSMSController | Api sendMessage test
15/10/2020 09:32:29 PM | INFO | http-nio-8080-exec-4 | SendSMSServiceImpl | Phone number-->
[0000000000, 0000000000]
15/10/2020 09:32:29 PM | INFO | http-nio-8080-exec-4 | SendSMSServiceImpl | Phone number-->
[0000000000, 0000000000]
15/10/2020 09:32:29 PM | INFO | http-nio-8080-exec-4 | SendSMSServiceImpl | Message number-->In
distress!!!
15/10/2020 09:32:29 PM | INFO | http-nio-8080-exec-4 | SendSMSServiceImpl | Message number-->In
distress!!!
15/10/2020 09:32:29 PM | INFO | http-nio-8080-exec-4 | SendSMSServiceImpl | numbers---->0000000000,
0000000000
15/10/2020 09:32:29 PM | INFO | http-nio-8080-exec-4 | SendSMSServiceImpl | numbers---->0000000000,
0000000000
不只是这个 api 代码中的每个 api 都会被调用两次,这在部署在 Azure 中时也会发生。一个答案表明它可能是由于 JSONView,但它在我的系统中不可用,其他答案也无济于事,请问有什么解决方案吗?
正如 anavaraslamurep 和 Ismail 所建议的,这确实是一个记录器错误,我使用了 log4j,并且在配置附加程序时消息被记录了两次。我解决了将可加性设置为 false 的问题,这里是配置:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Properties>
<!-- azure server log -->
<Property name="log-path">d:\home\Citizen_Engagement_App_V3_logs
</Property>
</Properties>
<Appenders>
<RollingFile name="file-log"
fileName="${log-path}/citizen_app_application.log"
filePattern="${log-path}/%d{dd/MM/yyyy hh:mm:ss a}/application-log-%i.log.gz">
<PatternLayout
pattern="%d{dd/MM/yyyy hh:mm:ss a} | %5level | %t | %c{1} | %m%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
</RollingFile>
<RollingFile name="error-log" fileName="${log-path}/application_error.log"
filePattern="${log-path}/errorlog/%d{yyyy}/%d{MMM}/%d{dd}/application-log-%i-
error.log.gz">
<PatternLayout
pattern="%d{dd/MM/yyyy hh:mm:ss a} | %5level | %t | %c{1} | %m%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
</RollingFile>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout
pattern="%d{dd/MM/yyyy hh:mm:ss a} | %5level | %t | %c{1} | %m%n" />
</Console>
</Appenders>
<Loggers>
<logger name="org.hibernate.SQL" level="DEBUG" additivity="false">
<appender-ref ref="file-log" />
<appender-ref ref="console" />
</logger>
<logger name="com.trinity" level="ALL" additivity="false">
<appender-ref ref="file-log" />
<appender-ref ref="console" />
</logger>
<logger name="org.trinity" level="ALL" additivity="false">
<appender-ref ref="file-log" />
<appender-ref ref="console" />
</logger>
<logger name="kafka" level="ALL" additivity="false">
<appender-ref ref="file-log" />
<appender-ref ref="console" />
</logger>
<logger name="jms" level="ALL" additivity="false">
<appender-ref ref="file-log" />
<appender-ref ref="console" />
</logger>
<Root level="ERROR">
<appender-ref ref="error-log" />
<appender-ref ref="console" />
</Root>
</Loggers>
</Configuration>
我有一个 spring 引导应用程序,它充当离子应用程序的服务,每当我调用 api 时,控制器方法都会被调用两次,例如:
@RestController
@CrossOrigin
@RequestMapping("/sendSMS")
public class SendSMSController {
@Autowired
SendSMSService sendSMSService;
protected final Log logger = LogFactory.getLog(getClass());
@PostMapping(value = "/sendMessage", produces = "application/json",consumes = "application/json")
public Map<String, Object> sendMessage(@RequestBody UserRegistration userRegistration) {
logger.info("Api sendMessage test");
return sendSMSService.sendMessage(userRegistration);
}
}
当我从邮递员调用 http://localhost:8080/CEPMobileService/sendSMS/sendMessage 时,api /sendMessage 被调用两次,如日志中所示
15/10/2020 09:32:29 PM | INFO | http-nio-8080-exec-4 | SendSMSController | Api sendMessage test
15/10/2020 09:32:29 PM | INFO | http-nio-8080-exec-4 | SendSMSController | Api sendMessage test
15/10/2020 09:32:29 PM | INFO | http-nio-8080-exec-4 | SendSMSServiceImpl | Phone number-->
[0000000000, 0000000000]
15/10/2020 09:32:29 PM | INFO | http-nio-8080-exec-4 | SendSMSServiceImpl | Phone number-->
[0000000000, 0000000000]
15/10/2020 09:32:29 PM | INFO | http-nio-8080-exec-4 | SendSMSServiceImpl | Message number-->In
distress!!!
15/10/2020 09:32:29 PM | INFO | http-nio-8080-exec-4 | SendSMSServiceImpl | Message number-->In
distress!!!
15/10/2020 09:32:29 PM | INFO | http-nio-8080-exec-4 | SendSMSServiceImpl | numbers---->0000000000,
0000000000
15/10/2020 09:32:29 PM | INFO | http-nio-8080-exec-4 | SendSMSServiceImpl | numbers---->0000000000,
0000000000
不只是这个 api 代码中的每个 api 都会被调用两次,这在部署在 Azure 中时也会发生。一个答案表明它可能是由于 JSONView,但它在我的系统中不可用,其他答案也无济于事,请问有什么解决方案吗?
正如 anavaraslamurep 和 Ismail 所建议的,这确实是一个记录器错误,我使用了 log4j,并且在配置附加程序时消息被记录了两次。我解决了将可加性设置为 false 的问题,这里是配置:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Properties>
<!-- azure server log -->
<Property name="log-path">d:\home\Citizen_Engagement_App_V3_logs
</Property>
</Properties>
<Appenders>
<RollingFile name="file-log"
fileName="${log-path}/citizen_app_application.log"
filePattern="${log-path}/%d{dd/MM/yyyy hh:mm:ss a}/application-log-%i.log.gz">
<PatternLayout
pattern="%d{dd/MM/yyyy hh:mm:ss a} | %5level | %t | %c{1} | %m%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
</RollingFile>
<RollingFile name="error-log" fileName="${log-path}/application_error.log"
filePattern="${log-path}/errorlog/%d{yyyy}/%d{MMM}/%d{dd}/application-log-%i-
error.log.gz">
<PatternLayout
pattern="%d{dd/MM/yyyy hh:mm:ss a} | %5level | %t | %c{1} | %m%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
</RollingFile>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout
pattern="%d{dd/MM/yyyy hh:mm:ss a} | %5level | %t | %c{1} | %m%n" />
</Console>
</Appenders>
<Loggers>
<logger name="org.hibernate.SQL" level="DEBUG" additivity="false">
<appender-ref ref="file-log" />
<appender-ref ref="console" />
</logger>
<logger name="com.trinity" level="ALL" additivity="false">
<appender-ref ref="file-log" />
<appender-ref ref="console" />
</logger>
<logger name="org.trinity" level="ALL" additivity="false">
<appender-ref ref="file-log" />
<appender-ref ref="console" />
</logger>
<logger name="kafka" level="ALL" additivity="false">
<appender-ref ref="file-log" />
<appender-ref ref="console" />
</logger>
<logger name="jms" level="ALL" additivity="false">
<appender-ref ref="file-log" />
<appender-ref ref="console" />
</logger>
<Root level="ERROR">
<appender-ref ref="error-log" />
<appender-ref ref="console" />
</Root>
</Loggers>
</Configuration>