使用Spring时报Failed to write HTTP message:Could not write content与自动拆箱

Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: (was java.lang.NullPointerException) (through reference chain: java.util.HashMap["AccountModle_H"]->java.util.ArrayList[0]->com.wgs.tender.modle.AccountModle["pageNum"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: java.util.HashMap["AccountModle_H"]->java.util.ArrayList[0]->com.wgs.tender.modle.AccountModle["pageNum"])

- Handler execution resulted in exception: Could not write content: (was java.lang.NullPointerException) (through reference chain: java.util.HashMap["AccountModle_H"]->java.util.ArrayList[0]->com.wgs.tender.modle.AccountModle["pageNum"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: java.util.HashMap["AccountModle_H"]->java.util.ArrayList[0]->com.wgs.tender.modle.AccountModle["pageNum"])

在做项目时我遇到的报错如上所示。

这个异常是我在使用Spring中,在JavaBean中使用了包装类Integer,

private Integer  pageNum而在方法中使用了@ResponseBody注解,

在return  map返回给前台时Spring中的类com.fasterxml.jackson.databind.JsonMappingException

这个类会对包装类自动拆箱成对应的基本类型,如果返回的Integer中存在null的话,那么在拆箱成int时就会报错

如果将null赋值给包装类Integer,然后将Integer赋给各自的基本类型,编译器不会报,但是你将会在运行时期遇到空指针异常。这是Java中的自动拆箱导致的,任何含有null值的包装类在Java拆箱生成基本数据类型时候都会抛出一个空指针异常



 

猜你喜欢

转载自blog.csdn.net/wgs_93/article/details/56014607