缺少供应商标头的Spring拒绝请求(自定义MIME类型)

时间:2016-10-31 14:14:37

标签: java spring header mime-types

我有以下声明:

@RequestMapping(value= "/", method = RequestMethod.POST, headers = "Accept=application/vnd.woot4moo-v1+json"  

public @ResonseBody void create(String key){...}

这当然需要json,但我希望它拒绝任何不包含标题vnd.woot4moo-v1部分的请求。这甚至可行吗?

1 个答案:

答案 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>