【发布时间】:2014-03-12 18:34:34
【问题描述】:
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
public class App {
public static void main(String[] args){
Client client = Client.create();
WebResource webResource = client.resource("https://mywebsite.com/getDevices");
ClientResponse response = webResource.accept("application/xml").get(
ClientResponse.class);
System.out.println(response.getEntity(Devices.class));
}
}
Devices.java
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace="myNamespace", propOrder = {"deviceList"})
@XmlRootElement(name = "Entries", namespace="myNamespace")
public class Devices {
@XmlElement(name = "Entry", namespace="myNamespace")
protected List<Devices.Device> deviceList;
public List<Devices.Device> getEntry() {
if (deviceList == null) {
deviceList = new ArrayList<Devices.Device>();
}
return this.deviceList;
}
@Override
public String toString() {
return deviceList.toString();
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {"devicename"})
public static class Device {
String devicename;
public String getDevicename() {
return devicename;
}
public void setDevicename(String value) {
this.devicename = value;
}
@Override
public String toString() {
return devicename;
}
}
}
从 Web 服务返回的示例 XML
<Entries xmlns="myNamespace">
<Entry><devicename xmlns="myNamespace">Device1</devicename></Entry>
<Entry><devicename xmlns="myNamespace">Device2</devicename></Entry>
</Entries>
它似乎正确地提取了数据,但为每个设备名返回 null。
【问题讨论】: