【问题标题】:RestFul Service (spring3) CLIENT java?RestFul Service (spring3) CLIENT java?
【发布时间】:2012-08-23 23:04:34
【问题描述】:

我正在尝试在客户端解组 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();

请帮忙!!!

【问题讨论】:

  • 调试并在列表中使用断点,检查该列表不为空且对象已设置所有属性。
  • 它没有击中二传手...
  • 您的 XML 和域对象是什么样的?

标签: java xml json spring


【解决方案1】:

您可以在Unmarshaller 上设置ValidationEventhandler 的实例,以便在解组过程中出现任何问题时得到通知。这有助于调试问题:

    jaxbUnmarshaller.setEventHandler(new ValidationEventHandler() {

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

    });

客户端示例

【讨论】:

    【解决方案2】:

    检查您的类是否正确序列化并使用 @XmlRootElement@XmlElement 进行注释。

    检查this example 和这个other one。另一个full example

    添加一些断言来检查not null对象和list not empty

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-26
      • 1970-01-01
      • 2017-03-24
      • 2023-03-30
      • 1970-01-01
      • 2011-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多