简体   繁体   English

在jhipster项目中将实体属性驼峰案例转换为json中的蛇案例

[英]Convert entity property camel case to snake case in json in jhipster project

I am working with a project that is generated with jhipster. 我正在使用jhipster生成的项目。 It is a micro service architecture project. 这是一个微服务架构项目。

In my entity class properties are named with camel case. 在我的实体类中,属性以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. 我想要我的实体属性将驼峰的情况,但在json响应它将蛇案。 That is I don't want to change my entity class but I want to change my json response like bellow 那是我不想改变我的实体类,但我想改变我的json响应,如下所述

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

You have two possibilities: 你有两种可能性:

Explicit naming your properties: 明确命名您的属性:

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

or changing how jackson (which is used for de/serialization) works: 或改变jackson(用于去/序列化)的工作原理:

Jackson has a setting called PropertyNamingStrategy.SNAKE_CASE which you can set for the jackson objectmapper. Jackson有一个名为PropertyNamingStrategy.SNAKE_CASE的设置,你可以为jackson objectmapper设置它。

So, you need to configure Jackson for that, eg by adding your own object mapper: 因此,您需要为此配置Jackson,例如通过添加您自己的对象映射器:

@Configuration
public class JacksonConfiguration {

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

As far as I know, in older version of JHipster, there was already a JacksonConfiguration to configure the JSR310 time module, but was removed later... 据我所知,在旧版JHipster中,已经有一个JacksonConfiguration来配置JSR310时间模块,但后来被删除了......

Adding this to your application.yml should also work: 将此添加到您的application.yml也应该工作:

spring.jackson.property-naming-strategy=SNAKE_CASE

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将蛇形案例转换为骆驼案例以进行 Spring 分页和排序 - Convert snake case to camel case for Spring Paging and Sorting 泽西JSON从骆驼箱切换到下划线(蛇盒) - Jersey JSON switching from camel case to underscores (snake case) Jackson - JsonTypeInfo,可以处理骆驼和蛇案例的属性 - Jackson - JsonTypeInfo, property that can handles both camel and snake case 有什么简单的方法可以将驼峰式案例正确转换为蛇式案例吗? - Is there any simple way to convert camel-case to snake-case correctly? 在 springboot 应用程序中的 H2 db 中创建表和列时,无法将骆驼案例转换为蛇案例 - Not able to convert camel case to snake case while creating tables and columns in H2 db in a springboot application 如何在 Spring Boot 中自动将 camel case 请求正文转换为 snake case protobuf 消息 - How to auto convert camel case request body to snake case protobuf message in Spring Boot 使用Jackson进行JSON字符串序列化(驼峰式)和反序列化(来自蛇形) - JSON string serialization ( to camel case ) and deserialization (from snake case )using Jackson 如何在Java中将snake_case JSON转换为嵌套JSON? - How to convert a snake_case JSON to nested JSON in java? 我应该如何将 JSON 有效载荷的大小写转换为小驼峰大小写? - How should I convert the case of a JSON payload to lower camel case? 库类的JSON蛇案 - JSON snake case for library class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM