SpringBoot过滤器获取POST请求的JSON参数

项目中需要将每个请求的路径和请求参数以及响应结果,都记录在日志中,这样在出现问题时可以快速定位是哪里出现了问题。想到了使用过滤器来实现这个功能,当请求来到过滤器时,会有一个Request参数,通过该参数就能获取到请求路径和请求参数,以及相关内容

parameterMap = httpRequest.getParameterMap();
String requestMethod = httpRequest.getMethod();
String remoteAddr = httpRequest.getRemoteAddr();
int remotePort = httpRequest.getRemotePort();

上面的getParameterMap(),只能够获取到GET请求的参数,如果是POST方法传的JSON那就没法获取到,那如何获取呢,POST的请求是在请求体body中,而POST请求中的body参数是已流形式存在的,所以我们可以通过获取到输入流来获取body

ServletInputStream inputStream = httpRequest.getInputStream();
InputStreamReader reader = new InputStreamReader(inputStream,StandardCharsets.UTF_8);
BufferedReader bfReader = new BufferedReader(reader);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bfReader.readLine()) != null){
sb.append(line);
}
System.out.println(sb.toString());

通过上面的方法,我们确实能在过滤器中获取到POST的JSON参数了,但是按照上面的方法实现的过滤器,我们会发现,当请求经过过滤器来到Controller的时候,请求参数不见了

image-20201219133609844

可以看到,过滤器确实拿到JSON参数,但是接着报了一个request body missing的异常,也就是请求来到Controller时,参数没有了,这是为啥呢?我们先去源码看看,Controller平时是怎么拿到请求参数的吧

image-20201219134042794

根据DeBug,可以看到SpringBoot处理请求的最主要的两个方法是上图红框的doServicedoDisparch方法,上面就是通过反射去获取参数名去匹配等

image-20201219134417523

来到invokeForRequest方法,这里面的getMethodArgumentValues,就是SpringBoot获取请求参数的入口,进入入口后

image-20201219135249418

再经过上面的红框,就能看到SpringBoot获取POST请求JSON的参数的真面目了

image-20201219135426586

从源码我们可以看到,SpringBoot也是通过获取request的输入流来获取参数,这样上面的疑问就能解开了,为什么经过过滤器来到Controller请求参数就没了,这是因为 InputStream read方法内部有一个,postion,标志当前流读取到的位置,每读取一次,位置就会移动一次,如果读到最后,InputStream.read方法会返回-1,标志已经读取完了,如果想再次读取,可以调用inputstream.reset方法,position就会移动到上次调用mark的位置,mark默认是0,所以就能从头再读了。但是呢 是否能reset又是由markSupported决定的,为true能reset,为false就不能reset,从源码可以看到,markSupported是为false的,而且一调用reset就是直接异常
image-20201219140007286

所以这也就代表,InputStream只能被读取一次,后面就读取不到了。因此我们在过滤器的时候,已经将InputStream读取过了一次,当来到Controller,SpringBoot读取InputStream的时候自然是什么都读取不到了

image-20201219140830451

既然InputStream只能读取一次,那我们可以把InputStream给保存下来,然后完整的传下去SpringBoot就可以读取到了,这里就需要用到HttpServletRequest的包装类HttpServletRequestWrapper了,该类可以自定义一些方法。我们创建一个类并继承这个包装类

public class RequestWrapper extends HttpServletRequestWrapper {
    private final byte[] body;

    public RequestWrapper(HttpServletRequest request) throws IOException {
        super(request);
        //保存一份InputStream,将其转换为字节数组
        body = StreamUtils.copyToByteArray(request.getInputStream());
    }

	//转换成String
    public String getBodyString(){
        return new String(body,StandardCharsets.UTF_8);
    }

    @Override
    public BufferedReader getReader() throws IOException {
        return new BufferedReader(new InputStreamReader(getInputStream()));
    }

	//把保存好的InputStream,传下去
    @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() {
                return false;
            }

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

            @Override
            public void setReadListener(ReadListener readListener) {
            }
        };
    }
}

通过保存一份流,就可实现在过滤器中能拿到JSON参数,同时Controller也不会丢失参数

