I am updating spring boot app from 1.5.6.Release to 2.3.5 but getting exception while building project. Following is my error class:
@Configuration
@EnableBatchProcessing
@Import({BatchScheduler.class})
public class MyLogBatchConfiguration {
private static final Logger log = LoggerFactory.getLogger(MyLogBatchConfiguration.class);
@Autowired private SimpleJobLauncher jobLauncher;
@Autowired
@Qualifier(value = "dataSource1")
public DataSource mainDataSource;
@Autowired
@Qualifier(value = "dataSource2")
public DataSource tempDataSource;
@Autowired public StepBuilderFactory stepBuilderFactory;
@Autowired public JobBuilderFactory jobBuilderFactory;
@Autowired private RestTemplate myRestTemplate;
@Qualifier(value = "createLocalSessionFactory")
@Autowired
private SessionFactory createLocalSessionFactory;
@Qualifier(value = "createSessionFactory")
@Autowired
private SessionFactory createSessionFactory;
@Bean
RestTemplate restTemplate(RestTemplateBuilder builder) {
RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
for (HttpMessageConverter<?> converter : converters) {
if (converter instanceof MappingJackson2HttpMessageConverter) {
MappingJackson2HttpMessageConverter jsonConverter =
(MappingJackson2HttpMessageConverter) converter;
jsonConverter.setObjectMapper(new ObjectMapper());
jsonConverter.setSupportedMediaTypes(
ImmutableList.of(
new MediaType(
"application", "json", MappingJackson2HttpMessageConverter.DEFAULT_CHARSET),
new MediaType(
"text", "javascript", MappingJackson2HttpMessageConverter.DEFAULT_CHARSET)));
}
}
return restTemplate;
}
@Bean
public LocalSessionFactoryBean createLocalSessionFactory(
@Qualifier("dataSource1") DataSource dataSource) {
LocalSessionFactoryBean factory = new LocalSessionFactoryBean();
factory.setDataSource(dataSource);
factory.setPackagesToScan(
new String[] {
"X",
"Y",
"Z"
});
factory.setHibernateProperties(hibernateProperties());
return factory;
}
@Bean
public LocalSessionFactoryBean createSessionFactory(
@Qualifier("dataSource2") DataSource dataSource) {
LocalSessionFactoryBean factory = new LocalSessionFactoryBean();
factory.setDataSource(dataSource);
factory.setPackagesToScan(
new String[] {
"X",
"Y",
"Z"
});
factory.setHibernateProperties(hibernateProperties());
return factory;
}
I am getting following error : :
> org.springframework.beans.factory.UnsatisfiedDependencyException: > Error creating bean with name 'MyLogBatchConfiguration': Unsatisfied > dependency expressed through field 'myRestTemplate'; nested exception > is org.springframework.beans.factory.UnsatisfiedDependencyException: > Error creating bean with name 'batchConfiguration': Unsatisfied > dependency expressed through field 'myRestTemplate'; nested exception > is org.springframework.beans.factory.BeanCreationException: Error > creating bean with name 'restTemplate' defined in class path resource > [com//configuration/MyLogBatchConfiguration.class]: Bean instantiation > via factory method failed; nested exception is > org.springframework.beans.BeanInstantiationException: Failed to > instantiate [org.springframework.web.client.RestTemplate]: Circular > reference involving containing bean 'MyLogBatchConfiguration' - > consider declaring the factory method as static for independence from > its containing instance. Factory method 'restTemplate' threw > exception; nested exception is java.lang.NullPointerException at > org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) > at > org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) > at > org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) > at > org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1420) > at > org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) > at > org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) > at > org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) > ] at > sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) > at java.lang.reflect.Method.invoke(Method.java:498) at > org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) > at org.springframework.boot.loader.Launcher.launch(Launcher.java:107) > at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) > at > org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88) > Caused by: > org.springframework.beans.factory.UnsatisfiedDependencyException: > Error creating bean with name 'batchConfiguration': Unsatisfied > dependency expressed through field 'myRestTemplate'; nested exception > is org.springframework.beans.factory.BeanCreationException: Error > creating bean with name 'restTemplate' defined in class path resource > [com//configuration/MyLogBatchConfiguration.class]: Bean instantiation > via factory method failed; nested exception is > org.springframework.beans.BeanInstantiationException: Failed to > instantiate [org.springframework.web.client.RestTemplate]: Circular > reference involving containing bean 'MyLogBatchConfiguration' - > consider declaring the factory method as static for independence from > its containing instance. Factory method 'restTemplate' threw > exception; nested exception is java.lang.NullPointerException at > org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) > at > org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) > at > org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) > at > org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1420) > at > org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) > at > org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) > at > org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) > at > org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1307) > at > org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1227) > at > org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) > ... 28 common frames omitted Caused by: > org.springframework.beans.factory.BeanCreationException: Error > creating bean with name 'restTemplate' defined in class path resource > [com/*/configuration/MyLogBatchConfiguration.class]: Bean > instantiation via factory method failed; nested exception is > org.springframework.beans.BeanInstantiationException: Failed to > instantiate [org.springframework.web.client.RestTemplate]: Circular > reference involving containing bean 'MyLogBatchConfiguration' - > consider declaring the factory method as static for independence from > its containing instance. Factory method 'restTemplate' threw > exception; nested exception is java.lang.NullPointerException at > org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:655) > at > org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:635) > at > org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1336) > at > org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1176) > at > org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556) > at > org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) > at > org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1307) > at > org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1227) > at > org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) > ... 50 common frames omitted Caused by: > org.springframework.beans.BeanInstantiationException: Failed to > instantiate [org.springframework.web.client.RestTemplate]: Circular > reference involving containing bean 'MyLogBatchConfiguration' - > consider declaring the factory method as static for independence from > its containing instance. Factory method 'restTemplate' threw > exception; nested exception is java.lang.NullPointerException at > org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) > at > org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:650) > ... 63 common frames omitted Caused by: > java.lang.NullPointerException: null at > org.springframework.util.MimeType.(MimeType.java:143) at > org.springframework.http.MediaType.(MediaType.java:433)
I have omitted some in-between stack trace to keep important lines. It was working fine in 1.5.6 Release version. The main reason is the last line, where it works with
MimeType.java MediaType.Java
and throws Null Pointer Exception.How can I pass MediaTypes correctly ? I do see a warning in eclipse as :
The field AbstractJackson2HttpMessageConverter.DEFAULT_CHARSET is
deprecated
Any workaround of handling Null Media type is appreciated.
This is problem from 2.2.0 spring-boot. They set deprecation for default charset encoding.
Try replacing with followingFor both type. json and text,
new MediaType("application", "json", MappingJackson2HttpMessageConverter. StandardCharsets.UTF_8))