Rename "_embedded" to "data" or anything else in spring-hateoas

74 views Asked by At

I am building REST API with Spring HATEOAS.

After some tries, I got it upto this point.

{
    "_embedded": {
        "organizations": [
            {
                "id": "3b2ef569-acd2-40dc-a57d-c1170f6c7429",
                "name": "Google",
                "organizationGroupId": "1d52c6d8-5f71-4ccc-b47a-a98da7eef700",
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/api/v1/organizations/3b2ef569-acd2-40dc-a57d-c1170f6c7429"
                    },
                    "organizations": {
                        "href": "http://localhost:8080/api/v1/organizations"
                    },
                    "organizationGroup": {
                        "href": "http://localhost:8080/api/v1/organization-groups/1d52c6d8-5f71-4ccc-b47a-a98da7eef700"
                    }
                }
            }
        ]
    },
    "_links": {
        "first": {
            "href": "http://localhost:8080/api/v1/organizations?page=1&size=10&sort=createdAt,asc"
        },
        "prev": {
            "href": "http://localhost:8080/api/v1/organizations?page=1&size=10&sort=createdAt,asc"
        },
        "self": {
            "href": "http://localhost:8080/api/v1/organizations?page=2&size=10&sort=createdAt,asc"
        },
        "last": {
            "href": "http://localhost:8080/api/v1/organizations?page=2&size=10&sort=createdAt,asc"
        }
    },
    "page": {
        "size": 10,
        "totalElements": 11,
        "totalPages": 2,
        "number": 2
    }
}

I don't like the "_embedded" and "_links". Is there a way I can change these names? Or these are kind of standards which we are supposed to use in Spring?

I was able to change the collection names by using @Relation(collectionRelation = "organizations") annotation on my Dto class. But unable to find anything for renaming "_embedded".

1

There are 1 answers

0
yejianfengblue On

This project provides JSON:API media type support for Spring HATEOAS.

https://toedter.github.io/spring-hateoas-jsonapi/release/reference/index.html

The correct way is custom media type https://youtu.be/o4cc6KzNrpI?list=PL54DWbEqNyYmEXWtKIxWUtV-cIYqJuQCW&t=2700 if someone want another format.