9

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

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.

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

{
   "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

{
   "id": 0,
   "name_bangla": "string",
   "name_english": "string"
}
Md. Shougat Hossain
  • 501
  • 2
  • 8
  • 24

2 Answers2

22

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 has a setting called PropertyNamingStrategy.SNAKE_CASE which you can set for the jackson objectmapper.

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

@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...

Adding this to your application.yml should also work:

spring.jackson.property-naming-strategy=SNAKE_CASE
Kenny Cason
  • 12,109
  • 11
  • 47
  • 72
Indivon
  • 1,784
  • 2
  • 17
  • 32
  • Thank you for your help. I just add `property-naming-strategy: CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES` to my `application.yml` file in `spring:jackson:` section. Now the output is fine. But I find some error in my log in the beginning of the run. The errors looks like `java.lang.NoSuchMethodException: com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder.(com.fasterxml.jackson.databind.PropertyName, com.fasterxml.jackson.databind.AnnotationIntrospector, boolean)` can you help me to overcome this error? @Indivon – Md. Shougat Hossain Nov 24 '16 at 12:30
  • 1
    Notice that CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES is deprecated in favour of SNAKE_CASE. The no such method exception could occur, if you have either a wrong version, or you use e.g. "JsonProperty" or other Jackson-Annotations from a wrong package. There is com.fasterxml.* and org.springframework.cloud.* in a jhipster app. May be, this could be the problem... – Indivon Nov 24 '16 at 12:36
11

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

Little example in Kotlin:

@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy::class)
data class Specialization(val altUrl: String, val altId: Int, val altName: String)
Sonique
  • 6,670
  • 6
  • 41
  • 60