1
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.javaJAXB編組沒有嵌套的對象與新澤西
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;
}
}
}
樣品從XML Web服務
<Entries xmlns="myNamespace">
<Entry><devicename xmlns="myNamespace">Device1</devicename></Entry>
<Entry><devicename xmlns="myNamespace">Device2</devicename></Entry>
</Entries>
回到它似乎在數據正確地拉,但每個返回null設備名稱。
完美。有趣的是,我昨天讀了確切的博客,並在Eclipse中出現了一些錯誤。今天運行良好。非常感謝。 –