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
source share