Java Spring REST-@createdDate在抽象超类中抛出错误
我在春季休息方面遇到了麻烦(新来的) 我的意图是创建一个抽象的超类,其中包含一些公共字段,比如ID/Version/CreatedDate等等。 它在JPA和数据库方面工作得很好。但是,当我试图使用某个存储库Java Spring REST-@createdDate在抽象超类中抛出错误,java,spring,audit,spring-rest,Java,Spring,Audit,Spring Rest,我在春季休息方面遇到了麻烦(新来的) 我的意图是创建一个抽象的超类,其中包含一些公共字段,比如ID/Version/CreatedDate等等。 它在JPA和数据库方面工作得很好。但是,当我试图使用某个存储库paging和sortingrepository公开子级时,一旦查询api,就会出现以下错误 其他领域的工作还不错。它与@CreatedDate注释相对应 错误: Resolved exception caused by Handler execution: org.spri
paging和sortingrepository
公开子级时,一旦查询api,就会出现以下错误
其他领域的工作还不错。它与@CreatedDate注释相对应
错误:
Resolved exception caused by Handler execution:
org.springframework.http.converter.HttpMessageNotWritableException:
Could not write JSON: java.sql.Date cannot be cast to java.lang.String;
nested exception is com.fasterxml.jackson.databind.JsonMappingException:
java.sql.Date cannot be cast to java.lang.String (through reference chain:
org.springframework.hateoas.PagedResources["_embedded"]-
java.util.Collections$UnmodifiableMap["users"]->java.util.ArrayList[0]org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$PersistentEntityResourceSerializer$1["content"]->com.*.*.domain.User["createdDate"])
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class AbstractPersistentObject
implements PersistentObject, Serializable {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
protected Long id;
protected Integer version;
@org.springframework.data.annotation.CreatedDate
@Temporal(TemporalType.DATE)
public Date createdDate;
... getter / setter / constructor
protected Date getCreatedDate() {
return createdDate;
}
protected void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
@Entity
@Table(name = "UserTable")
public class User extends AbstractPersistentObject {
private String firstName;
private String lastName;
@NotNull
@Column(unique = true)
private String email;
@JsonIgnore
private String password;
private boolean verified;
...
Upgrading Spring Boot from 2.0.0.M2 to 2.0.0.M3.
Upgrading Spring Boot from 2.0.0.M2 to 2.0.0.M3.
Super:
Resolved exception caused by Handler execution:
org.springframework.http.converter.HttpMessageNotWritableException:
Could not write JSON: java.sql.Date cannot be cast to java.lang.String;
nested exception is com.fasterxml.jackson.databind.JsonMappingException:
java.sql.Date cannot be cast to java.lang.String (through reference chain:
org.springframework.hateoas.PagedResources["_embedded"]-
java.util.Collections$UnmodifiableMap["users"]->java.util.ArrayList[0]org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$PersistentEntityResourceSerializer$1["content"]->com.*.*.domain.User["createdDate"])
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class AbstractPersistentObject
implements PersistentObject, Serializable {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
protected Long id;
protected Integer version;
@org.springframework.data.annotation.CreatedDate
@Temporal(TemporalType.DATE)
public Date createdDate;
... getter / setter / constructor
protected Date getCreatedDate() {
return createdDate;
}
protected void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
@Entity
@Table(name = "UserTable")
public class User extends AbstractPersistentObject {
private String firstName;
private String lastName;
@NotNull
@Column(unique = true)
private String email;
@JsonIgnore
private String password;
private boolean verified;
...
Upgrading Spring Boot from 2.0.0.M2 to 2.0.0.M3.
Upgrading Spring Boot from 2.0.0.M2 to 2.0.0.M3.
示例子项:
Resolved exception caused by Handler execution:
org.springframework.http.converter.HttpMessageNotWritableException:
Could not write JSON: java.sql.Date cannot be cast to java.lang.String;
nested exception is com.fasterxml.jackson.databind.JsonMappingException:
java.sql.Date cannot be cast to java.lang.String (through reference chain:
org.springframework.hateoas.PagedResources["_embedded"]-
java.util.Collections$UnmodifiableMap["users"]->java.util.ArrayList[0]org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$PersistentEntityResourceSerializer$1["content"]->com.*.*.domain.User["createdDate"])
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class AbstractPersistentObject
implements PersistentObject, Serializable {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
protected Long id;
protected Integer version;
@org.springframework.data.annotation.CreatedDate
@Temporal(TemporalType.DATE)
public Date createdDate;
... getter / setter / constructor
protected Date getCreatedDate() {
return createdDate;
}
protected void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
@Entity
@Table(name = "UserTable")
public class User extends AbstractPersistentObject {
private String firstName;
private String lastName;
@NotNull
@Column(unique = true)
private String email;
@JsonIgnore
private String password;
private boolean verified;
...
Upgrading Spring Boot from 2.0.0.M2 to 2.0.0.M3.
Upgrading Spring Boot from 2.0.0.M2 to 2.0.0.M3.
解决方案:
Resolved exception caused by Handler execution:
org.springframework.http.converter.HttpMessageNotWritableException:
Could not write JSON: java.sql.Date cannot be cast to java.lang.String;
nested exception is com.fasterxml.jackson.databind.JsonMappingException:
java.sql.Date cannot be cast to java.lang.String (through reference chain:
org.springframework.hateoas.PagedResources["_embedded"]-
java.util.Collections$UnmodifiableMap["users"]->java.util.ArrayList[0]org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$PersistentEntityResourceSerializer$1["content"]->com.*.*.domain.User["createdDate"])
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class AbstractPersistentObject
implements PersistentObject, Serializable {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
protected Long id;
protected Integer version;
@org.springframework.data.annotation.CreatedDate
@Temporal(TemporalType.DATE)
public Date createdDate;
... getter / setter / constructor
protected Date getCreatedDate() {
return createdDate;
}
protected void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
@Entity
@Table(name = "UserTable")
public class User extends AbstractPersistentObject {
private String firstName;
private String lastName;
@NotNull
@Column(unique = true)
private String email;
@JsonIgnore
private String password;
private boolean verified;
...
Upgrading Spring Boot from 2.0.0.M2 to 2.0.0.M3.
Upgrading Spring Boot from 2.0.0.M2 to 2.0.0.M3.
尝试从包中使用@CreatedDate:
org.springframework.data.annotation.CreatedDate
在类AbstractPersistentObject
中,实例变量CreatedDate
声明为public,而getter和setter声明为protected。尝试反向访问,以便getter和setter是公共的,而实例变量是受保护的(或私有的)。感谢您的支持
我没有搞砸!我目前使用的是Spring boot 2.0.0.M2。我更新到2.0.0.M3,它就可以工作了
我很快调查了回购协议,但没有发现任何关于我们的信息
解决方案:
Resolved exception caused by Handler execution:
org.springframework.http.converter.HttpMessageNotWritableException:
Could not write JSON: java.sql.Date cannot be cast to java.lang.String;
nested exception is com.fasterxml.jackson.databind.JsonMappingException:
java.sql.Date cannot be cast to java.lang.String (through reference chain:
org.springframework.hateoas.PagedResources["_embedded"]-
java.util.Collections$UnmodifiableMap["users"]->java.util.ArrayList[0]org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$PersistentEntityResourceSerializer$1["content"]->com.*.*.domain.User["createdDate"])
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class AbstractPersistentObject
implements PersistentObject, Serializable {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
protected Long id;
protected Integer version;
@org.springframework.data.annotation.CreatedDate
@Temporal(TemporalType.DATE)
public Date createdDate;
... getter / setter / constructor
protected Date getCreatedDate() {
return createdDate;
}
protected void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
@Entity
@Table(name = "UserTable")
public class User extends AbstractPersistentObject {
private String firstName;
private String lastName;
@NotNull
@Column(unique = true)
private String email;
@JsonIgnore
private String password;
private boolean verified;
...
Upgrading Spring Boot from 2.0.0.M2 to 2.0.0.M3.
Upgrading Spring Boot from 2.0.0.M2 to 2.0.0.M3.
应该早点检查这一点,因为在使用此版本时,不知何故它是预期的 该错误表示存在数据类型转换问题。请包括
AbstractPersistentObject
中createdDate
的getter和setter。我包括了该字段的getter setter。回复的Thx您的项目中是否有注释@EnableJpaAuditing
?是的,我让createdDate注释工作。他们在儿童班的时候就工作。我查过了。遗憾的是,这是正确的,但我之前没有确定^^^是的,我已经和thoose玩过了。谢谢你的回复,我通过升级Spring Boot解决了这个问题。很高兴知道你解决了这个问题。为我没有要求版本信息而感到羞耻。我只是假设你使用的是一个稳定的版本,而不是一个里程碑式的版本。是的,我应该首先提到这一点。这只是一个用于学习目的的项目,所以我认为它会很好…:)