繁体   English   中英

RestFul服务(spring3)客户端java?

[英]RestFul Service (spring3) CLIENT java?

我正在尝试在客户端解组JAXB,但是我正在获取Object的属性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进行注释。

检查这个例子另一个 例子 另一个完整的例子

放置一些断言以检查非null对象并列出not empty

最后,如果您使用的是Spring 3,我不明白您为什么要管理连接和输入流...尝试使用控制器的方法,也许您需要一个自定义的解组器:

@RequestMapping(method=RequestMethod.GET, value="/myurl")
public ModelAndView getUsers(@RequestBody List<LDAPUsers> users) {

您可以在Unmarshaller上设置ValidationEventhandler的实例,以在Unmarshaller过程中发生任何问题时得到通知。 这可以帮助调试问题:

    jaxbUnmarshaller.setEventHandler(new ValidationEventHandler() {

        @Override
        public boolean handleEvent(ValidationEvent event) {
            System.out.println(event.getMessage());
            return true;
        }

    });

客户实例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM