好的,我遇到的问题是我可以使用RestTemplate成功进行发帖。该请求在服务器端成功完成。

但是,当要在客户端进行设置时,不会正确分配值。下面的第一类是应该由Post调用填充的客户端。第二类是服务器用来发送它的类。

我相信我的问题是由于服务器发送的JSON格式引起的。看起来像下面的样子。

{ "record":{"firstName":"Bill", "lastName":"Johnson", "role":6}}


Spring不能自动将此映射到客户端POJO。有什么办法可以解决而不必更改服务器端代码?

谢谢。

EmployeeResponse response = restTemplate.postForObject(uri, request, EmployeeResponse.class );

//(Client Side)
public class EmployeeResponse {

    private String firstName;
    private String lastName;
    private int role;


    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName= firstName;
    }
    public String getLastName() {
        return longKey;
    }

    public void setLastName(String lastName) {
        this.lastName= lastName;
    }
    public int getRole() {
        return role;
    }

    public void setRole(int role) {
        this.role = role;
    }
}

//(Server-Side)
public class EmployeeResponse {

    private EmployeeRecord record;

    public String getFirstName() {
        return record.getFirstName();
    }

    public String getLastName() {
        return record.getLastName();
    }

    public int getRole() {
        return record.getRole();
    }

    public ELAActivationResponse(EmployeeRecord record) {
        this.record = record;
    }

}

最佳答案

这是因为因为服务器响应是用根元素record包装的,而您的客户端对象没有,所以它无法封送响应。

您需要使用EmployeeResponse注释响应对象类(@JsonRootName(value = "record")

07-24 18:27
查看更多