I have 2 services running(Frontend, Backend). Frontend service has Feign Client to connect to Backend API service. When a request hits Frontend, through Feign client it hits Backend API.
I have an API endpoint GET /api/v1/person which returns response like below format
{
"firstName": "stack"
"lastName": "overflow",
"address" : {
"address1" : "xyz",
"address2" : "abc street",
"postalcode": "123456"
}
}
The data for the address object is populated from external API as a JSON string. But the keys are in a different format, so I am using @JsonProperty annotation in setters and getters to convert them properly.
class Person {
private String firstName;
private String lastName;
private Address address;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setAddress(Address address) {
this.address= address;
}
public void getAddress() {
return this.address;
}
}
public class Address {
private String address1;
private String address2;
private String postalCode;
@JsonProperty("address1")
public String getAddress1() {
return address1;
}
@JsonProperty("ADD_ONE")
public void setAddress1(String address1) {
this.address1 = address1;
}
@JsonProperty("address2")
public String getAddress2() {
return address2;
}
@JsonProperty("ADD_TWO")
public void setAddress2(String address2) {
this.address2 = address2;
}
@JsonProperty("postalCode")
public String getPostalCode() {
return postalCode;
}
@JsonProperty("PST_CDE")
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
}
From the Backend API, the response looks good and as expected. But when it reached Frontend service, the address object returned as null/empty as below
{
"firstName": "stack"
"lastName": "overflow",
"address" : {}
}
Instead of using @JsonProperty in the Address class, if I rename the JSON keys and convert the JSON string to a java object, I am getting the expected response.
I am not sure why it returns empty or null when I use the @JsonProperty in the Address class and pass the response from the Backend to the Frontend service.
Note: Response object is common for both services.
Can someone please let me know, what I am missing here?
Thanks.
The issue is resolved when I added a new Address class for the Frontend service.
Frontend Service - with no annotations set.
public class Address {
private String address1;
private String address2;
private String postalCode;
public String getAddress1() {
return address1;
}
public void setAddress1(String address1) {
this.address1 = address1;
}
public String getAddress2() {
return address2;
}
public void setAddress2(String address2) {
this.address2 = address2;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
}
Backend Service - with @JsonProperty for setters and getters to convert the JSON keys.
public class Address {
private String address1;
private String address2;
private String postalCode;
@JsonProperty("address1")
public String getAddress1() {
return address1;
}
@JsonProperty("ADD_ONE")
public void setAddress1(String address1) {
this.address1 = address1;
}
@JsonProperty("address2")
public String getAddress2() {
return address2;
}
@JsonProperty("ADD_TWO")
public void setAddress2(String address2) {
this.address2 = address2;
}
@JsonProperty("postalCode")
public String getPostalCode() {
return postalCode;
}
@JsonProperty("PST_CDE")
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
}
Since the Address class was Common for both the services, the issue occurred, where the Frontend service was not able to convert the object since the Address class attribute names are different(used @JsonProperty for setter method).
Please comment if there is anything that I missed.
Thanks all for your proper responses.