[英]How can I change Jacksons Configuration when using Spring Data REST?
I'm trying to configure Jackson to show JSR 310 instants in ISO 8601 format. 我正在尝试配置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;
}
}
However this is not a unique Bean, and realistically I only would like to disable this one setting. 但是,这不是唯一的Bean,实际上,我只想禁用此设置。 So I'm not really wanting to create ObjectMapper, so much as specify a single setting on it.
因此,我并不是真的想要创建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
Though not really required, here's my pom.xml
虽然不是必需的,
pom.xml
是我的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xenoterracide</groupId>
<artifactId>modern-spring-web-development</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.M5</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>http://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
and the output of my controller that has an Instant in a Greeting DTO. 以及在Greeting DTO中具有Instant的控制器的输出。
{"content":"Hello, \"me\"","time":1443886979.716000000}
How can I reconfigure just this one setting on the default object mapper? 如何仅在默认对象映射器上重新配置这一设置?
You can (and probably should) you RepositoryRestConfigurerAdapter
(in Spring Data Rest 2.4) or RepositoryRestMvcConfiguration
which expose the configureObjectMapper
method. 您可以(可能应该)使用
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)中的杰克逊自动配置属性,就可以使它更具引导性:
spring.jackson.serialization.write_dates_as_timestamps=false
After some experimentation, I found that if I annotate my "setter" (setter injection) with it will get run. 经过一些试验,我发现如果我用它的“ setter”(setter注入)进行注释,它将可以运行。 This is a little simpler, and cleaner than using field injection and
@PostConstruct
. 这比使用字段注入和
@PostConstruct
更简单,更干净。
@Configuration
class Jackson {
@Autowired
void configureObjectMapper( final ObjectMapper objectMapper ) {
objectMapper.disable( SerializationFeature.WRITE_DATES_AS_TIMESTAMPS );
}
}
I think one way could be to inject the ObjectMapper
in a suitable bean class and then do the setting in a @PostConstruct
method, eg: 我认为一种方法可能是将
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 );
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.