当前位置: 代码迷 >> 综合 >> SpringMVC interceptor 中获取 @RequestBody 的内容并修改
  详细解决方案

SpringMVC interceptor 中获取 @RequestBody 的内容并修改

热度:1   发布时间:2023-12-11 15:59:52.0

在springmvc interceptor 中获取并修改请求的内容。

TestController.java

@RestController
@RequestMapping("/test")
public class TestController {@PostMapping("/t1")public Object test(@RequestBody Set<Integer> list) {return list;}}

ParamsRequestFilter.java

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.ServletComponentScan;import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;@Slf4j
@ServletComponentScan
@WebFilter(filterName = "ParamsRequestFilter", urlPatterns = {"/test/*"})
public class ParamsRequestFilter implements Filter {@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {chain.doFilter(new ParamsRequestWrapper((HttpServletRequest) request), response);}
}

ParamsRequestWrapper.java

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.io.IOUtils;import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;public class ParamsRequestWrapper extends HttpServletRequestWrapper {@Getter@Setterprivate String body;ParamsRequestWrapper(HttpServletRequest request) throws IOException {super(request);body = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8);}@Overridepublic ServletInputStream getInputStream() {final ByteArrayInputStream bais = new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8));return new ServletInputStream() {@Overridepublic boolean isFinished() {return false;}@Overridepublic boolean isReady() {return false;}@Overridepublic void setReadListener(ReadListener listener) {}@Overridepublic int read() {return bais.read();}};}@Overridepublic BufferedReader getReader() {return new BufferedReader(new InputStreamReader(getInputStream()));}
}

ParamsInterceptor.java

import com.alibaba.fastjson.JSON;
import com.starxg.soluo.front.filter.ParamsRequestWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;@Slf4j
public class ParamsInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest or, HttpServletResponse response, Object handler) {ParamsRequestWrapper request = (ParamsRequestWrapper) or;log.info("源:{}", request.getBody());request.setBody(JSON.parseObject(request.getBody()).getJSONArray("a").toString());log.info("后:{}", request.getBody());return true;}}

测试结果

从代码中可以看出我们也可以在filter中修改。那为什么要在Interceptor中修改呢?因为在Interceptor可以抛出异常并交给Spring捕获 。如果在Filter中抛异常不方便处理。

  相关解决方案