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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24805043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:50:08  来源:igfitidea点击:

org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON

springresttemplate

提问by Vivek Giri

org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: (was java.lang.NullPointerException) (through reference chain: in.xyz.sync.dto.ClassRoom["id"]); nested exception is com.fasterxml.Hymanson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: in.xyz.sync.dto.ClassRoom["id"])

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

import java.util.List;

import org.codehaus.Hymanson.annotate.JsonIgnoreProperties;
import org.codehaus.Hymanson.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;


}

回答by Dave McLure

You may have better results switching from primitives (int & long) to Java classes (Int & Long) in your ClassRoom class. See JsonMappingException (was java.lang.NullPointerException)

在 ClassRoom 类中从基元(int & long)切换到 Java 类(Int & Long)可能会有更好的结果。请参阅JsonMappingException(是 java.lang.NullPointerException)

You might also have luck pinning down which field is null by assigning @NotNull (javax.validation.constraints.NotNull) to all of your fields. If you are encountering a null value, the @NotNull annotation will cause a runtime exception. Then, comment out the @NotNull annotations one by one until the exception goes away and you will find the culprit (or at least the first one of them if there are more than one).

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

回答by Francisco López-Sancho

I spend a whole day to fix an error like this. In my case I was having that Nullpointer because I had a Serializer that had a dependency that didn't instantiate, and the context of the original exception was gone! :'( so its message wasn't expressive enough. I had to debug many times until I find my Bean which wasn't properly instantiated. It has something to do with Spring MockMvc (I had to do a workaround, I mean, stop using this mock. Still looking forward to make it work ) In my case last external library method before finding my class was: com.fasterxml.Hymanson.databind.ser.BeanPropertyWriter.serializeAsField(Object bean, JsonGenerator jgen, SerializerProvider prov)

我花了一整天来修复这样的错误。在我的例子中,我有那个 Nullpointer 因为我有一个没有实例化的依赖的序列化器,并且原始异常的上下文消失了!:'( 所以它的信息不够表达。我不得不调试很多次,直到我发现我的 Bean 没有正确实例化。它与 Spring MockMvc 有关(我不得不做一个解决方法,我的意思是,停止使用这个模拟。仍然期待让它工作)在我的情况下,在找到我的类之前的最后一个外部库方法是:com.fasterxml.Hymanson.databind.ser.BeanPropertyWriter.serializeAsField(Object bean, JsonGenerator jgen, SerializerProvider prov)

It was here, last external class, from where I got NullPointerException. So my recommendation is track down that NullPointerException until you find the exact line. That will probably tell you what's happening.

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

Hope it helps.

希望能帮助到你。

回答by G.Raju

@GetMapping("/findAll")
@ResponseBody   
public Iterable<Customer> findAllCustomers() {

 return  repository.findAll();
}                               

When I am trying to do like this then I got the same exception

当我尝试这样做时,我遇到了同样的异常

After that, I tried with iterating and added to List then I return that list so my problem solved.

在那之后,我尝试迭代并添加到 List 然后我返回该列表所以我的问题解决了。

try this one!

试试这个!

@GetMapping("/findAll")
@ResponseBody   
public List<Customer> findAllCustomers() {
    List<Customer> customers = new ArrayList<>();
    Iterable<Customer> customersAll = repository.findAll();
    for (Customer customer : customersAll) {
        customers.add(customer);
    }
 return customers;
}

回答by Miguel Ramos

In my case, i just had to replace the int by Integer:

就我而言,我只需将 int 替换为 Integer:

e.g.:

例如:

private int idtask; => private Integer idtask;

Did the same for getter and setter.

对 getter 和 setter 做了同样的事情。

public Integer getIdlayer() {
        return idlayer;
    }

public void setIdlayer(Integer idlayer) {
    this.idlayer = idlayer;
}

This solved my issue. Basically i was having this issue because there was a null value in this column. Using: Spring Boot + PostgreSQL

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

Note: Dave McLure' answer is the solution too.

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