nullpointerexception mediatype

pn9klfpd  于 2021-07-16  发布在  Java
关注(0)|答案(1)|浏览(393)

我正在将SpringBoot应用程序从1.5.6.release更新到2.3.5,但在生成项目时出现异常。下面是我的错误类:

@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;
  }

出现以下错误:

> 
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)

为了保留重要的行,我省略了一些堆栈之间的跟踪。在1.5.6版本中运行良好。主要原因是最后一行,它与
mimetype.java媒体类型.java
并引发空指针异常。如何正确传递mediatypes?我确实在eclipse中看到一个警告:

The field AbstractJackson2HttpMessageConverter.DEFAULT_CHARSET is 
 deprecated

任何处理空媒体类型的解决方法都是值得赞赏的。

dxxyhpgq

dxxyhpgq1#

这是2.2.0 spring boot的问题。他们为默认字符集编码设置了弃用。
尝试用以下两种类型替换。json和文本,

new MediaType("application", "json", MappingJackson2HttpMessageConverter. StandardCharsets.UTF_8))

相关问题