Handle Exceptions when using @RequestHeader in spring application

▼魔方 西西 提交于 2019-12-26 12:23:06

问题


Here is my Java code that uses the Spring Framework:

@RequestMapping(method = RequestMethod.POST)
public @ResponseBody String SampleFunction(@RequestHeader("Authorization") String details)
{
    System.out.println("Authorization details recieved");

}

I am trying to access Authorization header. I want to handle the missing Authorization header by redirecting it to a 400 Bad Request page. How can I do this?


回答1:


By default the header is required. So if it is missing, you will get an exception. However, see code below. Now, if it is missing, the details string will be null.

@RequestHeader(required = false, value = "Authorization") String details



回答2:


If the header value is missing by default the response is 400 Bad Request. You just need to configure redirecting.



来源:https://stackoverflow.com/questions/13601150/handle-exceptions-when-using-requestheader-in-spring-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!