问题
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