我有以下声明:
@RequestMapping(value= "/", method = RequestMethod.POST, headers = "Accept=application/vnd.woot4moo-v1+json"
public @ResonseBody void create(String key){...}
这当然需要json
,但我希望它拒绝任何不包含标题vnd.woot4moo-v1
部分的请求。这甚至可行吗?
答案 0 :(得分:1)
您可以通过实施HandlerInterceptor
来过滤特定请求,如下所示:
RequestMethodInterceptor类:
public class RequestMethodInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
//Get the 'Accept' header value
String headerValue = request.getHeader("Accept");
//check the request contains expected header value
if(!headerValue.equals("application/vnd.woot4moo-v1+json")) {
//Reject and Log or Ignore upon your requirement & return false
return false;
} else {
return true;
}
}
}
XML配置:
<mvc:interceptors>
<bean class="xyz.RequestMethodInterceptor" />
</mvc:interceptors>