【问题标题】:JacksonMapping Exception :: HttpMessageNotWritableException: Could not write content: (was java.lang.NullPointerException)JacksonMapping 异常 :: HttpMessageNotWritableException: 无法写入内容: (是 java.lang.NullPointerException)
【发布时间】:2017-05-10 05:07:35
【问题描述】:
  • 框架 Spring MVC 4.x
  • 休眠 4.x
  • 杰克逊 2.8

我有两列,一列是publishDatecreatedDate。发布日期用户需要手动输入。 MySQL 列中的创建日期默认设置为CURRENT_TIMESTAMP,因此当创建条目时,DB 会自动为该条目添加时间戳。

我有一本书 POJO publishDatecreatedDate 字段...publishDate 可以出于某种原因处理空数据。但是 Timestamp 字段出现异常。这是为什么呢?

org.springframework.http.converter.HttpMessageNotWritableException: Could not 
write content: (was java.lang.NullPointerException) (through reference chain: 
java.util.HashMap["results"]->java.util.ArrayList[30]-
>com.app.books.Book["dateCreated"]); nested exception is 
com.fasterxml.jackson.databind.JsonMappingException: (was 
java.lang.NullPointerException) (through reference chain: 
java.util.HashMap["results"]->java.util.ArrayList[30]-
>com.app.books.Book["dateCreated"])

我试图通过添加注释来抑制这一点,我尝试了其中的几个,因为我一直在阅读有关要使用的 cmets 部分的冲突信息。

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) // but show JsonSerialize is deprecated

@JsonInclude(Include.NON_NULL)// Include can not be resolved as variable

@JsonInclude(JsonInclude.Include.NON_DEFAULT) // finally doesn't give me an error but I still get the same exception.

这是我的书课

@Entity
@Table(name="books")
@Component
public class Book implements Serializable{


/**
 * 
 */
private static final long serialVersionUID = -2042607611480064259L;

@Id
@GeneratedValue
private int id;

@NotBlank
private String name;

@NotBlank
@Size(min=2, max=16)
private String ispn;

@DecimalMin(value = "0.1")
private double price;

//@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
//@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
//@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
private Timestamp dateCreated;

private Date datePublished;

写得很详细...感谢您的帮助..

【问题讨论】:

    标签: mysql json spring jackson


    【解决方案1】:

    在 Book 类中从原语(int 和 double)切换到 Java 类(Int 和 Double)可能会有更好的结果。见JsonMappingException (was java.lang.NullPointerException) 也见JSON: JsonMappingException while try to deserialize object with null values

    【讨论】:

      猜你喜欢
      • 2019-09-10
      • 2021-08-03
      • 2019-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-09
      相关资源
      最近更新 更多