Spring Boot无法识别新增ModelMapper依赖 错误导入Swagger同名类
问题根源
项目中同时引入了Swagger和ModelMapper依赖,二者都存在类名同为ModelMapper的类,IDE自动导入时优先匹配了Swagger包下的类,才会出现依赖不识别的问题。
解决方案
1. 修正配置类的导入逻辑
删除配置类中错误的Swagger ModelMapper导入语句,替换为ModelMapper官方工具的正确导入,修正后的代码如下:
import org.modelmapper.ModelMapper; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class Config { @Bean public ModelMapper modelMapper(){ return new ModelMapper(); } }
如果后续仍出现类名冲突问题,可以直接使用全类名声明Bean,避免导入歧义:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class Config { @Bean public org.modelmapper.ModelMapper modelMapper(){ return new org.modelmapper.ModelMapper(); } }
2. 重新加载Maven依赖
如果修改导入后IDE仍识别不到ModelMapper依赖,可执行如下操作:
- 点击IDE Maven面板的刷新按钮,重新加载所有依赖
- 执行
mvn clean install命令完成项目构建,确认依赖已正常下载
如果检查外部库发现没有org.modelmapper相关依赖,可调整Maven镜像为国内源后重新拉取依赖即可。
内容的提问来源于stack exchange,提问作者Daniel R
相关产品推荐
相关产品推荐

