How to deserialize json with RestTemplate using Builder Pattern and @JsonDeserialize
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:
Code:
@JsonDeserialize(builder = Policy.Builder.class)
public final class Policy implements Comparable<Policy>, 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:
Code:
Policy policy = new Policy.Builder().withXXX()...build();
restTemplate.postForObject("http://localhost:8080/policies/policy.json", policy, String.class);
Error in RestTemplate#doExecute() - line 436:
Code:
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.<init>()