0

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.

12
  • Can you mention the complete stack trace please Commented Mar 22, 2021 at 13:28
  • i have updated.Plz let me know if u need more info. I earlier thought it is cyclic dependency issue as mentioned in some stack trace but i tried removing that ,still the error persists, then i saw at bottom of logs, i got some MediaType Null pointer Exception and thats why restTemplate Bean is not getting created. Commented Mar 22, 2021 at 13:35
  • 1
    Have look at this post and see if this helps and let me know stackoverflow.com/questions/58525387/… Commented Mar 22, 2021 at 13:37
  • I have updated the link in above comment Commented Mar 22, 2021 at 13:38
  • I replaced MappingJackson2HttpMessageConverter.DEFAULT_CHARSET statement with StandardCharsets.UTF_8, but still it give same error.The link was suppose to do this only .right? Commented Mar 22, 2021 at 15:31

1 Answer 1

1

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))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.