我正在尝试在客户端对JAXB进行解组,但得到了对象的属性NULL
这就是我正在做的去分解
URL url = new URL("http://localhost:9191/service/firstName/tony?format=xml");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/atom+xml");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
JAXBContext jaxbContext = JAXBContext.newInstance(LDAPUsers.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
LDAPUsers lu = (LDAPUsers) jaxbUnmarshaller.unmarshal(br);
ArrayList<LDAPUser> list = new ArrayList<LDAPUser>();
//list.addAll(lu.getCounty());
**System.out.println(lu.ldapUser.get(0).getFirstName());//this is giving NULL**
conn.disconnect();
请帮忙!!!
检查您的类是否正确序列化,并使用@XmlRootElement和@XmlElement 检查这个例子和另一个例子。又是一个完整的例子。 放置一些断言以检查是否为空对象和列表是否为空。 最后,如果您使用的是spring3,我不明白您为什么要管理连接和输入流。。。尝试一种使用控制器的方法,也许你需要一个自定义的解组器:@RequestMapping(method=RequestMethod.GET, value="/myurl")
public ModelAndView getUsers(@RequestBody List<LDAPUsers> users) {
您可以在Unmarshaller
上设置ValidationEventhandler
的实例,以便在解组过程中收到任何问题的通知。这可以帮助调试问题:
jaxbUnmarshaller.setEventHandler(new ValidationEventHandler() {
@Override
public boolean handleEvent(ValidationEvent event) {
System.out.println(event.getMessage());
return true;
}
});
客户端示例
- http://blog.bdoughan.com/2010/08/creating-restful-web-service-part-55.html