gpt4 book ai didi

java - Spring REST - 抽象父类(super class)中的 @createdDate 抛出错误

转载 作者:行者123 更新时间:2023-12-01 06:05:32 25 4
gpt4 key购买 nike

我在使用 Spring REST 时遇到问题(新手)。

我的目的是拥有一个带有一些常见字段(例如 ID/Version/CreatedDate 等)的抽象父类(super class)。它与 JPA 和数据库配合得很好。但是,当我尝试使用某些存储库 PagingAndSortingRepository 公开子项时,一旦查询 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"])

super :

@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.

最佳答案

AbstractPersistentObject类中,实例变量createdDate被声明为public,而getter和setter被声明为protected。尝试反转访问权限,以便 getter 和 setter 是公共(public)的,而实例变量是 protected (或私有(private)的)。

关于java - Spring REST - 抽象父类(super class)中的 @createdDate 抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45661735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com