How do you configure RESTTemplate to deserialize the response Json using Jackson's @JsonDeserialize?
My domain classes based on Builder Pattern and jackson's @JsonDeserialize marshall and unmarshall fine in unit tests. However, when used in conjunction with Spring's RESTTemplate (Spring 3.1) it fails.
Domain class:
@JsonDeserialize(builder = Policy.Builder.class)
public final class Policy implements Comparable, Serializable {
// immutable attributes
private Policy(Builder builder) {
...
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Builder {
/// withXXX methods
public Policy build() {
return new Policy(this);
}
}
}
Spring RESTTemplate code in Unit test:
Policy policy = new Policy.Builder().withXXX()...build();
restTemplate.postForObject("http://localhost:8080/policies/policy.json", policy, String.class);
Error in RestTemplate#doExecute() - line 436:
org.springframework.web.client.HttpServerErrorException: 500 Could not instantiate bean class [xxx.domain.Policy]: No default constructor found; nested exception is java.lang.NoSuchMethodException: xxx.domain.Policy.()
if a constructor is already defined you need to include the default constructor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With