0

I am serializing json to object using Jackson and return it in API response; however, the json file contains different field names than I have in the object , they are not completely different , but there are "_" between words , but in Object field names the field is on word for example , in json file customer_mobile_no in object the filed name is customerMobileNo , however , I am using @JsonProperty annotation on field name, so that Jackson knows the name of the JSON field to map. as @JsonProperty annotation is used for both deserializing and serializing.

ResponseDto

    @Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class ResponseDto  {
@JsonProperty("customer_mobile_no")
private String customerMobileNo;
}

JsonFile

"customer_mobile_no" : 011111111

lines used to map from json to object

  List<ResponseDto> responseDto = mapper.readerForListOf(ResponseDto.class).readValue(json.toString());

the fields' name are returned in response as the same as in Json ; however , I want to return field name as the same as in object . I mean I get customer_mobile_no in response , but I want to get field name customerMobileNo : 0111

enter image description here

I have tried to use JsonSetter , but it overrides the field name as well .

Omar Abo Elsoud
  • 105
  • 1
  • 6

0 Answers0