简体   繁体   中英

Spring controller method is getting called multiple times

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?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM