I have this DTO which I'm using for request and response DTO between two microservices:
HttpEntity<ProfileResource> requestEntity = new HttpEntity<>(request);
ResponseEntity<ProfileResource> response = null;
try {
response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, ProfileResource.class,
Collections.emptyMap());
} catch (Exception ex) {.....}
DTO:
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NON_PRIVATE)
@JsonIgnoreProperties(ignoreUnknown = false)
public class ProfileResource {
private String firstName;
private Error error;
.......
}
............
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Error implements ErrorCode {
private String code;
}
Into receiving microservices I get error:
org.springframework.web.client.HttpClientErrorException$BadRequest: 400 : "{"error":{"code":"5023","message":"You submitted a request that is not parseable.: field 'error' not recognized",
Do you know how I can fix this issue? Maybe the solution is to ignore the value error
?
Solution:
I managed to fix the issue by adding:
@JsonInclude(JsonInclude.Include.NON_NULL)
private Error error;
@JsonIgnoreProperties(ignoreUnknown = false) is giving this error. You are tightly coupling the API response with what you are expecting Change it to
@JsonIgnoreProperties(ignoreUnknown = true)
public class ProfileResource {
Also make sure Error class has this
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Error