【发布时间】:2020-10-15 16:10:24
【问题描述】:
我有一个 spring boot 应用程序,它充当 ionic 应用程序的服务,每当我调用 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,但它在我的系统中不可用,其他答案也无济于事,请问有什么解决方案吗?
【问题讨论】:
-
如果同一个 API 调用了两次,那么为什么两个请求的电话号码不同。
-
你调试过你的代码吗?可能问题出在记录器上。
-
你在某处有什么过滤器吗?或检查 chain.doFilter(req, res)
-
@Alien 我在提问时忘记编辑这些电话号码。其实是同一个数字
-
@silentsudo 不,先生,没有这样的过滤器。
标签: java spring spring-boot controller spring-restcontroller