java jhipster项目中json中实体属性camel case转换为snake case

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40782685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:28:13  来源:igfitidea点击:

Convert entity property camel case to snake case in json in jhipster project

javajsonspring-bootjhipster

提问by Md. Shougat Hossain

I am working with a project that is generated with jhipster. It is a micro service architecture project.

我正在处理一个用 jhipster 生成的项目。它是一个微服务架构项目。

In my entity class properties are named with camel case. So when I create a rest service it gives me json, where the json property names are as same as the entity properties.

在我的实体类中,属性以驼峰命名。所以当我创建一个休息服务时,它给了我 json,其中 json 属性名称与实体属性相同。

Entity class

实体类

@Entity
@Table(name = "ebook")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "ebook")
public class Ebook implements Serializable {

    private Long id;
    private String nameBangla;
    private String nameEnglish;

Json response

JSON 响应

{
   "id": 0,
   "nameBangla": "string",
   "nameEnglish": "string"
}

I want that my entity property will camel case, But in json response it will snake case. That is I don't want to change my entity class but I want to change my json response like bellow

我希望我的实体属性是驼峰式的,但在 json 响应中它是蛇形的。那是我不想更改我的实体类,但我想更改我的 json 响应,如下所示

{
   "id": 0,
   "name_bangla": "string",
   "name_english": "string"
}

回答by Indivon

You have two possibilities:

你有两种可能:

Explicit naming your properties:

显式命名您的属性:

@JsonProperty("name_bangla")
private String nameBangla;
@JsonProperty("name_english")
private String nameEnglish;

or changing how Hymanson (which is used for de/serialization) works:

或更改 Hymanson(用于反/序列化)的工作方式:

Hymanson has a setting called PropertyNamingStrategy.SNAKE_CASEwhich you can set for the Hymanson objectmapper.

Hymanson 有一个名为的设置PropertyNamingStrategy.SNAKE_CASE,您可以为 Hymanson objectmapper 设置。

So, you need to configure Hymanson for that, e.g. by adding your own object mapper:

因此,您需要为此配置 Hymanson,例如通过添加您自己的对象映射器:

@Configuration
public class HymansonConfiguration {

    @Bean
    public Hymanson2ObjectMapperBuilder Hymanson2ObjectMapperBuilder() {
        return new Hymanson2ObjectMapperBuilder().propertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
    }
} 

As far as I know, in older version of JHipster, there was already a HymansonConfigurationto configure the JSR310 time module, but was removed later...

据我所知,在旧版本的JHipster中,已经有一个HymansonConfiguration配置JSR310时间模块,但后来被删除了......

Adding this to your application.yml should also work:

将此添加到您的 application.yml 也应该有效:

spring.Hymanson.property-naming-strategy=SNAKE_CASE

回答by Sonique

Also you can use annotation to define naming strategy per class.

您也可以使用注释来定义每个类的命名策略。

Little example in Kotlin:

Kotlin 中的小例子:

@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy::class)
data class Specialization(val altUrl: String, val altId: Int, val altName: String)