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

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

TestController.java

@RestController
@RequestMapping("/test")
public class TestController {

    @PostMapping("/t1")
    public Object test(@RequestBody Set 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 {

    @Override
    public 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
    @Setter
    private String body;

    ParamsRequestWrapper(HttpServletRequest request) throws IOException {
        super(request);
        body = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8);
    }

    @Override
    public ServletInputStream getInputStream() {
        final ByteArrayInputStream bais = new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8));
        return new ServletInputStream() {
            @Override
            public boolean isFinished() {
                return false;
            }

            @Override
            public boolean isReady() {
                return false;
            }

            @Override
            public void setReadListener(ReadListener listener) {

            }

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

    @Override
    public 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 {

    @Override
    public 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;
    }

}

测试结果

SpringMVC interceptor 中获取 @RequestBody 的内容并修改_第1张图片

SpringMVC interceptor 中获取 @RequestBody 的内容并修改_第2张图片

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

你可能感兴趣的:(Java,spring)