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 Service返回的示例XML
<Entries xmlns="myNamespace">
<Entry><devicename xmlns="myNamespace">Device1</devicename></Entry>
<Entry><devicename xmlns="myNamespace">Device2</devicename></Entry>
</Entries>
它似乎正确地拉入数据,但每个设备名称都返回null。
答案 0 :(得分:1)
您可以在名为@XmlSchema
的类上使用包级别package-info
注释,而不是您当前如何映射命名空间限定条件,如下所示:
<强> package-info.java 强>
@XmlSchema(
namespace = "myNamespace",
elementFormDefault = XmlNsForm.QUALIFIED)
package your.pkg;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
了解更多信息