RestTemplate Spring 引导中的 NullPointerException MediaType
NullPointerException MediaType in RestTemplate Spring boot
我正在将 spring 引导应用程序从 1.5.6.Release 更新到 2.3.5,但在构建项目时出现异常。
以下是我的错误 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;
}
我收到以下错误:
:
>
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[=14=](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 MediaType.Java
并抛出空指针 Exception.How 我可以正确传递 MediaTypes 吗?我确实在 Eclipse 中看到警告:
The field AbstractJackson2HttpMessageConverter.DEFAULT_CHARSET is
deprecated
任何处理 Null Media 类型的解决方法都值得赞赏。
这是 2.2.0 spring-boot 的问题。他们为默认字符集编码设置了弃用。
尝试用以下两种类型替换。 json 和文本,
new MediaType("application", "json", MappingJackson2HttpMessageConverter. StandardCharsets.UTF_8))
我正在将 spring 引导应用程序从 1.5.6.Release 更新到 2.3.5,但在构建项目时出现异常。 以下是我的错误 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;
}
我收到以下错误: :
> 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[=14=](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 MediaType.Java
并抛出空指针 Exception.How 我可以正确传递 MediaTypes 吗?我确实在 Eclipse 中看到警告:
The field AbstractJackson2HttpMessageConverter.DEFAULT_CHARSET is
deprecated
任何处理 Null Media 类型的解决方法都值得赞赏。
这是 2.2.0 spring-boot 的问题。他们为默认字符集编码设置了弃用。
尝试用以下两种类型替换。 json 和文本,
new MediaType("application", "json", MappingJackson2HttpMessageConverter. StandardCharsets.UTF_8))