Spring REST - 抽象超类中的@createdDate 抛出错误
Spring REST - @createdDate in Abstract superclass throws error
我在使用 Spring REST(新手)时遇到了问题。
我的目的是让 Abstract-Super-Class 包含一些常用字段,例如 ID/Version/CreatedDate 等等。
它在 JPA 和数据库方面工作得很好。但是,当我尝试使用某些存储库 PagingAndSortingRepository
公开 child 时,一旦我查询 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["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;
}
示例child:
@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.
尝试使用包中的@CreatedDate:org.springframework.data.annotation.CreatedDate
在 class AbstractPersistentObject
中,实例变量 createdDate
被声明为 public 而 getter 和 setter 被声明为 受保护 。尝试反转访问,以便 getter 和 setter 是 public 并且实例变量是 protected (或私人)。
感谢支持。
幸好我没有搞砸!我目前使用 Spring boot 2.0.0.M2。我更新到 2.0.0.M3 并且它正常工作。
我很快查看了回购协议,但没有找到任何关于我们的信息。
解法:
Upgrading Spring Boot from 2.0.0.M2 to 2.0.0.M3.
应该早点检查这个,因为在使用这个版本时它是预期的!
我在使用 Spring REST(新手)时遇到了问题。
我的目的是让 Abstract-Super-Class 包含一些常用字段,例如 ID/Version/CreatedDate 等等。
它在 JPA 和数据库方面工作得很好。但是,当我尝试使用某些存储库 PagingAndSortingRepository
公开 child 时,一旦我查询 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["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;
}
示例child:
@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.
尝试使用包中的@CreatedDate:org.springframework.data.annotation.CreatedDate
在 class AbstractPersistentObject
中,实例变量 createdDate
被声明为 public 而 getter 和 setter 被声明为 受保护 。尝试反转访问,以便 getter 和 setter 是 public 并且实例变量是 protected (或私人)。
感谢支持。
幸好我没有搞砸!我目前使用 Spring boot 2.0.0.M2。我更新到 2.0.0.M3 并且它正常工作。
我很快查看了回购协议,但没有找到任何关于我们的信息。
解法:
Upgrading Spring Boot from 2.0.0.M2 to 2.0.0.M3.
应该早点检查这个,因为在使用这个版本时它是预期的!