Spring MVC returns HTTP 406 at URL with period

I found one very strange behavior of Spring MVC.

I have a controller with a method:

@RequestMapping (value = "/delete/{id:.*}", method = RequestMethod.DELETE) public ResponseEntity<Response> delete(@PathVariable (value = "id") final String id) { HttpStatus httpStatus = HttpStatus.OK; final Response responseState = new Response( ResponseConstants.STATUS_SUCCESS ); try { POJO pojo = mediaFileDao.findById( id ); if (pojo != null) { delete(pojo); } else { httpStatus = HttpStatus.NOT_FOUND; responseState.setError( "NOT_FOUND" ); } } catch (Exception e) { httpStatus = HttpStatus.INTERNAL_SERVER_ERROR; responseState.setError( e.getMessage() ); } return new ResponseEntity<>( responseState, httpStatus ); } 

So, the problem is that the identifier contains a dot (for example, "my_file.wav") Spring returns HTTP 406 anyway, but if the id does not contain a dot, Spring returns responseState (like json) as I expet. I tried to fix it differently (add @ResponseBody, change Jackson version, downgrade Spring to 4.0), but without any results.

Can anyone help me?

UPDATE I turn on the logs for Spring MVN and saw this

The identifier contains a period:

 DEBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - Resolving exception from handler [public org.springframework.http.ResponseEntity<my.package.response.Response> my.package.Controller.deleteMediaFile(java.lang.String) throws java.lang.Exception]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation DEBUG org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver - Resolving exception from handler [public org.springframework.http.ResponseEntity<my.package.response.Response> my.package.Controller.deleteMediaFile(java.lang.String) throws java.lang.Exception]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation DEBUG org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolving exception from handler [public org.springframework.http.ResponseEntity<my.package.response.Response> my.package.Controller.deleteMediaFile(java.lang.String) throws java.lang.Exception]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation 

ID does not contain a dot:

 DEBUG org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdviceChain - Invoking ResponseBodyAdvice chain for body=my.package.re sponse.Response@1e66a392 DEBUG org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdviceChain - After ResponseBodyAdvice chain body=my.package.response.Response@1e66a392 

Decision

Spring does not ignore file extension

SpringMVC: inconsistent display behavior depending on URL extension

+5
source share
2 answers

In your xml servlet, disable the Spring suffix:

 <mvc:annotation-driven> <mvc:path-matching registered-suffixes-only="true"/> </mvc:annotation-driven> 

This is a feature that allows callers to specify how they want to return their contents by inserting them as a suffix at the end of the URL:

 GET /user/bob.json GET /use/bob.jsp 

But 99 out of 100 projects do not use this feature. And that just causes problems when dots appear at the end of the URL.

+2
source

You should have a customized content negotiation manager command like this:

 <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" /> <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> <property name="favorPathExtension" value="false" /> </bean> 

Came from this article

0
source

All Articles