英文:
Spring controller method is getting called multiple times
问题
我有一个作为 Ionic 应用程序服务的 Spring Boot 应用程序,每当我调用一个 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);
}
}
当我从 Postman 调用 http://localhost:8080/CEPMobileService/sendSMS/sendMessage 时,日志中显示 /sendMessage API 被调用了两次:
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,但它在我的系统中不可用,其他答案也无法解决,有什么解决方法吗?
英文:
I have a spring boot application which acts as a service to a ionic app, and whenever I call an api the controller methods gets called twice for eg:
@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);
}
}
When I call http://localhost:8080/CEPMobileService/sendSMS/sendMessage from postman, the api /sendMessage gets called twice as shown in the logs
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
Not just this api every api in the code gets called twice and this happens when deployed in Azure too. An answer suggested it maybe due to JSONView, but it is not available in my system and other answers couldn't help, any solutions please?
答案1
得分: 1
根据 anavaraslamurep 和 Ismail 的建议,的确是一个日志记录器错误。我使用了 log4j,由于我配置了附加器,消息被记录了两次。我通过将 additivity 设置为 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>
英文:
As suggested by anavaraslamurep and Ismail, it was indeed a logger error, I have used log4j and the messages were logged twice as I had configured appenders. I solved it setting additivity to false, here is the configuration:
<?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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论