文章目录
1.问题分析2.解决方案
2.1 方案一2.2 方案二2.3. 方案三2.4 其他
1.问题分析
实现ResponseBodyAdvice接口,同时使用RestController,统一后端返回给前端的数据的格式。
@RestControllerAdvice(annotations = RestResponse.class)
public class ResponseAdvice implements ResponseBodyAdvice
debug:
可以知道:访问swagger-ui.html 的时候,会通过ApiResourceController拿到需要的东西;这里由于对结果做了一层封装;swagger的前端页面无法正常解析;因此报错。
2.解决方案
2.1 方案一
修改supports方法,不对swagger的方法进行处理。
@RestControllerAdvice
public class ResponseAdvice implements ResponseBodyAdvice
2.2 方案二
编写一个组合注解,@RestResponse,替代RestController
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD})
@documented
@RestController
public @interface RestResponse {
}
在ResponseBodyAdvice实现类上设置被@RestResponse标注对的controller需要进行处理
@RestControllerAdvice(annotations = RestResponse.class)
public class ResponseAdvice implements ResponseBodyAdvice
2.3. 方案三
使用basePackages来限制要扫描的包。
@RestControllerAdvice(basePackages = "com.zhx.controller")
public class ResponseAdvice implements ResponseBodyAdvice
2.4 其他
@RestControllerAdvice 中还有其他的方法能得到一样的效果