使用Spring Data REST时如何更改Jacksons Configuration?
我正在尝试配置Jackson以ISO 8601格式显示JSR 310瞬间。
@Configuration class Jackson { @Bean static ObjectMapper objectMapper() { ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules(); objectMapper.disable( SerializationFeature.WRITE_DATES_AS_TIMESTAMPS ); return objectMapper; } }
然而,这不是一个独特的Bean,实际上我只想禁用这一个设置。 所以我真的不想创建ObjectMapper,就像在其上指定一个设置一样。
java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.EndpointWebMvcHypermediaManagementContextConfiguration$MvcEndpointAdvice': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.fasterxml.jackson.databind.ObjectMapper org.springframework.boot.actuate.autoconfigure.EndpointWebMvcHypermediaManagementContextConfiguration$MvcEndpointAdvice.mapper; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.fasterxml.jackson.databind.ObjectMapper] is defined: expected single matching bean but found 3: objectMapper,halObjectMapper,_halObjectMapper at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:834) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:667) at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:342) at org.springframework.boot.SpringApplication.run(SpringApplication.java:273) at org.springframework.boot.SpringApplication.run(SpringApplication.java:980) at org.springframework.boot.SpringApplication.run(SpringApplication.java:969) at com.xenoterracide.example.Application.main(Application.java:9) ... 5 more Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.fasterxml.jackson.databind.ObjectMapper org.springframework.boot.actuate.autoconfigure.EndpointWebMvcHypermediaManagementContextConfiguration$MvcEndpointAdvice.mapper; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.fasterxml.jackson.databind.ObjectMapper] is defined: expected single matching bean but found 3: objectMapper,halObjectMapper,_halObjectMapper at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:571) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ... 22 more Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.fasterxml.jackson.databind.ObjectMapper] is defined: expected single matching bean but found 3: objectMapper,halObjectMapper,_halObjectMapper at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1079) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:967) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:543) ... 24 more
虽然不是真的需要, pom.xml
是我的pom.xml
4.0.0 com.xenoterracide modern-spring-web-development 1.0-SNAPSHOT 1.8 org.springframework.boot spring-boot-starter-parent 1.3.0.M5 org.springframework.boot spring-boot-starter-web com.fasterxml.jackson.datatype jackson-datatype-jsr310 org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-devtools org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-data-rest com.h2database h2 spring-milestones Spring Milestones http://repo.spring.io/milestone false
以及在Greeting DTO中有一个Instant的我的控制器的输出。
{"content":"Hello, \"me\"","time":1443886979.716000000}
如何在默认对象映射器上重新配置这一个设置?
您可以(并且可能应该)使用RepositoryRestConfigurerAdapter
(在Spring Data Rest 2.4中)或RepositoryRestMvcConfiguration
来公开configureObjectMapper
方法。
@Configuration class RepositoryRestAdapterConfiguration extends RepositoryRestConfigurerAdapter { @Override public void configureJacksonObjectMapper(ObjectMapper objectMapper) { objectMapper.registerModule(new JavaTimeModule()); objectMapper.disable( SerializationFeature.WRITE_DATES_AS_TIMESTAMPS ); } }
实际上,您可以使用application.properties(或application.yml)中的jackson自动配置属性使其更具启动性:
spring.jackson.serialization.write_dates_as_timestamps=false
经过一些实验,我发现如果我用它来注释我的“setter”(setter注入)就会运行。 这比使用字段注入和@PostConstruct
更简单,更简洁。
@Configuration class Jackson { @Autowired void configureObjectMapper( final ObjectMapper objectMapper ) { objectMapper.disable( SerializationFeature.WRITE_DATES_AS_TIMESTAMPS ); } }
我认为一种方法是将ObjectMapper
注入合适的bean类中,然后在@PostConstruct
方法中进行设置,例如:
@Configuration // or @Service or some bean public class SomeClass ... { @Autowired private ObjectMapper objectMapper; @PostConstruct private void configureObjectMapper() { objectMapper.disable( SerializationFeature.WRITE_DATES_AS_TIMESTAMPS ); } }
- 使用ObjectMapper更改字段大小写
- 找不到com.fasterxml.jackson.core.JsonParseException的类文件
- 使用Jackson XmlMapper进行序列化时添加DTD
- 如何使用java中的XPath / JsonPath更改json文件中的值
- 如何告诉jackson在反序列化期间忽略空对象?
- 如何使用Spring RestTemplate表示为JSON的查询参数?
- 如何使用Jackson的本地化小数分隔符反序列化浮点值
- 从spring mvc controller返回一个简单的map结构到ajax
- Spring REST,JSON“无法处理托管/后向引用’defaultReference’”415不支持的媒体类型