java - Spring MVC 在带点的 URL 上返回 HTTP 406

标签 java spring spring-mvc path-variables http-delete

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

我有 Controller 和方法:

@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 );
}

所以,问题是当 id 包含点(例如“my_file.wav”)时,Spring 在任何情况下都会返回 HTTP 406,但如果 id 不包含点,Spring 会返回 responseState(as 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

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

解决方案

Spring does not ignore file extension

SpringMVC: Inconsistent mapping behavior depending on url extension

最佳答案

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

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

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

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

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

关于java - Spring MVC 在带点的 URL 上返回 HTTP 406,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30306311/

相关文章:

java - 使用 MapReduce 将 CSV 文件加载到 Hbase 表中

spring - 在 Spring 3 中绑定(bind)多个选项,其中选项值可能包含逗号

java - Spring MVC 中单例 bean 多次实例化?

java - JTable 未显示。我如何解决它?

java - 具有自定义类型的 Eclipse Content Assist

java - 从下载的源码目录运行Netty 4.0的DiscardServer

json - spring MockMVC 中是否有一种方法可以将 json 内容作为对象获取?

java - Spring 休息模板 : Host name 'localhost' does not match the certificate subject provided by the peer

java - Spring配置@ResponseBody JSON格式

java - 如何根据条件应用Spring消息转换器?