@RequestBody 空格去除
时间: 2023-11-08 12:06:02 浏览: 189
乐优商城.xmind
您可以使用SpringMVC中的过滤器或拦截器来去除@RequestBody中的空格。具体实现可以参考以下代码:
```java
@Component
public class RequestBodyTrimFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper((HttpServletRequest) request);
String requestBody = new String(requestWrapper.getContentAsByteArray(), requestWrapper.getCharacterEncoding()); requestBody = requestBody.replaceAll("\\s+", "");
final byte[] requestBodyBytes = requestBody.getBytes();
requestWrapper.setContentLength(requestBodyBytes.length);
requestWrapper.setCharacterEncoding("UTF-8");
requestWrapper.resetInputStream();
chain.doFilter(requestWrapper, response);
}
}
```
阅读全文