在 Spring 中配置 ObjectMapper
我的目标是配置objectMapper
,使其仅序列化用@JsonProperty
注释的元素。
为此,我遵循了说明,其中说明了如何配置对象映射器。
我按照此处所述添加了自定义对象映射器。
但是,当类 NumbersOfNewEvents
被序列化时,它仍然包含 json 中的所有属性。
为什么会发生这种情况,我怎样才能得到想要的结果?
杰克逊1.8.0 spring 3.0.5
CustomObjectMapper
public class CompanyObjectMapper extends ObjectMapper {
public CompanyObjectMapper() {
super();
setVisibilityChecker(getSerializationConfig()
.getDefaultVisibilityChecker()
.withCreatorVisibility(JsonAutoDetect.Visibility.NONE)
.withFieldVisibility(JsonAutoDetect.Visibility.NONE)
.withGetterVisibility(JsonAutoDetect.Visibility.NONE)
.withIsGetterVisibility(JsonAutoDetect.Visibility.NONE)
.withSetterVisibility(JsonAutoDetect.Visibility.DEFAULT));
}
}
servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="de.Company.backend.web" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper" ref="jacksonObjectMapper" />
</bean>
</list>
</property>
</bean>
<bean id="jacksonObjectMapper" class="de.Company.backend.web.CompanyObjectMapper" />
</beans>
NumbersOfNewEvents
public class NumbersOfNewEvents implements StatusAttribute {
public Integer newAccepts;
public Integer openRequests;
public NumbersOfNewEvents() {
super();
}
}
My goal is to configure the objectMapper
in the way that it only serialises element which are annotated with @JsonProperty
.
In order to do so I followed this explanation which says how to configurate the objectmapper.
I included the custom objectmapper as described here.
However, when the class NumbersOfNewEvents
is serialized it still contains all attributes in the json.
Why is this happening, and how can I get the desired result?
Jackson 1.8.0
spring 3.0.5
CustomObjectMapper
public class CompanyObjectMapper extends ObjectMapper {
public CompanyObjectMapper() {
super();
setVisibilityChecker(getSerializationConfig()
.getDefaultVisibilityChecker()
.withCreatorVisibility(JsonAutoDetect.Visibility.NONE)
.withFieldVisibility(JsonAutoDetect.Visibility.NONE)
.withGetterVisibility(JsonAutoDetect.Visibility.NONE)
.withIsGetterVisibility(JsonAutoDetect.Visibility.NONE)
.withSetterVisibility(JsonAutoDetect.Visibility.DEFAULT));
}
}
servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="de.Company.backend.web" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper" ref="jacksonObjectMapper" />
</bean>
</list>
</property>
</bean>
<bean id="jacksonObjectMapper" class="de.Company.backend.web.CompanyObjectMapper" />
</beans>
NumbersOfNewEvents
public class NumbersOfNewEvents implements StatusAttribute {
public Integer newAccepts;
public Integer openRequests;
public NumbersOfNewEvents() {
super();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
使用 Spring Boot (1.2.4) 和 Jackson (2.4.6),以下基于注释的配置对我有用。
Using Spring Boot (1.2.4) and Jackson (2.4.6) the following annotation based configuration worked for me.
这可能是因为我使用的是 Spring 3.1(而不是您指定的问题所指定的 Spring 3.0.5),但 Steve Eastwood 的答案对我不起作用。此解决方案适用于 Spring 3.1:
在您的 spring xml 上下文中:
It may be because I'm using Spring 3.1 (instead of Spring 3.0.5 as your question specified), but Steve Eastwood's answer didn't work for me. This solution works for Spring 3.1:
In your spring xml context:
很长一段时间都有
org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean
。从 Spring Boot 1.2 版本开始,有用于 Java 配置的 org.springframework.http.converter.json.Jackson2ObjectMapperBuilder 。在 String Boot 中,配置可以非常简单:
在
classpath:application.properties
中或@Configuration
类中的一些 Java 代码:请参阅:
There is
org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean
for a long time. Starting from 1.2 release of Spring Boot there isorg.springframework.http.converter.json.Jackson2ObjectMapperBuilder
for Java Config.In String Boot configuration can be as simple as:
in
classpath:application.properties
or some Java code in@Configuration
class:See:
我已将其与 Jackson 2.x 和 Spring 3.1.2+ 一起使用
servlet-context.xml:
请注意,根元素是 < code>,因此您可能需要删除
beans
并向其中一些元素添加mvc
,具体取决于您的设置。au.edu.unimelb.atcom.transfer.json.mappers.JSONMapper.java:
DateSerializer.java:
DateDeserializer.java:
I've used this with Jackson 2.x and Spring 3.1.2+
servlet-context.xml:
Note that the root element is
<beans:beans>
, so you may need to removebeans
and addmvc
to some of these elements depending on your setup.au.edu.unimelb.atcom.transfer.json.mappers.JSONMapper.java:
DateSerializer.java:
DateDeserializer.java:
解决方案 1
第一个(经过测试的)工作解决方案非常有用,尤其是在使用
@EnableWebMvc
时:解决方案 2
当然,下面的常见方法也适用(也可以与
@EnableWebMvc
):为什么 @EnableWebMvc 的使用是一个问题?
@EnableWebMvc
正在使用DelegatingWebMvcConfiguration
扩展了WebMvcConfigurationSupport
,它执行以下操作:这意味着无法注入您自己的
ObjectMapper
来准备将其用于使用@EnableWebMvc时创建默认的MappingJackson2HttpMessageConverter。SOLUTION 1
First (tested) working solution useful especially when using
@EnableWebMvc
:SOLUTION 2
Of course the common approach below works too (also working with
@EnableWebMvc
):Why @EnableWebMvc usage is a problem?
@EnableWebMvc
is usingDelegatingWebMvcConfiguration
which extendsWebMvcConfigurationSupport
which does this:which means that there's no way of injecting your own
ObjectMapper
with the purpose of preparing it to be used for creating the defaultMappingJackson2HttpMessageConverter
when using@EnableWebMvc
.我现在找到了基于 https://github.com/FasterXML/jackson-module-hibernate< 的解决方案/a>
我扩展了对象映射器并在继承的构造函数中添加了属性。
然后新的对象映射器被注册为一个bean。
I found the solution now based on https://github.com/FasterXML/jackson-module-hibernate
I extended the object mapper and added the attributes in the inherited constructor.
Then the new object mapper is registered as a bean.
如果您想添加自定义 ObjectMapper 来注册自定义序列化器,请尝试我的答案。
就我而言(Spring 3.2.4 和 Jackson 2.3.1),自定义序列化器的 XML 配置:
以无法解释的方式被某些东西覆盖回默认值。
这对我有用:
CustomObject.java
CustomObjectSerializer.java
我的解决方案中不需要 XML 配置 (
(...)
) 。If you want to add custom ObjectMapper for registering custom serializers, try my answer.
In my case (Spring 3.2.4 and Jackson 2.3.1), XML configuration for custom serializer:
was in unexplained way overwritten back to default by something.
This worked for me:
CustomObject.java
CustomObjectSerializer.java
No XML configuration (
<mvc:message-converters>(...)</mvc:message-converters>
) is needed in my solution.我正在使用 Spring 4.1.6 和 Jackson FasterXML 2.1.4。
这适用于我的 applicationContext.xml 配置
I am using Spring 4.1.6 and Jackson FasterXML 2.1.4.
this works at my applicationContext.xml configration
要在普通的 spring-web 中配置消息转换器,在本例中要启用 Java 8 JSR-310 JavaTimeModule,您首先需要在
@Configuration
类中实现WebMvcConfigurer
并然后覆盖configureMessageConverters
方法:像这样,您可以在基于 Java 的 Spring 配置中注册任何自定义的
ObjectMapper
。To configure a message converter in plain spring-web, in this case to enable the Java 8 JSR-310 JavaTimeModule, you first need to implement
WebMvcConfigurer
in your@Configuration
class and then override theconfigureMessageConverters
method:Like this you can register any custom defined
ObjectMapper
in a Java-based Spring configuration.在 Spring Boot
2.2.x
中,您需要像这样配置它:Kotlin:
In Spring Boot
2.2.x
you need to configure it like this:Kotlin:
在 Spring 4 以上,如果您只想配置
ObjectMapper
,则无需配置MappingJacksonHttpMessageConverter
。(配置
MappingJacksonHttpMessageConverter
将导致你失去其他MessageConverter)你只需要做:
并在你的Spring配置中,创建这个bean:
Above Spring 4, there is no need to configure
MappingJacksonHttpMessageConverter
if you only intend to configureObjectMapper
.(configure
MappingJacksonHttpMessageConverter
will cause you to lose other MessageConverter)You just need to do:
And in your Spring configuration, create this bean:
我正在使用 Spring 3.2.4 和 Jackson FasterXML 2.1.1。
我创建了一个自定义 JacksonObjectMapper,它使用映射对象的每个属性的显式注释:
然后在上下文配置 (servlet-context.xml) 中实例化它:
这很好用!
I am using Spring 3.2.4 and Jackson FasterXML 2.1.1.
I have created a custom JacksonObjectMapper that works with explicit annotations for each attribute of the Objects mapped:
Then this is instantiated in the context-configuration (servlet-context.xml):
This works fine!
如果您使用 Spring WebFlux,
@EnableWebFlux
会覆盖任何自定义设置,例如spring.jackson.serialization.write-dates-as-timestamps=false
,因此您需要覆盖 WebFlux 的ObjectMapper
实例根据WebFlux 文档。If you are using Spring WebFlux,
@EnableWebFlux
overrides any custom settings likespring.jackson.serialization.write-dates-as-timestamps=false
, so you need to override WebFlux'sObjectMapper
instance according to WebFlux doc.