如何在Spring Data REST中使用spring.data.rest.enable-enum-translation [英] How to use spring.data.rest.enable-enum-translation in Spring Data REST

查看:219
本文介绍了如何在Spring Data REST中使用spring.data.rest.enable-enum-translation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Boot 1.5.3,Spring Data REST,HATEOAS,Hibernate. 在我的模型中,有时会使用如下枚举:

I'm using Spring Boot 1.5.3, Spring Data REST, HATEOAS, Hibernate. In my model sometimes I'm using enum like:

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

根据Spring Boot 文档,有一个看起来有用的属性:

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

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

我没有找到有关如何使用它的文档.我发现旧的参考文献似乎应该添加类似的内容:

I didn't find documentation about how to use that. I found old references where seems I should add something like:

roles.role_admin=Amministratore

在我的messages.properties中.那会很酷,但是不起作用,我的REST回复包含类中显示的枚举值,没有任何翻译. 有人可以向我解释使用Spring的此功能的正确方法吗?

in my messages.properties. That would be cool but it doesn't work and my REST reply contains enum value shown like in the class, without any translation. Could someone explain me the right way to use this function of Spring?

推荐答案

要使用此功能,您必须向项目的资源"文件夹中添加其余消息"资源束.然后像下面这样在这些文件中描述您的枚举:

To use this feature you have to add a 'rest-messages' Resource Bundle to your project 'resources' folder. Then describe your enums in these files like this:

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

如果您有嵌套枚举,则必须将它们和带有'$'号的父类加入:

If you have a nested enums you have to join them and the parent class with '$' sign:

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

您可以用相同的方式描述您的链接:

In the same manner you can describe your links:

_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"
}

还有一个 SDR参考中有关此问题的一点信息.

There is also a little bit info in the SDR reference regarding to this issue.

请参见 Restbucks示例.

这篇关于如何在Spring Data REST中使用spring.data.rest.enable-enum-translation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