前端的同事要求说尽量不要有null,可有为空串“” 或者 0 或者 [], 但尽量不要null。
所以@JsonInclude(Include.NON_NULL) 这个注解放在类头上就可以解决。 实体类与json互转的时候 属性值为null的不参与序列化
import com.fasterxml.jackson.Annotation.JsonInclude;
import com.fasterxml.jackson.Annotation.JsonInclude.Include;
@JsonInclude(Include.NON_NULL)
public class WithdrawDetail implements serializable {
}
或者
WithdrawDetail wd = new WithdrawDetail();
wd.setserializationInclusion(Include.NON_null);
实际效果
全局配置
springMVC.xml
<!-- 默认的注解映射的支持 比如requestMapper之类的 --><mvc:Annotation-driven><mvc:message-converters> <bean class="org.springframework.http.converter.json.MappingJackson2httpmessageConverter"><property name="objectMapper"><bean class="com.fasterxml.jackson.databind.objectMapper"><property name="serializationInclusion"> <value type="com.fasterxml.jackson.Annotation.JsonInclude.Include">NON_NULL</value> </property></bean></property></bean></mvc:message-converters></mvc:Annotation-driven>