Spring Rest Service调用期间的解组问题

时间:2016-05-09 09:51:18

标签: spring rest spring-rest xml-binding

使用Spring Rest Template调用Rest Web服务,如下所示 -

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

并以String格式获取输出

<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

}   

获取info类的所有值,如Firstname = null ..

有谁能说出缺少的东西? 感谢

1 个答案:

答案 0 :(得分: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

}