在Spring中configurationObjectMapper

我的目标是configurationobjectMapper的方式,它只是串行化的@JsonProperty注释的@JsonProperty

为了做到这一点,我遵循这个说明如何configurationobjectmapper。

我包含了这里描述的自定义对象映射器。

但是,当NumbersOfNewEvents类被序列化时,它仍然包含json中的所有属性。

有人有提示吗? 提前致谢

jackson1.8.0spring3.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(); } } 

使用Spring Boot(1.2.4)和Jackson(2.4.6),以下基于注解的configuration对我有用。

 @Configuration public class JacksonConfiguration { @Bean public ObjectMapper objectMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true); return mapper; } } 

这可能是因为我使用的是Spring 3.1(而不是Spring 3.0.5,因为你的问题是指定的),但是Steve Eastwood的回答对我并不适用。 这个解决scheme适用于Spring 3.1:

在你的spring xml上下文中:

 <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"/> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="objectMapper" ref="jacksonObjectMapper" /> </bean> </mvc:message-converters> </mvc:annotation-driven> <bean id="jacksonObjectMapper" class="de.Company.backend.web.CompanyObjectMapper" /> 

我已经使用了Jackson 2.x和Spring 3.1.2+

servlet的context.xml中:

请注意,根元素是<beans:beans> ,因此您可能需要删除beans并根据您的设置将mvc添加到其中一些元素。

  <annotation-driven> <message-converters> <beans:bean class="org.springframework.http.converter.StringHttpMessageConverter" /> <beans:bean class="org.springframework.http.converter.ResourceHttpMessageConverter" /> <beans:bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <beans:property name="objectMapper" ref="jacksonObjectMapper" /> </beans:bean> </message-converters> </annotation-driven> <beans:bean id="jacksonObjectMapper" class="au.edu.unimelb.atcom.transfer.json.mappers.JSONMapper" /> 

au.edu.unimelb.atcom.transfer.json.mappers.JSONMapper.java:

 public class JSONMapper extends ObjectMapper { public JSONMapper() { SimpleModule module = new SimpleModule("JSONModule", new Version(2, 0, 0, null, null, null)); module.addSerializer(Date.class, new DateSerializer()); module.addDeserializer(Date.class, new DateDeserializer()); // Add more here ... registerModule(module); } } 

DateSerializer.java:

 public class DateSerializer extends StdSerializer<Date> { public DateSerializer() { super(Date.class); } @Override public void serialize(Date date, JsonGenerator json, SerializerProvider provider) throws IOException, JsonGenerationException { // The client side will handle presentation, we just want it accurate DateFormat df = StdDateFormat.getBlueprintISO8601Format(); String out = df.format(date); json.writeString(out); } } 

DateDeserializer.java:

 public class DateDeserializer extends StdDeserializer<Date> { public DateDeserializer() { super(Date.class); } @Override public Date deserialize(JsonParser json, DeserializationContext context) throws IOException, JsonProcessingException { try { DateFormat df = StdDateFormat.getBlueprintISO8601Format(); return df.parse(json.getText()); } catch (ParseException e) { return null; } } } 

我现在find了基于https://github.com/FasterXML/jackson-module-hibernate的解决scheme

我扩展了对象映射器,并在inheritance的构造函数中添加了属性。

然后,新的对象映射器被注册为一个bean。

 <!-- https://github.com/FasterXML/jackson-module-hibernate --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <array> <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="objectMapper"> <bean class="de.company.backend.spring.PtxObjectMapper"/> </property> </bean> </array> </property> </bean> 

如果你想添加自定义的ObjectMapper来注册自定义序列化器,试试我的答案。

在我的情况下(Spring 3.2.4和Jackson 2.3.1),自定义序列化器的XMLconfiguration:

 <mvc:annotation-driven> <mvc:message-converters register-defaults="false"> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="objectMapper"> <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"> <property name="serializers"> <array> <bean class="com.example.business.serializer.json.CustomObjectSerializer"/> </array> </property> </bean> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> 

以无法解释的方式被某种东西覆盖回默认值。

这对我工作:

CustomObject.java

 @JsonSerialize(using = CustomObjectSerializer.class) public class CustomObject { private Long value; public Long getValue() { return value; } public void setValue(Long value) { this.value = value; } } 

CustomObjectSerializer.java

 public class CustomObjectSerializer extends JsonSerializer<CustomObject> { @Override public void serialize(CustomObject value, JsonGenerator jgen, SerializerProvider provider) throws IOException,JsonProcessingException { jgen.writeStartObject(); jgen.writeNumberField("y", value.getValue()); jgen.writeEndObject(); } @Override public Class<CustomObject> handledType() { return CustomObject.class; } } 

我的解决scheme中不需要XMLconfiguration( <mvc:message-converters>(...)</mvc:message-converters> )。

有很长一段时间org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean 。 从Spring Boot的1.2版本开始,有一个org.springframework.http.converter.json.Jackson2ObjectMapperBuilder for Java Config。

在string引导configuration可以像这样简单:

 spring.jackson.deserialization.<feature_name>=true|false spring.jackson.generator.<feature_name>=true|false spring.jackson.mapper.<feature_name>=true|false spring.jackson.parser.<feature_name>=true|false spring.jackson.serialization.<feature_name>=true|false spring.jackson.default-property-inclusion=always|non_null|non_absent|non_default|non_empty 

classpath:application.properties@Configuration类中的一些Java代码:

 @Bean public Jackson2ObjectMapperBuilder jacksonBuilder() { Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder(); builder.indentOutput(true).dateFormat(new SimpleDateFormat("yyyy-MM-dd")); return builder; } 

看到:

我正在使用Spring 4.1.6和Jackson FasterXML 2.1.4。

  <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="objectMapper"> <bean class="com.fasterxml.jackson.databind.ObjectMapper"> <!-- 设置不输出null字段--> <property name="serializationInclusion" value="NON_NULL"/> </bean> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> 

这工作在我的applicationContext.xmlconfiguration

我正在使用Spring 3.2.4和Jackson FasterXML 2.1.1。

我已经创build了一个自定义的JacksonObjectMapper ,它为映射的对象的每个属性使用显式注释:

 package com.test; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; public class MyJaxbJacksonObjectMapper extends ObjectMapper { public MyJaxbJacksonObjectMapper() { this.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY) .setVisibility(PropertyAccessor.CREATOR, JsonAutoDetect.Visibility.ANY) .setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE) .setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE) .setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE); this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); } } 

然后在上下文configuration( servlet-context.xml )中实例化:

 <mvc:annotation-driven> <mvc:message-converters> <beans:bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <beans:property name="objectMapper"> <beans:bean class="com.test.MyJaxbJacksonObjectMapper" /> </beans:property> </beans:bean> </mvc:message-converters> </mvc:annotation-driven> 

这工作正常!

如果只打算configurationObjectMapper ,则在Spring 4之上,不需要configurationMappingJacksonHttpMessageConverter

(configurationMappingJacksonHttpMessageConverter会导致你失去其他MessageConverter)

你只需要做:

 public class MyObjectMapper extends ObjectMapper { private static final long serialVersionUID = 4219938065516862637L; public MyObjectMapper() { super(); enable(SerializationFeature.INDENT_OUTPUT); } } 

在你的Springconfiguration中,创build这个bean:

 @Bean public MyObjectMapper myObjectMapper() { return new MyObjectMapper(); } 
Interesting Posts