在Spring MVC或Spring Boot中使用Filter打印请求参数问题

 使用Spring MVC或Spring Boot中打印或记录日志一般使用AOP记录Request请求和Response响应参数,在不使用AOP的前提下,如果在Filter中打印日志,在打印或消费请求类型为Content-Type:application/json的请求时,会出现严重的问题。

在Spring体系中,过滤器的定义我们一般采用继承OncePerRequestFilter的方式,当然也可以使用原始的Filter。

错误写法一:

如果不对request和response进行处理,使用伪代码采用如下写法打印请求和响应参数(注:此时request请求类型为Post,接收的是Json数据)

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse             
                  response, FilterChain filterChain) throws ServletException, IOException {
        filterChain.doFilter(request, response);
        printRequestLog(request);
        printResonseLog(response);
    }

       运行测试后你会发现抛出如下异常: 

java.io.IOException: Stream closed
	at org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:359) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
	at org.apache.catalina.connector.CoyoteInputStream.read(CoyoteInputStream.java:132) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
	at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284) ~[na:1.8.0_191]
	at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326) ~[na:1.8.0_191]
	at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178) ~[na:1.8.0_191]
	at java.io.InputStreamReader.read(InputStreamReader.java:184) ~[na:1.8.0_191]
	at java.io.BufferedReader.fill(BufferedReader.java:161) ~[na:1.8.0_191]
	at java.io.BufferedReader.readLine(BufferedReader.java:324) ~[na:1.8.0_191]
	at java.io.BufferedReader.readLine(BufferedReader.java:389) ~[na:1.8.0_191]
	at com.micro.backend.filter.LoggingFilter.getBodyString(LoggingFilter.java:60) [classes/:na]
	at com.micro.backend.filter.LoggingFilter.doFilterInternal(LoggingFilter.java:49) [classes/:na]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.31.jar:9.0.31]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.31.jar:9.0.31]
	at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) [spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.31.jar:9.0.31]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.31.jar:9.0.31]
	at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) [spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.31.jar:9.0.31]
	at org.apache.ca
  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring Boot ,获取 API 接口注解的方法与 Spring MVC 类似,可以使用 RequestMappingHandlerMapping 来获取所有的请求处理方法及其对应的请求路径等信息,但是无法直接获取 @api 注解信息。 如果您想要获取使用了 @api 注解的 API 接口,可以通过自定义注解扫描器实现。具体方法如下: 1. 定义一个自定义注解,例如 @MyApi。 2. 在 Spring Boot 启动类上标注 @ComponentScan 注解,例如: @SpringBootApplication @ComponentScan(basePackages = "com.example", includeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {MyApi.class}) }) public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } 3. 在代码使用自定义注解 @MyApi 标注 API 接口,例如: @RestController public class DemoController { @GetMapping("/api/test") @MyApi(name = "测试接口", description = "用于测试的接口") public String test() { return "test"; } } 4. 在代码获取使用了 @MyApi 注解的 API 接口,例如: @Autowired private RequestMappingHandlerMapping handlerMapping; public List<ApiInfo> getApiInfoList() { List<ApiInfo> apiInfoList = new ArrayList<>(); Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods(); for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethods.entrySet()) { RequestMappingInfo mappingInfo = entry.getKey(); HandlerMethod handlerMethod = entry.getValue(); Method method = handlerMethod.getMethod(); MyApi api = method.getAnnotation(MyApi.class); if (api != null) { String name = api.name(); String description = api.description(); String[] patterns = mappingInfo.getPatternsCondition().getPatterns().toArray(new String[0]); RequestMethod[] methods = mappingInfo.getMethodsCondition().getMethods().toArray(new RequestMethod[0]); apiInfoList.add(new ApiInfo(name, description, patterns, methods)); } } return apiInfoList; } 通过以上方法,您就可以获取使用了 @MyApi 注解的 API 接口信息了。需要注意的是,如果您的自定义注解不在 Spring Boot 启动类所在的包及其子包,需要使用 includeFilters 参数来指定需要扫描的包和注解类型。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值