HandlerInterceptor拦截器 :request body is missing OR Stream closed

使用HandlerInterceptor拦截器,都有可能遇到的一个问题

你只要你用了,你就很熟悉,不熟悉的你也不会看,是不是这个道理...
遇到错误主要有这两个:

1.HttpMessageNotReadableException: Required request body is missing

2.exception is java.io.IOException: Stream closed

原因:
由于 request中getReader()和getInputStream()只能调用一次
所以在Controller里面方法上@ResponseBody回再次调用一次getInputStream()会报错

通常做法是:
一、添加HttpServletRequestWrapper 类并继承 HttpServletRequestWrapper 包装类

/**
 * @Description: 设置拦截
 * @author: 你瞅瞅~~~~
 * @create: 2020-01-10 14:51
 */
public class HttpServletRequestWrapper extends HttpServletRequestWrapper {  
    private final byte\[\] body;   
    private HttpServletRequest delegate;  
    public HttpServletRequestWrapper(HttpServletRequest request) throws IOException {    
           super(request);    
    body \= StreamUtil.readBytes(request.getInputStream());  
    delegate \= request;  
    }    

    @Override    
    public BufferedReader getReader() throws IOException {  
           return new BufferedReader(new InputStreamReader(getInputStream(), Constant.DEFAULT\_ENCODING));  
    }    

    @Override    
    public ServletInputStream getInputStream() throws IOException {    
           final ByteArrayInputStream bais = new ByteArrayInputStream(body);    
    return new ServletInputStream() {    

    @Override    
    public int read() throws IOException {    
                   return bais.read();    
    }  

    @Override  
    public boolean isFinished() {  
        // TODO Auto-generated method stub  
       return false;  
    }  

    @Override  
    public boolean isReady() {  
       // TODO Auto-generated method stub  
       return false;  
    }  

    @Override  
    public void setReadListener(ReadListener arg0) {  
            // TODO Auto-generated method stub  
          }    
       };    
    }    

    @Override    
    public String getParameter(String key)  {    
           return delegate.getParameter(key);    
    }    
  
}

写到这里简直就是福利
二、定义统一的RequestWrapperFilter

 /**
 * @Description: filter 所有请求,将request 转化为 HttpServletRequestWrapper 向下传递,不丢失request body
 * @author: 你瞅瞅~~
 * @create: 2020-01-10 14:51
 */
 public class RequestWrapperFilter implements Filter {  
    @Override  
    public void init(FilterConfig filterConfig) throws ServletException {  
  
    }  
  
    @Override  
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {  
        HttpServletRequest servletRequest = (HttpServletRequest) request;  
        if ("application/x-www-form-urlencoded".equals(servletRequest.getHeader("content-type"))) {  
          filterChain.doFilter(servletRequest, response);  
        }else{  
          MAPIHttpServletRequestWrapper myRequestWrapper = new MAPIHttpServletRequestWrapper((HttpServletRequest) request);  
         filterChain.doFilter(myRequestWrapper, response);  
        }  
    }  
  
   @Override  
   public void destroy() {  
  
   }  
}

三、在项目设置FilterConfig
实例化wrapperFilter 起到拦截所有请求,这样就可以把request传递到我们使用HandlerInterceptor和RequestBody里。

/**
 * @Description: 设置请求过滤  
 * @author: 你瞅瞅~~
 * @create: 2020-01-10 14:51
 */
@Configuration  
public class FilterConfig {  
  
    @Bean  
  public FilterRegistrationBean someFilterRegistration() {  
        FilterRegistrationBean registration = new FilterRegistrationBean();  
        registration.setFilter(wrapperStreamFilter());  
        registration.addUrlPatterns("/login/users/*");  
        registration.setName("streamFilter");  
        return registration;  
  }  
  
  
    @Bean(name \= "wrapperStreamFilter")  
    public Filter wrapperStreamFilter() {  
        return new RequestWrapperFilter();  
  }
  
}

这一篇和上一篇HandlerInterceptor拦截器是姐妹篇,有兴趣或者最近项目想使用,可以参考哟...
有些方法可以提到公共位置,例如common之类的依赖里,这样我们就可以统一供大家调用岂不是方便。

你可能感兴趣的:(java,拦截器)