• Spring Data Rest 支持Put请求


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




    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 }

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

     <!--解决使用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>
  • 相关阅读:
    Redis 2种持久化模式的缺陷
    我看过得最易懂的一段AOP的解释
    mysql-高性能索引策略
    几款效率神器助你走上人生巅峰
    shell脚本报错:"[: =: unary operator expected"
    CentOS7中使用iptables
    php foreach用法和实例
    shell 学习四十五天---xargs
    chain issues incorrect order,EXtra certs,Contains anchor
    Ubuntu 能ping通DNS 地址 无法解析域名
  • 原文地址:https://www.cnblogs.com/zhuwenjun/p/3756639.html
Copyright © 2020-2023  润新知