2015-05-18 13 views
5

Tôi đã tìm thấy một hành vi rất lạ của Spring MVC.Spring MVC trả lại HTTP 406 trên URL có dấu chấm

Tôi có bộ điều khiển với phương pháp:

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

Vì vậy, vấn đề là khi id chứa dấu chấm (ví dụ "my_file.wav".) Trở về mùa xuân HTTP 406 trong mọi trường hợp, nhưng nếu id không chứa dấu chấm , Spring trả về responseState (như json) khi tôi hết hạn. Tôi đã cố sửa nó theo cách khác (thêm @ResponseBody, thay đổi phiên bản jackson, hạ cấp Spring xuống 4.0) nhưng không có kết quả nào.

Bất kỳ ai có thể giúp tôi không?

CẬP NHẬT tôi cho phép các bản ghi cho mùa xuân mvn và thấy

ID này chứa dot:

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 không chứa dấu chấm:

DEBUG org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdviceChain - Invoking ResponseBodyAdvice chain for [email protected] 
DEBUG org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdviceChain - After ResponseBodyAdvice chain [email protected] 

SOLUTION

Spring does not ignore file extension

SpringMVC: Inconsistent mapping behavior depending on url extension

Trả lời

2

Trong xml servlet của bạn, tắt phù hợp với hậu tố Spring:

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

Đây là một tính năng cho phép người gọi để chỉ định cách họ muốn nội dung của họ được trả về bởi bám nó như là một hậu tố ở cuối URL:

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

Nhưng 99 trong số 100 dự án không sử dụng tính năng này. Và nó chỉ gây ra vấn đề khi có dấu chấm ở cuối URL.

+0

câu trả lời của bạn có vẻ đúng, nhưng nó không giúp tôi. Mùa xuân vẫn gửi HTTP 406. – Vartlok

+0

bây giờ hãy thử thay đổi ánh xạ yêu cầu để chỉ là '@RequestMapping (value ="/delete/{id} ", method = RequestMethod.DELETE)' –

+0

Và vẫn còn HTTP 406 – Vartlok

0

Bạn cần có một dịch vụ quản lý đàm phán nội dung tùy chỉnh được định nghĩa như thế này:

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

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

Đến từ này article

Các vấn đề liên quan