7

我发现 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 );
}

所以,问题是当 id 包含点(例如“my_file.wav”)时,Spring 在任何情况下都会返回 HTTP 406,但如果 id 不包含点,Spring 在我 expet 时返回 responseState(as json)。我尝试通过不同的方式修复它(添加@ResponseBody,更改杰克逊版本,将 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 不会忽略文件扩展名

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

4

2 回答 2

3

在您的 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 的末尾碰巧有点时,它只会导致问题。

于 2015-05-18T15:07:50.980 回答
1

您需要像这样定义自定义内容协商管理器服务:

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />

<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <property name="favorPathExtension" value="false" />
</bean>

来自这篇文章

于 2015-05-26T18:06:25.437 回答