0

使用 Spring Rest 模板调用 Rest Web 服务,如下所示:

ResponseEntity<String> response = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.GET, entity, String.class); 

并以字符串格式获取输出

<Info xmlns="http://schemas.test.org/2009/09/Tests.new" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<FirstName>FirstName</FirstName>
<LastName>LastName</LastName>
<TestGuid>Guid</TestGuid>
<TestUID>1</TestUID>
<Token>token</Token>
<TestUserID>14</TestUserID>
</Info>

尝试将其解组为 java 类时,如下所示

ResponseEntity<Info> response = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.GET, entity, Info.class)

Info 类定义为

@XmlRootElement(name = "Info", namespace = "http://schemas.test.org/2009/09/Tests.new")
public class Info implements Serializable{

    private String firstName;
    private String lastName;
    private String testGuid;
    private String testUID;
    private String token;
    private String testUserID;

    //getters and setter

}   

获取信息类 null 的所有值,例如 Firstname=null..

谁能告诉我缺少什么?谢谢

4

1 回答 1

0

你需要像这样映射你的java类

@XmlRootElement(name = "Info", namespace = "http://schemas.test.org/2009/09/Tests.new")
@XmlAccessorType(XmlAccessType.FIELD)
public class Info implements Serializable{

    @XmlElement(name="FirstName")
    private String firstName;
    @XmlElement(name="LastName")
    private String lastName;
    @XmlElement(name="TestGuid")
    private String testGuid;
    @XmlElement(name="TestUID")
    private String testUID;
    @XmlElement(name="Token")
    private String token;
    @XmlElement(name="TestUserID")
    private String testUserID;

    //getters and setter

}   
于 2016-05-09T10:55:12.727 回答