image-20201219141929107

有一点需要注意的,在过滤器放行的时候,放行的是包装类和而不是原来的Request

image-20201219142124831

  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 在Spring Boot中,接收HTTP请求参数乱码通常是由于默认字符编码不正确所致。解决该问题的方法有以下几种: 1. 使用RequestMapping或GetMapping注解的方法时,可在注解中指定produces和consumes属性,并指定字符编码。例如: ```java @RequestMapping(value = "/example", produces = "application/json;charset=UTF-8", consumes = "application/json;charset=UTF-8") public String example(@RequestBody String requestParam) { // 处理请求参数 } ``` 2. 修改Spring Boot应用的全局字符编码设置,可在application.properties或application.yml文件中配置。例如在application.properties中添加: ``` spring.http.encoding.force=true spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true ``` 3. 可以通过在Spring Boot的启动类中添加Filter或Interceptor,手动处理请求参数的字符编码。例如创建一个字符编码过滤器: ```java @Component public class CharacterEncodingFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException {} @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding("UTF-8"); chain.doFilter(request, response); } @Override public void destroy() {} } ``` 通过以上方法,可以解决Spring Boot接收HTTP请求参数乱码的问题。在实际应用中,根据具体需求选择适合的解决方案。 ### 回答2: 在Spring Boot中,接收HTTP请求参数乱码的问题通常是由于字符编码不一致导致的。可以通过以下几种方式来解决: 1. 使用过滤器(Filter): 可以在Spring Boot中注册一个字符编码过滤器,通过将所有的请求和响应的字符编码都设置为UTF-8来避免乱码问题。在Spring Boot中可以通过重写WebMvcConfigurer接口中的addInterceptors方法来注册过滤器。 2. 在application.properties(application.yml)文件中设置字符编码: 可以通过在application.properties(application.yml)文件中添加以下配置来设置字符编码: spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true spring.http.encoding.force=true 3. 使用@RequestParam注解指定字符编码: 可以在Controller中的接收参数的方法上使用@RequestParam注解,并通过设置其value属性来指定字符编码。 例如: @RequestMapping("/test") public String test(@RequestParam(value = "name", required = false) String name) { // ...业务逻辑 return "success"; } 4. 在请求头中指定字符编码: 可以在发送HTTP请求时,在请求头中指定字符编码为UTF-8。例如,在使用HttpClient发送请求时,可以使用setHeader方法设置字符编码。 例如: HttpClient httpClient = new DefaultHttpClient(); HttpPost post = new HttpPost(url); post.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); // ...设置请求参数 HttpResponse response = httpClient.execute(post); 通过以上几种方式,可以解决Spring Boot接收HTTP请求参数乱码的问题。 ### 回答3: 当Spring Boot接收到HTTP请求参数乱码的情况时,可以采取以下措施解决问题。 首先,可以在Spring Boot的配置文件application.properties(或application.yml)中添加以下配置,设置请求编码格式为UTF-8: ``` spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true spring.http.encoding.force=true ``` 同时,可以使用过滤器请求进行编码处理。在新建一个类中,实现javax.servlet.Filter接口,并重写doFilter方法: ``` import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; @WebFilter(filterName = "encodingFilter", urlPatterns = "/*") public class EncodingFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); chain.doFilter(request, response); } } ``` 在上述代码中,设置请求和响应的编码格式为UTF-8。 然后,可以为Spring Boot的主类添加一个注解,启用过滤器: ``` import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication @ServletComponentScan public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 在上述代码中,使用了@SpringBootAppliaction注解标记为Spring Boot主类,并使用@ServletComponentScan注解扫描过滤器。 最后,可以在控制器中使用@RequestParam注解显式指定请求参数的编码格式。例如: ``` import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/hello") public String hello(@RequestParam(value = "name", required = false) String name) { if (!StringUtils.isEmpty(name)) { // 对name参数进行进一步处理 } return "Hello, " + name; } } ``` 在上述代码中,使用@RequestParam注解指定了name参数,并设置了编码格式。 通过以上措施,可以解决Spring Boot接收HTTP请求参数乱码的问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值