我正试图在我的项目中实现swagger2。(Spring项目)我遵循了这个指南
然后我在pom.xml中加入了“傲慢”
<spring.boot.version>2.1.4.RELEASE</spring.boot.version>
<org.mapstruct.version>1.3.0.Final</org.mapstruct.version>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
<scope>compile</scope>
</dependency>我创建了一个@Configuration类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}现在,当我启动应用程序时,我会得到以下错误:
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [configuration.SwaggerConfig]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'licenseMapperImpl' for bean class [springfox.documentation.swagger2.mappers.LicenseMapperImpl] conflicts with existing, non-compatible bean definition of same name and class [data.mapper.LicenseMapperImpl]
Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'licenseMapperImpl' for bean class [springfox.documentation.swagger2.mappers.LicenseMapperImpl] conflicts with existing, non-compatible bean definition of same name and class [data.mapper.LicenseMapperImpl]LicenseMapperImpl是由MapStruct生成的,我不知道如何解决这个问题。我认为一个可能的解决方案应该是向compoment但是mapstruct中添加名称,但是我在他的存储库https://github.com/mapstruct/mapstruct/issues/1427上发现了这个bug。
应该使用现有的、不兼容的同名和类的bean定义抛出ConflictingBeanDefinitionException。但是它们不应该存在两个LicenseMapperImpl.class bean,而LicenseMapperImpl.class是一个简单的组件。
这是LicenseMapperImpl.class
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2019-07-19T11:46:08+0200",
comments = "version: 1.3.0.Final, compiler: javac, environment: Java 1.8.0_181 (Oracle Corporation)"
)
@Component
public class LicenseMapperImpl implements LicenseMapper {
@Override
public License toEntity(LicenseDto dto) {
// do something
}
@Override
public LicenseDto toDto(License entity) {
// do something
}
@Override
public List<License> toEntity(List<LicenseDto> dtoList) {
// do something
}
@Override
public List<LicenseDto> toDto(List<License> entityList) {
// do something
}
}编辑
我将LicenseMapper重命名为LicenseServiceMapper,一切都很好。我不知道怎么做,如果有人能理清我的想法,我会很高兴的
发布于 2020-08-26 22:35:32
我也不知道为什么会发生这种情况,但我为我们的项目找到了一个简单的解决方案:
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE, implementationName = "test")
public interface ModelMapper {
ModelMapper INSTANCE = Mappers.getMapper(ModelMapper.class);
@Mapping(target = "lastUpdated", source = "time")
Model modelDtoToEntity(ModelDto modelDto);
@Mapping(target = "time", source = "lastUpdated")
ModelDto entityToModelDto(Model model);
}我刚刚添加了属性implementationName,并给它一个自定义名称(这里是implementationName = "test")。我也试着重新命名,就像你说的,但这并没有为我解决它。
我使用的是MapStruct 1.3.1.Final和Swagger3.0.0与Gradle
发布于 2020-08-27 15:56:12
通过将LicenseMapper重命名为LicenseServiceMapper来解析。
发布于 2021-07-27 06:47:44
当我将映射器从一个包移到另一个包时,与swagger无关,我得到了相同的错误。我通过运行mvn clean修复了它,也可以通过删除已经生成的/target dir来修复它。
https://stackoverflow.com/questions/57110287
复制相似问题