[英]How to customize JSON responses in Spring Boot?
I am new to Java and Spring Boot.我是 Java 和 Spring 引导的新手。 I am creating a REST Service using Spring Boot.
我正在使用 Spring 引导创建 REST 服务。 I have an Entity which contains only references to other entities like below:
我有一个实体,它只包含对其他实体的引用,如下所示:
@Entity
@Table(name = "table_xyz")
public class RolesAccessEntity {
@Id
@Column(name = "id", columnDefinition = "serial")
private int id;
@ManyToOne
@JoinColumn(name = "role_id", referencedColumnName = "id")
private RoleEntity roleEntity;
@ManyToOne
@JoinColumn(name = "page_id", referencedColumnName = "id")
private PagesEntity pagesEntity;
@ManyToOne
@JoinColumn(name="column_id", referencedColumnName = "id")
private ColumnsEntity columnsEntity;
}
I have to get the entire content and return the REST response.我必须获取全部内容并返回 REST 响应。 When I do that by putting the POJO in the ObjectNode using Jackson, I get the response as below:
当我使用 Jackson 将 POJO 放入 ObjectNode 时,我得到如下响应:
{
"success": true,
"values": [
{
"id": 32,
"roleEntity": {
"id": 15,
"roleName": "Role1"
},
"pagesEntity": {
"id": 1,
"pageName": "Page1"
},
"columnsEntity": {
"id": 1,
"columnName": "Column1"
}
},
{
"id": 33,
"roleEntity": {
"id": 15,
"roleName": "Role1"
},
"pagesEntity": {
"id": 1,
"pageName": "Page1"
},
"columnsEntity": {
"id": 2,
"columnName": "Column2"
}
},
{
"id": 34,
"roleEntity": {
"id": 15,
"roleName": "Role1"
},
"pagesEntity": {
"id": 1,
"pageName": "Page1"
},
"columnsEntity": {
"id": 3,
"columnName": "Column3"
}
}
]
}
But, how do I group things together?但是,我如何将事物组合在一起? Like this:
像这样:
{
"success": true,
"values":
[
{
"id": 32,
"roleEntity": {
"role_id": 15,
"roleName": "Role1",
"values": [
{"page_id": 1,
"page_name": "Page1",
"values": [
{
"column_id": 1,
"column_name": "Column1"
},
{
"column_id": 2,
"column_name": "Column2"
}
]
},
{"page_id": 2,
"page_name": "Page2",
"values": [
{
"column_id": 3,
"column_name": "Column1"
},
{
"column_id": 4,
"column_name": "Column2"
}
]
}
]}}]}
I can probably write a hashmap in service and group by writing a lot of nested code to get this.我可能可以通过编写大量嵌套代码来在服务和组中编写 hashmap 来获得它。 But does not feel like very Java-ish.
但感觉不是很Java-ish。 Is there a way I could write another POJO which will give me this result?
有没有办法我可以编写另一个 POJO 来给我这个结果?
Make a two-layer: one is the MySQL POJO model and another model (DTO) which used for delivering the response for REST.制作两层:一层是 MySQL POJO model 和另一层 model (DTO),用于传递 Z50780F47F568 的响应when you fetch the data from the MySQL POJO model and use a mapper class to convert the data from MySQL POJO model to DTO model and send the DTO model as a response.
when you fetch the data from the MySQL POJO model and use a mapper class to convert the data from MySQL POJO model to DTO model and send the DTO model as a response. In the DTO model, we can use annotations which helps to design the JSON response.
在 DTO model 中,我们可以使用有助于设计 JSON 响应的注释。
For example: LocalDataTime variable will give you a long detail format in the response for that we use the serializer (annotation) to format it例如:LocalDataTime 变量将在响应中为您提供一个很长的详细格式,因为我们使用序列化程序(注释)来格式化它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.