当前位置:网站首页>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");
}
边栏推荐
- [sharing game modeling model making skills] how ZBrush adjusts the brush size
- Modeling just learning is very confused. How to learn the next generation role modeling process?
- Opencv (13): brief introduction to cv2.findcontours, cv:: findcontours and description of cv2.findcontours function in various versions of opencv
- 有人是靠自学建模找到工作的吗?千万别让这些思维害了你
- 【2018】【论文笔记】石墨烯场效应管及【1】——GFETs的种类和原理,GFETs特性,GFETs在太赫兹中的应用和原理
- Is it suitable for learning 3D modeling? You can't lose one of these five points
- [toggle 30 days of ML] Diabetes genetic risk detection challenge (2)
- Foundation of class
- Completion report of communication software development and Application
- Use three JS realize the 'ice cream' earth, and let the earth cool for a summer
猜你喜欢

Great God "magic change" airpods, equipped with usb-c interface, 3D printing shell makes maintenance easier
![Log framework [detailed learning]](/img/2f/2aba5d48e8a544eae0df763d458e84.png)
Log framework [detailed learning]

Opencv (13): brief introduction to cv2.findcontours, cv:: findcontours and description of cv2.findcontours function in various versions of opencv
![[whole process of game modeling model production] 3ds Max and ZBrush produce radio receivers](/img/c9/302a52d2c9f6fc3b5971e9a0ea55e6.png)
[whole process of game modeling model production] 3ds Max and ZBrush produce radio receivers

Analysis on the implementation of Flink exactly once delivery

Rhcsa note 4

Interviewer: what do you think is your biggest weakness?

SQLZOO——SELECT from Nobel Tutorial

Rhcsa notes 7
![MySQL [knowing and mastering one article is enough]](/img/5a/ed8f1d3fac63d329a28ebf6d4974f5.png)
MySQL [knowing and mastering one article is enough]
随机推荐
JS convert pseudo array to array
【游戏建模模型制作全流程】用ZBrush制作游戏士兵角色
My creation anniversary
Rhcsa notes 7
Where should we start to learn modeling from zero foundation? How to learn game modeling well?
【2020】【论文笔记】基于Rydberg原子的——
【3D建模制作技巧分享】Zbrush如何将图片转浮雕模型
398. Random number index hash table method
零基础要学建模该从何开始?如何才能学好游戏建模?
MySql【从了解到掌握 一篇就够】
[sharing game modeling model making skills] how ZBrush adjusts the brush size
Deepstream learning notes (II): description of GStreamer and deepstream-test1
80 + guests took the stage, users from more than 10 countries attended the meeting, and 70000 + viewers watched the end of "Gwei 2022 Singapore"
Paddlenlp之UIE分类模型【以情感倾向分析新闻分类为例】含智能标注方案)
【攻防世界WEB】难度三星9分入门题(终):fakebook、favorite_number
Flutter 运行模式
Is 3D modeling promising? Is employment guaranteed with high salary or is it more profitable to take orders in sideline industry
Stack / heap / queue question brushing (medium)
ResponseBodyAdvice接口使用导致的报错及解决
元胞数组处理