原因:
gateway引入了自定义的common包.
里边定义了统一返回值处理方法
@RestControllerAdvice(basePackages = {"com.*"}) // 这里要加上需要扫描的包
public class ResponseControllerAdvice implements ResponseBodyAdvice<Object> {
}
因为gateway不能使用web-starter所以报ResponseBodyAdvice找不到这个错误
修改结果
- 方法1
这种方法的缺点时有多少个这种类型的类,就需要修改多少地方
@RestControllerAdvice(basePackages = {"com.*"}) // 这里要加上需要扫描的包
@ConditionalOnClass(ResponseBodyAdvice.class)
public class ResponseControllerAdvice implements ResponseBodyAdvice<Object> {
}
- 方法2
这种方式解决后不能自动扫描到
common中的代码,如果需要个别路径下的代码,可以在启动类上添加注解@ComponentScan
将gateway的启动类放到与其他代码不一样的路径下边
如:
gateway启动类路径为com.a.GatewayApplication.java
common包的路径为com.b.*.ResponseControllerAdvice .java
本文探讨了在Spring Boot Gateway中引入自定义包处理统一返回值时,如何处理由于不使用web-starter而引发的`ResponseBodyAdvice`找不到的问题,介绍了两种解决方案:1) 使用`@ConditionalOnClass`注解指定依赖类;2) 调整启动类路径并手动扫描特定包。

2126

被折叠的 条评论
为什么被折叠?



