Home > Back-end >  Rename fields in response dto. Java, Spring
Rename fields in response dto. Java, Spring

Time:04-11

I have the following question. I want to rename fields in JSON response. In my DTO fields are defined in camal case. In response json a want to use separate words with underscores. I tried to use @JsonPropperty annotation but it is not working. My DTO class is below:

package com.example.demo.dto.response;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;


import java.util.Date;
import java.util.HashSet;
import java.util.Set;
@Data
public class UserResponseDto {
    private String id;
    private String name;
    @JsonProperty("second_name")
    private String secondName;
    private String email;
    private String login;
    private String password;
    @JsonProperty("date_of_birth")
    private Date dateOfBirth;
    @JsonProperty("date_of_registration")
    private Date dateOfRegistration;
    private Set<RoleResponseDto> roles = new HashSet<>();
}

And the response is still in camal case:

{
        "id": "d6c0873b-166b-4d6f-90b4-91d9f0dfa0a0",
        "name": "Simon",
        "secondName": "Wilder",
        "email": "[email protected]",
        "login": "QMY",
        "password": "$2a$10$Lj3EctARwD34EInzmwsjjuTsKiOzKtI7Gf7pskUfxcPt1PNRDHF1m",
        "dateOfBirth": "1991-12-21T00:00:00.000 00:00",
        "dateOfRegistration": "2022-04-07T12:52:24.059 00:00",
        "roles": [
            {
                "id": "a9cc27ba-532a-456a-a9b5-ad14052190f8",
                "title": "ROLE_USER"
            },
            {
                "id": "684bf3b6-721d-494c-9c66-caeee44933d2",
                "title": "ROLE_ADMIN"
            }
        ]
    }

Do You have any ideas? Or it is a bug, or what? Everywhere it is written to use @JsonPropperty annotation.

CodePudding user response:

So there are 2 ways to do what you want but first make sure you are using @ResponseBody on all your Controller methods or make sure your controller classes are using @RestController.

  1. if you want the whole application to use the underscore case in the response body then add this to your configs:

Gson Method

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(createGsonHttpMessageConverter());
        super.addDefaultHttpMessageConverters(converters);
    }

    @Bean
    public Gson createGson() {
        return new GsonBuilder().disableHtmlEscaping()
                .serializeNulls()
                .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
                .create();
    }

    @Bean
    public GsonHttpMessageConverter createGsonHttpMessageConverter() {
        GsonHttpMessageConverter gsonConverter = new GsonHttpMessageConverter();

        gsonConverter.setGson(createGson());

        return gsonConverter;
    }
}

Reference

Jackson Method

spring.jackson.property-naming-strategy=SNAKE_CASE

Reference:

  1. if you want it on a single DTO then you can use

    @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)

Refercences:

  • Related