How to use spring.data.rest.enable-enum-translation in Spring Data REST

I am using Spring Boot 1.5.3, Spring Data REST, HATEOAS, Hibernate. In my model, sometimes I use an enum like:

    public enum Roles {
    ROLE_ADMIN, ROLE_USER, ROLE_MANAGER, ROLE_TECH
}

      

According to the Spring Boot documentation , there is a property that seems useful:

# DATA REST (RepositoryRestProperties)
spring.data.rest.enable-enum-translation=true

      

I haven't found any documentation on how to use it. I found old links where it seems like I should add something like:

roles.role_admin=Amministratore

      

in my .properties posts. That would be cool, but it didn't work and my REST answer contains the enum value shown as in the class without any translation. Can anyone explain to me the correct way to use this Spring feature?

+3


source to share


1 answer


To use this feature, you must add the "resource package" "rest-messages" to your project's resources folder. Then describe your enums in those files as follows:

com.example.myproject.myapp.Roles.ROLE_ADMIN=Amministratore
com.example.myproject.myapp.Roles.ROLE_USER=Utente

      

If you have nested enums, you must join them, and the parent class with a "$" sign:

com.example.myproject.myapp.User$Roles.ROLE_ADMIN=Amministratore
com.example.myproject.myapp.User$Roles.ROLE_USER=Utente

      

You can describe your links in the same way:



_links.user.title=Utente
_links.users.title=Lista utenti

      

Then you get something like this:

"_links": {
    "user": {
        "href": "http://localhost:8080/api/users/1",
        "title": "Utente"
    }
}

"users": {
    "href": "http://localhost:8080/api/users{?page,size,sort}",
    "templated": true,
    "title": "Lista utenti"
}

      

There is also some information in the SDR link .

See Restbucks example .

+3


source







All Articles