码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Data Rest 支持Put请求

时间:2014-05-29 10:52:20      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:style   c   class   blog   code   java   

  最近在用 Spring Data Rest 写WebApi 遇到Put请求后报415 Mieda Type 的错误,GG了下 找到个解决办法 ,下面是针对Put请求的过滤。

bubuko.com,布布扣



 1 public class HttpPutRestfulContentFilter extends OncePerRequestFilter {
 2 
 3     private Charset charset = Charset.forName("UTF-8");
 4     private final String method="PUT";
 5 
 6     @Override
 7     protected void doFilterInternal(final HttpServletRequest request, HttpServletResponse response,
 8                                     FilterChain filterChain) throws ServletException, IOException {
 9 
10         if (this.method.equals(request.getMethod())) {
11             HttpInputMessage inputMessage = new ServletServerHttpRequest(request) {
12                 @Override
13                 public InputStream getBody() throws IOException {
14                     return request.getInputStream();
15                 }
16             };
17 
18             HttpServletRequest wrapper = new HttpMethodRequestWrapper(request, this.method);
19             filterChain.doFilter(wrapper, response);
20         }
21         else {
22             filterChain.doFilter(request, response);
23         }
24     }
25 
26     private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper {
27 
28         private final String method;
29 
30         public HttpMethodRequestWrapper(HttpServletRequest request, String method) {
31             super(request);
32             this.method = method;
33         }
34 
35         @Override
36         public String getMethod() {
37             return this.method;
38         }
39     }
40 }
bubuko.com,布布扣

 

  需要在web.xml中添加这个自定义过滤 

bubuko.com,布布扣
 <!--解决使用PUT请求接收不到参数的问题 -->
    <filter>
        <filter-name>HttpMethodFilter</filter-name>
        <filter-class>org.tccsdk.core.code.filter.HttpPutRestfulContentFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>HttpMethodFilter</filter-name>
        <servlet-name>api</servlet-name>
    </filter-mapping>

    <!-- Spring child -->
    <servlet>
        <servlet-name>api</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>api</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
bubuko.com,布布扣

 

Spring Data Rest 支持Put请求,布布扣,bubuko.com

Spring Data Rest 支持Put请求

标签:style   c   class   blog   code   java   

原文地址:http://www.cnblogs.com/zhuwenjun/p/3756639.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!