Spring Header @RequestMapping with equals

I want to use Springs @RequestMapping with a header attribute to detect an Accept header with value application/json;version=1.* . The plan is for the other method to display similarly for version 2, which will have the value application/json;version=2.* .

Spring seems to be ignoring the meaning of the version. I assume that it treats the equal sign as another attribute of the header.

Is there any way around this?

Side notes:

  • I cannot upgrade Spring version to support consumes attribute
  • I cannot change the request header format
+1
java spring header
source share
1 answer

It seems that spring requestmapping is ignoring media type parameters. You can get around this by manually sending a request to your preferred endpoint.

 @RequestMapping(value = "/", headers = "Accept=application/json") @ResponseBody String request(@RequestHeader HttpHeaders headers){ for(MediaType mediaType : headers.getAccept()){ if(mediaType.isCompatibleWith(MediaType.APPLICATION_JSON)){ if(mediaType.getParameter("version").startsWith("1.")){ return v1(); }else if(mediaType.getParameter("version").startsWith("2.")){ return v2(); } } } return "error"; } 
+1
source share

All Articles