希望我能得到一些解决特定问题的帮助。我基本上是在尝试创建一个REST API端点,该端点显示我的用户和AMP之间的一对一关系;USERPROFILE类。我正在尝试使用注释来执行此操作,但我没有任何运气。我似乎无法返回用户信息以及关联的个人资料信息。这是我的班级
package com.account.service.model;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import javax.persistence.*;
import java.util.Date;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String email;
private Boolean active;
@JsonIgnore
private String password;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private Date created_at;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private Date updated_at;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "user")
@JoinColumn(name = "user_id")
@JsonBackReference
private UserProfile userProfile;
public User(){}
public User(String email, Boolean active, String password, Date created_at, Date updated_at) {
this.email = email;
this.active = active;
this.password = password;
this.created_at = created_at;
this.updated_at = updated_at;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Boolean getActive() {
return active;
}
public void setActive(Boolean active) {
this.active = active;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getCreated_at() {
return created_at;
}
public void setCreated_at(Date created_at) {
this.created_at = created_at;
}
public Date getUpdated_at() {
return updated_at;
}
public void setUpdated_at(Date updated_at) {
this.updated_at = updated_at;
}
public UserProfile getUserProfile() {
return userProfile;
}
public void setUserProfile(UserProfile userProfile) {
this.userProfile = userProfile;
}
}
这是用户配置文件
package com.account.service.model;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import javax.persistence.*;
@Entity
@Table(name = "user_profiles")
public class UserProfile {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String first_name;
private String last_name;
@OneToOne(cascade = CascadeType.ALL, optional = false)
@JsonManagedReference
private User user;
public UserProfile(){}
public UserProfile(String first_name, String last_name, User user) {
this.first_name = first_name;
this.last_name = last_name;
this.user = user;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
我得到的响应是缺少配置文件关联的以下内容。
[
{
"id": 1,
"email": "ul@gmail.com",
"active": true,
"created_at": "2017-12-21",
"updated_at": "2017-12-21"
}
]
关于问题可能是什么的想法?
目的是显示以下内容:
[
{
"id": 1,
"email": "ul@gmail.com",
"active": true,
"created_at": "2017-12-21",
"updated_at": "2017-12-21",
"profile": {
"id": 1,
"first_name": "master",
"last_name": "splinter"
}
}
]
弄清楚了,如果其他任何人都陷入了此类问题上,请查看@JsonBackReference
,这应该在您的用户类上使用,而不是在您的个人资料类中。