如何配置 RESTTemplate 以使用 Jackson 的 @JsonDeserialize 反序列化响应 Json?
我的域类基于构建器模式和 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.()
最佳答案
如果已经定义了构造函数,则需要包含默认构造函数。
关于spring-mvc - 如何使用 Builder Pattern 和 @JsonDeserialize 使用 Spring RestTemplate 反序列化响应 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13457985/