当前位置:网站首页>Error reporting caused by the use of responsebodyadvice interface and its solution
Error reporting caused by the use of responsebodyadvice interface and its solution
2022-07-23 18:49:00 【Integer_ Double】
One 、ResponseBodyAdvice Use
ResponseBodyAdvice Used before the return value is written to the response , take body Re encapsulation of content , Go straight to the code
1.1 CommonResponseDataAdvice
@Component
public class CommonResponseDataAdvice implements ResponseBodyAdvice<Object> {
private static final String V_3_API_DOCS = "/v3/api-docs";
private static final String SWAGGER_RESOURCES = "/swagger-resources";
private static final String ADMIN_ACTUATOR = "/actuator";
public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
if (methodParameter.getAnnotatedElement().isAnnotationPresent(IgnoreResponseAdvice.class)) {
return false;
} else {
return !methodParameter.getMethod().isAnnotationPresent(IgnoreResponseAdvice.class);
}
}
@Nullable
public Object beforeBodyWrite(@Nullable Object o, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
// Avoid being right swagger Of api The interface return value is encapsulated
if (((ServletServerHttpRequest) serverHttpRequest).getServletRequest().getRequestURI().equals(V_3_API_DOCS)) {
return o;
}
if (((ServletServerHttpRequest) serverHttpRequest).getServletRequest().getRequestURI().contains(SWAGGER_RESOURCES)) {
return o;
}
// Avoid being right actuator The monitoring interface of
if (((ServletServerHttpRequest) serverHttpRequest).getServletRequest().getRequestURI().contains(ADMIN_ACTUATOR)) {
return o;
}
if (o instanceof Result) {
return o;
}
return new Result<>(0, "",o);
}
}
Key methods beforeBodyWrite, stay SpringMVC The main process has mentioned 
It will be called , Thus, the return value is encapsulated into a custom Result Inside , It is convenient for the front end to uniformly process the status code .
Two 、 Error reporting and resolution
2.1 Integrate swagger Report errors
In integration swagger after , Always report an error , Interface documents can't come out , The following error message appears :
Unable to render this definition
The provided definition does not specify a valid version field.
Please indicate a valid Swagger or OpenAPI version field. Supported version fields are swagger: “2.0” and those that match openapi: 3.0.n (for example, openapi: 3.0.0).
Baidu has been useless for a long time , Last check /v3/api-docs After the interface finds that the return value is encapsulated ,swagger The front end of cannot parse the custom structure , Result in an error .
Solution : Determine the specific interface path without encapsulation
2.2 Interface directly returns String Type error
Define an interface
@GetMapping("/getUserName")
@ApiOperation(" according to ID Get users ")
public String getUserName() {
return "username";
}
Use Swagger Send a request 
Wrong report , be supposed to Result Cannot be forced to String, There is no way but to find a solution from the source code , according to SpringMVC Main process , The key place is still here 
Spring Provides 10 A message converter ,String stay Json In front of . call beforeBodyWrite After method 
First match to the converter StringHttpMessageConverter, And will be body Encapsulated into Result type 
StringHttpMessageConverter The generics of are String, Get into write After method 
t The type is Result, A subclass of addDefaultHeaders What the method needs is String
Cause type strong conversion exception
Solution : Interface return String Type , Use in advance Result encapsulation . Can't be in beforeBodyWrite Judgment is StringHttpMessageConverter No encapsulation , because StringHttpMessageConverter After the conversion, return to ,Json The converter will not be used .
@GetMapping("/getUserName")
@ApiOperation(" according to ID Get users ")
public Result<String> getUserName() {
return new Result<>("username");
}
边栏推荐
- MySQL performance tuning
- LeetCode 剑指 Offer II 115.重建序列:图解 - 拓扑排序
- Navigation component of jetpack compose uses
- Boss online replay: the mistake I made when training Dall · e
- PCL:多直线拟合(RANSAC)
- 【攻防世界WEB】难度三星9分入门题(终):fakebook、favorite_number
- Where should we start to learn modeling from zero foundation? How to learn game modeling well?
- SQLZOO——SELECT from Nobel Tutorial
- Three things programmers want to do most | comics
- 【2020】【论文笔记】基于二维光子晶体的光控分光比可调Y——
猜你喜欢
![Multithreading [comprehensive study of graphics and text]](/img/70/8a1227b2159349cf25a85dff8f9d1c.png)
Multithreading [comprehensive study of graphics and text]

知乎二面:请问Redis 如何实现库存扣减操作和防止被超卖?

建模刚学习很迷茫,次世代角色建模流程具体该怎么学习?

1259. Disjoint handshake dynamic programming

ROS (27): the simple use of rosparam and the unsuccessful transfer of parameters through launch and its solution

How to become a modeler? Which is more popular, industrial modeling or game modeling?

How does Apache, the world's largest open source foundation, work?

SQLZOO——BBC QUIZ

ros(27):rosparam简单使用与一种通过launch传递参数不成功与解决

OSI模型第一层:物理层,基石般的存在!
随机推荐
Log framework [detailed learning]
PCL:多直线拟合(RANSAC)
LeetCode 0131. 分割回文串
PCL:多直線擬合(RANSAC)
jumpserver管理员账号被锁定
Google正在改进所有产品中的肤色表现 践行“图像公平”理念
一文详解:TMP1750芯片 三通道线性LED驱动器
【2020】【论文笔记】基于二维光子晶体的光控分光比可调Y——
Tampermonkey究竟有什么用?
Analysis on the implementation of Flink exactly once delivery
建模刚学习很迷茫,次世代角色建模流程具体该怎么学习?
The original path is not original [if there is infringement, please contact the original blogger to delete]
DB9 serial port and RJ45 serial port
Redis [2022 latest interview question]
【2020】【论文笔记】相变材料与超表面——
[whole process of game modeling model production] 3ds Max and ZBrush produce radio receivers
学次世代建模是场景好还是角色好?选对职业薪资多一半
MySQL classic exercises and answers, 50 common SQL sentence exercises
JUC concurrent programming [detailed explanation and demonstration]
Three things programmers want to do most | comics