org.springframework.http.converter.HttpMessageNotWritableException:无法写入JSON

问题描述 投票:0回答:3

org.springframework.http.converter.HttpMessageNotWritableException:无法编写JSON :(是java.lang.NullPointerException)(通过引用链:in.xyz.sync.dto.ClassRoom [“id”]);嵌套异常是com.fasterxml.jackson.databind.JsonMappingException :(是java.lang.NullPointerException)(通过引用链:in.xyz.sync.dto.ClassRoom [“id”])

import java.util.List;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.map.annotate.JsonSerialize;

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class ClassRoom extends CommonResponse {

    private long classId;
    private String clientClassRoomId;
    private long instituteId;
    private long teacherId;
    private String className;
    private long handleId;
    private int archived;
    private int deleted;
    private String creationTime;
    private String modifiedTime;
    private int type;
    private String classCode;
    private List<Student> student;
    private String handle;
    private String firstName;


}
spring resttemplate
3个回答
2
投票

在ClassRoom类中,您可能有更好的结果从基元(int和long)切换到Java类(Int和Long)。见qazxsw poi

通过将@NotNull(javax.validation.constraints.NotNull)分配给所有字段,您可能还可以确定哪个字段为空。如果遇到空值,则@NotNull注释将导致运行时异常。然后,逐个注释@NotNull注释,直到异常消失为止,你会发现罪魁祸首(或者至少是第一个,如果有多个)。


1
投票

我花了一整天来修复这样的错误。在我的情况下,我有Nullpointer因为我有一个没有实例化的依赖的Serializer,原始异常的上下文消失了! :'(所以它的消息不够表达。我不得不多次调试,直到找到没有正确实例化的Bean。它与Spring MockMvc有关(我必须做一个解决方法,我的意思是,停止使用这个模拟。仍然期待让它工作)在我的情况下,在找到我的课之前的最后一个外部库方法是:JsonMappingException (was java.lang.NullPointerException)

它就在这里,最后一个外部类,从那里我得到了NullPointerException。所以我的建议是跟踪NullPointerException,直到找到确切的行。这可能会告诉你发生了什么。

希望能帮助到你。


0
投票

在我的情况下,我只需要用Integer替换int:

e.g:

com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(Object bean, JsonGenerator jgen, SerializerProvider prov)

对吸气剂和二传手也一样。

private int idtask; => private Integer idtask;

这解决了我的问题。基本上我遇到了这个问题,因为此列中有一个空值。使用:Spring Boot + PostgreSQL

注意:Dave MccLure的回答是解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.