与MapStruct一起挥舞时抛出org.springframework.context.annotation.ConflictingBeanDefinitionException

时间:2019-07-19 10:17:11

标签: java spring maven spring-boot swagger

我正在尝试在我的项目中实现swagger2。 (Spring Boot项目) 我已遵循此guide

然后我将招摇摇号纳入了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

ConflictingBeanDefinitionException应该与相同名称和类的现有不兼容bean定义一起抛出。 但是它们不应该存在两个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,并且一切正常。 我不理解如何,如果有人能清除我的想法,我会很高兴

3 个答案:

答案 0 :(得分:0)

我也不知道为什么会这样,但是我为我们的项目找到了一个简单的解决方案:

@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")。我也尝试像您说的那样重命名,但这并不能解决我的问题。

我正在通过Gradle使用MapStruct 1.3.1.Final和Swagger 3.0.0

答案 1 :(得分:0)

通过将LicenseMapper重命名为LicenseServiceMapper来解决。

答案 2 :(得分:0)

当我将映射器从一个包移动到另一个包时,与 swagger 无关,我遇到了同样的错误。我通过运行 mvn clean 修复了它,或者可以通过删除已经生成的 /target 目录来修复它。