2016-10-31 5 views
0

У меня есть следующее объявление:Spring отклонить запрос, который отсутствует заголовок поставщика (Пользовательские MIME типа)

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

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

Это, конечно, будет принимать json, но я хочу, чтобы отклонить любой запрос, который не содержит vnd.woot4moo-v1 части заголовка. Возможно ли это?

ответ

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>