Java Don';当属性为';这是杰克逊的空名单
我有一个类需要使用jackson进行去序列化,该类有一个collection属性。集合为空,但不为null。 问题:如何在没有空集合的情况下反序列化类。示例代码如下:Java Don';当属性为';这是杰克逊的空名单,java,json,jackson,Java,Json,Jackson,我有一个类需要使用jackson进行去序列化,该类有一个collection属性。集合为空,但不为null。 问题:如何在没有空集合的情况下反序列化类。示例代码如下: class Person { String name; List<Event> events; //....getter, setter } 不是 怎么做?多谢各位~~ ================================================ 我已经根据尼克拉斯的建议解
class Person
{
String name;
List<Event> events;
//....getter, setter
}
不是
怎么做?多谢各位~~
================================================
我已经根据尼克拉斯的建议解决了这个问题。首先谢谢你。
我的jackson版本是2.1.1,Spring-3.2.2为这个版本导入了更好的支持。此外,这也适用于数组和集合。
以下是我的配置:
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="objectMapper"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!--Json Mapper-->
<bean name="objectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean" autowire="no">
<property name="featuresToDisable">
<list>
<!--not to return empty colletion-->
<value type="com.fasterxml.jackson.databind.SerializationFeature">WRITE_EMPTY_JSON_ARRAYS</value>
</list>
</property>
</bean>
写入空的JSON数组
Person
,但没有events
属性。然后,根据集合的值,您可以决定序列化原始的Person
对象,或者序列化替代的events
-less对象。这非常简单,应该适用于上述情况,但不如第一种选择灵活如果您可以使用Jackson注释更改原始模型,下面是实现此目的的方法 杰克逊有。您可以通过以下方式将其关闭:
objectMapper.configure(SerializationConfig.Feature.WRITE_EMPTY_JSON_ARRAYS, false);
但它只适用于数组
,而不适用于集合
。但是您可以将@JsonProperty
与@JsonIgnore
组合,如下所示:
//....getter, setter of Person class
@JsonIgnore
public List<Event> getEvents() {
return events;
}
@JsonProperty("events")
public Event[] getEventsArr() {
return events.toArray(new Event[0]);
}
Offtop:通常指定ObjectMapper
的显式实例非常有用,因为:
- 您可以按照自己的方式进行配置
- 您可以通过
@Autowired
应用程序中添加以下属性即可:
spring.jackson.serialization-inclusion=NON_EMPTY
您可以在类或字段上使用@JsonInclude(JsonInclude.Include.NON_EMPTY)注释
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
class Person
{
String name;
List<Event> events;
//....getter, setter
}
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
班主任
{
字符串名;
列出事件;
//……盖特,塞特
}
Spring boot会自动从application.properties中选取属性
您可以添加以下属性:
spring.jackson.serialization.write-empty-json-arrays=false
使用此注释@JsonInclude(JsonInclude.Include.NON\u EMPTY)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
person.list=新列表();
从文件
值,该值指示仅具有null值的属性,或者
被认为是空的,不包括在内
@JsonInclude(JsonInclude.Include.NON_EMPTY)包含这个,在类级别,将做以下事情
仅包含非空值
仅包括非空列表
我可以在注释中使用这个吗?我使用springmvc@ResponseBody进行自动反序列化:objectMapper.configure(SerializationConfig.Feature.WRITE\u EMPTY\u JSON\u array,false)我使用Jackson 2.x版。你知道怎么配置吗?我已经解决了!非常感谢。对于jackson2,请在我的questoin.jackson2.x中找到更多更新的答案:objectMapper.configure(SerializationFeature.WRITE\u EMPTY\u JSON\u array,false);适用于我的ArrayList案例这是与当前选择的答案(已弃用)相比的最佳答案@从2.8开始就不推荐使用,因为有更好的机制来指定筛选;特别是使用{@link com.fasterxml.jackson.annotation.JsonInclude}或配置覆盖。这对我不起作用。但是,确实是这样:spring.jackson.defaultproperty inclusion=NON_EMPTY
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper" ref="objectMapper"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!--custom Json Mapper configuration-->
<bean name="objectMapper" class="org.springframework.http.converter.json.JacksonObjectMapperFactoryBean" autowire="no">
<property name="featuresToDisable">
<list>
<!--Ignore unknown properties-->
<value type="org.codehaus.jackson.map.SerializationConfig.Feature">WRITE_EMPTY_JSON_ARRAYS</value>
</list>
</property>
</bean>
spring.jackson.serialization-inclusion=NON_EMPTY
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
class Person
{
String name;
List<Event> events;
//....getter, setter
}
spring.jackson.serialization.write-empty-json-arrays=false
@JsonInclude(JsonInclude.Include.NON_EMPTY)
person.list = new List<Event>();