Kaushik
Kaushik

Reputation: 553

How to customize JSON responses in Spring Boot?

I am new to Java and Spring Boot. I am creating a REST Service using Spring Boot. 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. When I do that by putting the POJO in the ObjectNode using Jackson, I get the response as below:

{
    "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. But does not feel like very Java-ish. Is there a way I could write another POJO which will give me this result?

Upvotes: 0

Views: 531

Answers (1)

Mohamed Ismail M
Mohamed Ismail M

Reputation: 100

Make a two-layer: one is the MySQL POJO model and another model (DTO) which used for delivering the response for REST . 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.

For example: LocalDataTime variable will give you a long detail format in the response for that we use the serializer (annotation) to format it

Upvotes: 1

Related Questions