java Spring启动控制器应该如何处理“丢失”的请求头?
我正在编写一个Spring启动应用程序。我的控制器有2个自定义请求头。我执行了几个测试,结果发现我的应用程序在头不存在时返回“404”
然而,我期望这会导致“400”错误
有人能详细说明为什么会发生这种情况吗?我该如何妥善处理?就像在告诉服务的消费者报头丢失一样
@RestController("fundsConfirmationController")
@RequestMapping(
value="/accounts/{accountId}/funds-confirmations",
headers = {"X-CAF-MSGID", "X-AccessToken"}
)
public class FundsConfirmationController implements FundsConfirmationControllerI{
private FundsConfirmationServiceI fundsConfirmationService;
@Autowired
public FundsConfirmationController(FundsConfirmationServiceI fundsConfirmationService){
this.fundsConfirmationService = fundsConfirmationService;
}
@GetMapping(
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity<?> fundsConfirmation(@RequestHeader(value="X-CAF-MSGID") String messageId,
@RequestHeader(value="X-AccessToken") String accessToken,
FundsConfirmationRequest requestParams) { ... }
# 1 楼答案
有两种解决方案可以做到这一点
首先使用}
@RequestHeader
和required
{第二次使用
HttpServletRequest
而不是@RequestHeader