Issue
So I have a DTO like this,
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder
@ToString
@JsonIgnoreProperties(ignoreUnknown=true)
public class PersonDTO {
private String name;
private String address;
private String phone;
private String other;
}
I am passing the following payload to an external API
{
"name": "John",
"phone": "123",
"address": "abc"
}
This is throwing me 500 internal error:
HttpClientErrorException$NotFound: 404: "{<EOL> "status": "Error", <EOL> "message": "Error validating JSON. Error: - Invalid type Null, expected String."}"
If I comment out other
property in DTO then I will successfully get 200 response. I thought @JsonIgnoreProperties
annotation is supposed to ignore the properties that are not present but it seems that's not working.
Solution
The error says that they can't parse Null
into String
. That's very interesting error and fooled me first.
Your json object with other field null
probably looks like this:
{
"name": "John",
"phone": "123",
"address": "abc"
}
So somehow this API can't just wrap quotes around null
and tells you it's not able to parse this.
Therefore I guess you need to set the value of other
to an empty string:
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder
@ToString
@JsonIgnoreProperties(ignoreUnknown=true)
public class PersonDTO {
private String name;
private String address;
private String phone;
private String other = ""; //empty because external api can't handle null
}
Answered By - Ausgefuchster
Answer Checked By - Clifford M. (JavaFixing Volunteer)