@jsoninclude(include.non\u null)不工作/jackson序列化空值

9cbw7uwe  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(391)

我已经将注解放置在类/pojo上,并配置了Map器,但它仍然序列化空值
我使用的是Hibernate4.3.7final和Jackson2.4.4。集合是延迟加载的
pojo:删除了getter和setter

@JsonInclude(Include.NON_NULL)
@Entity
@Table
public class School {

    @Id
    @GeneratedValue
    private int id;

    @OneToMany(cascade=CascadeType.ALL,fetch= FetchType.LAZY)
    private List<Student> students;

    @OneToMany(cascade=CascadeType.ALL,fetch= FetchType.LAZY)
    private List<Employee> staff;

}

jsonmapper公司:

@Component
public class JSONMapper extends ObjectMapper {
/**
     * 
     */
    private static final long serialVersionUID = -3131980955975958812L;

//ref http://blog.pastelstudios.com/2012/03/12/spring-3-1-hibernate-4-jackson-module-hibernate/ 

    public JSONMapper() {

        Hibernate4Module hm = new Hibernate4Module();
        registerModule(hm);
        configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        configure(SerializationFeature.INDENT_OUTPUT , false);
        configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
        setSerializationInclusion(Include.NON_NULL);  
    }
}

输出:

{"id":1,"students":null,"staff":null}
velaa5lx

velaa5lx1#

您可能想要针对项目提交一个bug;可能是延迟加载集合的处理(确实需要特殊处理并覆盖到默认集合)没有执行正确的包含检查。

7d7tgy0s

7d7tgy0s2#

尝试改用jsoninclude.non\u empty。

相关问题