引入的依赖版本
<!-- 第三方统一异常处理 -->
<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));
}
}