2

I am trying to unmarshal JAXB in client side, but I am getting Object's properties NULL.

Thats what I am doing to unmarshal

    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();

Pls Help !!!

user1534466
  • 139
  • 2
  • 4
  • 8

2 Answers2

0

Check that your class is serialized correctly and annotated with @XmlRootElement and @XmlElement.

Check this example and this other one. Another full example.

Put some assertions to check not null objects and list not empty.

Finally if you are using spring 3 I don't understand why you are managing connections and input streams... Try an approach using controller, maybe you need a custom unmarshaller:

@RequestMapping(method=RequestMethod.GET, value="/myurl")
public ModelAndView getUsers(@RequestBody List<LDAPUsers> users) {
Community
  • 1
  • 1
jmventar
  • 687
  • 1
  • 11
  • 32
0

You can set an instance of ValidationEventhandler on the Unmarshaller to be notified of any problems that occur during the unmarshal process. This can help in debugging the problem:

    jaxbUnmarshaller.setEventHandler(new ValidationEventHandler() {

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

    });

Client Example

bdoughan
  • 147,609
  • 23
  • 300
  • 400