春季控制器方法被多次调用。

huangapple go评论71阅读模式
英文:

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(&quot;/sendSMS&quot;)
public class SendSMSController {

@Autowired
SendSMSService sendSMSService;
protected final Log logger = LogFactory.getLog(getClass());

@PostMapping(value = &quot;/sendMessage&quot;, produces = &quot;application/json&quot;,consumes = &quot;application/json&quot;)
public  Map&lt;String, Object&gt; sendMessage(@RequestBody UserRegistration userRegistration) {	
	logger.info(&quot;Api sendMessage test&quot;);
	 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--&gt; 
[0000000000, 0000000000]
15/10/2020 09:32:29 PM |  INFO | http-nio-8080-exec-4 | SendSMSServiceImpl | Phone number--&gt; 
[0000000000, 0000000000]
15/10/2020 09:32:29 PM |  INFO | http-nio-8080-exec-4 | SendSMSServiceImpl | Message number--&gt;In 
distress!!!
15/10/2020 09:32:29 PM |  INFO | http-nio-8080-exec-4 | SendSMSServiceImpl | Message number--&gt;In 
distress!!!
15/10/2020 09:32:29 PM |  INFO | http-nio-8080-exec-4 | SendSMSServiceImpl | numbers----&gt;0000000000, 
0000000000
15/10/2020 09:32:29 PM |  INFO | http-nio-8080-exec-4 | SendSMSServiceImpl | numbers----&gt;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:

 &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;Configuration status=&quot;INFO&quot;&gt;
&lt;Properties&gt;
&lt;!-- azure server log --&gt;
&lt;Property name=&quot;log-path&quot;&gt;d:\home\Citizen_Engagement_App_V3_logs
&lt;/Property&gt;
&lt;/Properties&gt;
&lt;Appenders&gt;
&lt;RollingFile name=&quot;file-log&quot;
fileName=&quot;${log-path}/citizen_app_application.log&quot;
filePattern=&quot;${log-path}/%d{dd/MM/yyyy hh:mm:ss a}/application-log-%i.log.gz&quot;&gt;
&lt;PatternLayout
pattern=&quot;%d{dd/MM/yyyy hh:mm:ss a} | %5level | %t | %c{1} | %m%n&quot; /&gt;
&lt;Policies&gt;
&lt;TimeBasedTriggeringPolicy interval=&quot;1&quot;
modulate=&quot;true&quot; /&gt;
&lt;SizeBasedTriggeringPolicy size=&quot;100 MB&quot; /&gt;
&lt;/Policies&gt;
&lt;/RollingFile&gt;
&lt;RollingFile name=&quot;error-log&quot; fileName=&quot;${log-path}/application_error.log&quot;
filePattern=&quot;${log-path}/errorlog/%d{yyyy}/%d{MMM}/%d{dd}/application-log-%i- 
error.log.gz&quot;&gt;
&lt;PatternLayout
pattern=&quot;%d{dd/MM/yyyy hh:mm:ss a} | %5level | %t | %c{1} | %m%n&quot; /&gt;
&lt;Policies&gt;
&lt;TimeBasedTriggeringPolicy interval=&quot;1&quot;
modulate=&quot;true&quot; /&gt;
&lt;SizeBasedTriggeringPolicy size=&quot;100 MB&quot; /&gt;
&lt;/Policies&gt;
&lt;/RollingFile&gt;
&lt;Console name=&quot;console&quot; target=&quot;SYSTEM_OUT&quot;&gt;
&lt;PatternLayout
pattern=&quot;%d{dd/MM/yyyy hh:mm:ss a} | %5level | %t | %c{1} | %m%n&quot; /&gt;
&lt;/Console&gt;
&lt;/Appenders&gt;
&lt;Loggers&gt;
&lt;logger name=&quot;org.hibernate.SQL&quot; level=&quot;DEBUG&quot; additivity=&quot;false&quot;&gt;
&lt;appender-ref ref=&quot;file-log&quot; /&gt;
&lt;appender-ref ref=&quot;console&quot; /&gt;
&lt;/logger&gt;
&lt;logger name=&quot;com.trinity&quot; level=&quot;ALL&quot; additivity=&quot;false&quot;&gt;
&lt;appender-ref ref=&quot;file-log&quot; /&gt;
&lt;appender-ref ref=&quot;console&quot; /&gt;
&lt;/logger&gt;
&lt;logger name=&quot;org.trinity&quot; level=&quot;ALL&quot; additivity=&quot;false&quot;&gt;
&lt;appender-ref ref=&quot;file-log&quot; /&gt;
&lt;appender-ref ref=&quot;console&quot; /&gt;
&lt;/logger&gt;
&lt;logger name=&quot;kafka&quot; level=&quot;ALL&quot; additivity=&quot;false&quot;&gt;
&lt;appender-ref ref=&quot;file-log&quot; /&gt;
&lt;appender-ref ref=&quot;console&quot; /&gt;
&lt;/logger&gt;
&lt;logger name=&quot;jms&quot; level=&quot;ALL&quot; additivity=&quot;false&quot;&gt;
&lt;appender-ref ref=&quot;file-log&quot; /&gt;
&lt;appender-ref ref=&quot;console&quot; /&gt;
&lt;/logger&gt;
&lt;Root level=&quot;ERROR&quot;&gt;
&lt;appender-ref ref=&quot;error-log&quot; /&gt;
&lt;appender-ref ref=&quot;console&quot; /&gt;
&lt;/Root&gt;
&lt;/Loggers&gt;
&lt;/Configuration&gt;

huangapple
  • 本文由 发表于 2020年10月16日 00:10:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64375609.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定