第三方异常处理(org.zalando)0.27.0版本,关闭stackTrace显示。

引入的依赖版本

<!--   第三方统一异常处理     -->
<dependency>
   <groupId>org.zalando</groupId>
   <artifactId>problem-spring-web</artifactId>
   <version>0.27.0</version>
</dependency>

没有配置WebConfiguration前,通过IDEA的HTTP Client工具模拟请求,数据响应 Content length: 15773 bytes

POST http://localhost:27001/api/authLoginController/login

HTTP/1.1 400 
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Type: application/problem+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 29 Jun 2022 10:06:39 GMT
Connection: close

{
  "cause": null,
  "stackTrace": [
    {
      "methodName": "login",
      "fileName": "AuthLoginController.java",
      "lineNumber": 34,
      "className": "com.example.controller.AuthLoginController",
      "nativeMethod": false
    },
    //.........省略140+
    {
      "methodName": "invoke0",
      "fileName": "NativeMethodAccessorImpl.java",
      "lineNumber": -2,
      "className": "sun.reflect.NativeMethodAccessorImpl",
      "nativeMethod": true
    }
  ],
  "type": "https://www.jhipster.tech/problem/problem-with-message",
  "title": "参数校验失败",
  "status": "BAD_REQUEST",
  "detail": null,
  "instance": null,
  "parameters": {},
  "errorCode": "1002",
  "errorMessage": "参数校验失败",
  "message": "参数校验失败",
  "localizedMessage": "参数校验失败",
  "suppressed": []
}

Response code: 400; Time: 136ms; Content length: 15773 bytes

 配置WebConfiguration​​​​​​​后,数据响应 Content length: 138 bytes

HTTP/1.1 400 
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Type: application/problem+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 29 Jun 2022 10:10:06 GMT
Connection: close

{
  "errorCode": "1002",
  "errorMessage": "参数校验失败",
  "type": "https://www.jhipster.tech/problem/problem-with-message",
  "title": "参数校验失败",
  "status": 400
}

Response code: 400; Time: 128ms; Content length: 138 bytes

前后的响应大小非常可观,并且清爽了不少。

WebConfiguration文件,配置WebConfiguration文件, 需要开启@EnableWebMvc注解。

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.zalando.problem.jackson.ProblemModule;
import org.zalando.problem.violations.ConstraintViolationProblemModule;

import java.util.List;


@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.example.controller"})
public class WebConfiguration implements WebMvcConfigurer {

    /**
     * 关闭栈堆响应回显
     * @param converters
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        ObjectMapper mapper = Jackson2ObjectMapperBuilder.json()
                .modules(new ProblemModule(), new ConstraintViolationProblemModule()).build();

        converters.add(new MappingJackson2HttpMessageConverter(mapper));
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值