【问题标题】:How to use spring.data.rest.enable-enum-translation in Spring Data REST如何在 Spring Data REST 中使用 spring.data.rest.enable-enum-translation
【发布时间】:2017-12-01 16:47:10
【问题描述】:

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

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

根据 Spring Boot documentation,有一个属性似乎很有用:

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

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

roles.role_admin=Amministratore

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

【问题讨论】:

    标签: spring spring-mvc spring-boot spring-data-rest


    【解决方案1】:

    要使用此功能,您必须将“rest-messages”资源包添加到您的项目“资源”文件夹中。然后像这样在这些文件中描述你的枚举:

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

    如果你有一个嵌套枚举,你必须加入它们和带有'$'符号的父类:

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

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

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

    然后你会得到这样的东西:

    "_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 参考中也有一个 little bit info 与此问题有关。

    Restbucks example

    【讨论】:

    • 非常感谢!明天我会试试的。此功能是否向后工作?我的意思是,假设一个 Angular 应用程序得到了这个带有翻译后的枚举的 REST 响应。当 Web 应用程序保存传递翻译后的枚举的用户时,服务器是否理解它?谢谢
    • 不知道,但我确定它有效。请在尝试时提供反馈...
    猜你喜欢
    • 1970-01-01
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 2016-04-30
    • 2014-11-22
    • 2019-09-16
    • 2016-08-02
    相关资源
    最近更新 更多