框架Spring MVC 4.x
休眠4.x
杰克逊2.8
我有两列,一个是publishDate
和createdDate
。用户需要手动输入发布日期。 MySQL列中的“创建日期”的默认设置为CURRENT_TIMESTAMP
,因此,在创建条目时,数据库将自动为该条目加上时间戳。
我有一本书POJO publishDate
和createdDate
字段... publishDate
由于某种原因可以处理空数据。但是“时间戳记”字段例外。为什么是这样?
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"])
我试图通过添加注释来抑制这种情况,我尝试了其中的几个注释,因为我一直在阅读要使用的注释部分上的冲突信息。
@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;
非常担心...感谢您的帮助。
最佳答案
在Book类中,从基元(int和double)切换到Java类(Int和Double)可能会有更好的结果。另请参见JsonMappingException (was java.lang.NullPointerException)