如何配置RESTTemplate以使用Jackson的@JsonDeserialize反序列化响应Json?
我的基于Builder Pattern的域类和jackson的@JsonDeserialize marshall和unmarshall在单元测试中很好。但是,当与Spring的RESTTemplate(Spring 3.1)结合使用时,它会失败。
域类:
@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代码:
Policy policy = new Policy.Builder().withXXX()...build(); restTemplate.postForObject("http://localhost:8080/policies/policy.json", policy, String.class);
RestTemplate中的错误#doExecute() - 第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.()
答案 0 :(得分:2)
如果已经定义了构造函数,则需要包含默认构造函数。