gpt4 book ai didi

Spring 启动两个对象映射器 bean

转载 作者:行者123 更新时间:2023-12-04 16:07:22 34 4
gpt4 key购买 nike

我想在我的 Spring boot 项目中创建两个 Hibernate 感知对象映射器 bean。

一个 bean 强制延迟加载,另一个 bean 强制延迟加载设置为 false。我的bean定义如下:

我创建了一个 HibernateAwareObjectMapper类(class)

public class HibernateAwareObjectMapper extends ObjectMapper {
}


@Configuration
public class CrewuiserCorebeans extends WebMvcConfigurerAdapter {

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

@Bean
public ObjectMapper defaultObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
Hibernate5Module hibernate5Module = new Hibernate5Module();
hibernate5Module.configure(Hibernate5Module.Feature.FORCE_LAZY_LOADING, true);
objectMapper.registerModule(hibernate5Module);
return objectMapper;
}

@Bean
public HibernateAwareObjectMapper hibernateAwareObjectMapper() {
HibernateAwareObjectMapper objectMapper = new HibernateAwareObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
Hibernate5Module hibernate5Module = new Hibernate5Module();
hibernate5Module.configure(Hibernate5Module.Feature.FORCE_LAZY_LOADING, false);
objectMapper.registerModule(hibernate5Module);
return objectMapper;
}


@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(new UserContextResolver());
}

}

当我运行应用程序时,出现以下错误:
Parameter 0 of method mappingJackson2HttpMessageConverter in org.springframework.boot.autoconfigure.web.JacksonHttpMessageConvertersConfiguration$MappingJackson2HttpMessageConverterConfiguration required a single bean, but 2 were found:
- defaultObjectMapper: defined by method 'defaultObjectMapper' in class path resource [com/crewuiser/core/configuration/CrewuiserCorebeans.class]
- hibernateAwareObjectMapper: defined by method 'hibernateAwareObjectMapper' in class path resource [com/crewuiser/core/configuration/CrewuiserCorebeans.class]
org.springframework.boot.autoconfigure.web.JacksonHttpMessageConvertersConfiguration在 jar 里,所以我不能添加 @Qualifier注解。

请帮忙,如果我在这里遗漏了什么。我怎样才能让它工作?

最佳答案

我找到了解决办法。

我加了 @Primary到其中一个 bean 并使用 @Qualifier在我的其他类中,非主要 bean 需要 @Autowired .

@Bean
@Primary
public ObjectMapper defaultObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
return objectMapper;
}

在我的服务课上。
@Autowired
public OrganisationService(OrganisationValidator organisationValidator, OrganisationAuthority organisationAuthority,
OrganisationHelper organisationHelper, OrganisationRepository organisationRepository,
@Qualifier("hibernateAwareObjectMapper") ObjectMapper hibernateAwareObjectMapper) {}

关于Spring 启动两个对象映射器 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46548750/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com