Spring MVC使用dot返回URL上的HTTP 406

Var*_*lok 5 java spring path-variables spring-mvc http-delete

我发现了Spring MVC的一个非常奇怪的行为.

我有控制器方法:

@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 );
}
Run Code Online (Sandbox Code Playgroud)

所以,问题是当id包含点(例如"my_file.wav")时,Spring在任何情况下都会返回HTTP 406,但是如果id不包含点,则Spring会按照我的方式返回responseState(作为json).我尝试以不同的方式修复它(添加@ResponseBody,更改jackson版本,将Spring降级到4.0)但没有任何结果.

谁能帮我?

更新我为Spring MVN启用日志并看到了这一点

ID包含点:

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
Run Code Online (Sandbox Code Playgroud)

ID不包含点:

DEBUG org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdviceChain - Invoking ResponseBodyAdvice chain for body=my.package.response.Response@1e66a392
DEBUG org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdviceChain - After ResponseBodyAdvice chain body=my.package.response.Response@1e66a392
Run Code Online (Sandbox Code Playgroud)

Spring不会忽略文件扩展名

SpringMVC:取决于url扩展名的映射行为不一致

Dav*_*der 3

在你的servlet xml中,关闭Spring的后缀匹配:

<mvc:annotation-driven>
    <mvc:path-matching registered-suffixes-only="true"/>
</mvc:annotation-driven>
Run Code Online (Sandbox Code Playgroud)

此功能允许调用者通过将其作为后缀粘贴在 URL 末尾来指定他们希望如何返回内容:

GET /user/bob.json
GET /use/bob.jsp
Run Code Online (Sandbox Code Playgroud)

但 100 个项目中有 99 个不使用此功能。当 URL 末尾恰好有点时,它只会导致问题。