Spring数据休息@ManyToOne字段不以json形式出现

Sun*_*dav 5 json spring-data-jpa spring-data-rest spring-boot

我正在使用 Spring Boot、Spring Data JPA 和 Spring Data Rest 技术开发一个 Web 项目。我能够成功设置所有内容并能够获取简单 POJO 的 JSON。我自定义了两个类以具有 OneToMany 和 ManyToOne 关系,如下所示:-

@Entity
@Table(name="t_profile")
public class Profile {
@Id
@column(name="profile_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
@JoinColumn(name = "cat_id", referencedColumnName = "category_id")
@ManyToOne(optional=false)
private Category category;

// getters and setters
}

@Entity
@Table(name="t_category")
public class Category {
 @Id
 @column(name="category_id")
 @GeneratedValue(strategy = GenerationType.AUTO)
 private long id;
 private String name;
 @OneToMany(mappedBy="category")
 private List<Profile> profile; 
 // getters and setters 
}
http://localhost:8080/project/profiles
Run Code Online (Sandbox Code Playgroud)

当我使用休息客户端访问配置文件时;我能够获得带有 id、name 字段的 json 格式,但 ManyToOne 字段不是以 json 形式出现,而在控制器中进行调试时,配置文件列表具有类别值。但它不是以 json 形式出现的。

有什么想法吗?

cha*_*ybr 1

ManyToOne 字段将以链接形式出现。即在访问类别时,配置文件字段将列在 JSON 正文中的“_links”下,如下所示:

"_links" : {
    "profile" : {
        "href" : "http://<host>/<baseUrl>/category/<categoryId>/profile"
}
Run Code Online (Sandbox Code Playgroud)

要进一步获取给定类别的配置文件详细信息,请调用以下 api:

http://<host>/<baseUrl>/category/<categoryId>/profile
Run Code Online (Sandbox Code Playgroud)