创建了两个conversionService bean

时间:2018-03-26 08:08:03

标签: java spring spring-boot converter

我在尝试启动应用程序时遇到错误

Description:

Field conversionService in com.profectus.dashboard.service.impl.DashBoardSettingsServiceimpl required a single bean, but 2 were found:
    - mvcConversionService: defined by method 'mvcConversionService' in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]
    - defaultConversionService: defined by method 'defaultConversionService' in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

为什么要创建2个bean以及如何只保留一个转换器,我只想要只能将实体转换为pojo或pojo转换为实体的spring core转换器。

我因为这个问题而陷入困境,任何领导都会有所帮助。

服务类代码: -

import org.springframework.core.convert.ConversionService;
//other imports

@Service
public class DashBoardSettingsServiceimpl implements DashBoardSettingsService {

    @Autowired
    private DashBoardSettingJpaRepository dashBoardSettingRepo;

    @Autowired
    private ConversionService conversionService;

    @Override
    public DashBoardSettingResponse save(UserInfo userInfo, DashBoardSettingRequest request) {

        //other coded
        DashBoardSettigEntity entity = conversionService.convert(request.getDashBoardSetting(),
                DashBoardSettigEntity.class);


        DashBoardSettigEntity entityRetrieve = dashBoardSettingRepo.save(entity);

        DashBoardSetting setting = conversionService.convert(entityRetrieve, DashBoardSetting.class);

        DashBoardSettingResponse response = new DashBoardSettingResponse();

        response.addDashBoardSetting(setting);
        return response;
    }
}

2 个答案:

答案 0 :(得分:0)

自动装配类型DefaultConversionService而不是ConversionService

答案 1 :(得分:0)

似乎与Spring数据其余部分有自己的ConversionService实例有关。

您能尝试一下吗?

@Autowired @Qualifier("mvcConversionService") ConversionService conversionService;

对我有用。 不要忘记将转换器添加到WebMvcConfigurer实现中。