I am using MobileFirst platform Foundation 7.0.

I created a "Test" Java adapter that provides a REST API and returns a JAXB annotated Object.

When I test the api with JSON format it is working fine, but when I change the content type to application/xml I get "500 internal exception" error and in the console i see the following exception:

The system cannot marshal the model.UserManager JAXB object into XML content. Verify that the JAXB object is valid.

This is my User and UserManager POJOs:

@XmlRootElement(name="newuser")
@XmlAccessorType(XmlAccessType.NONE)
public class UserNew {

    @XmlElement
    String id;

    @XmlElement
    String name;

    @XmlElement
    String email;

    @XmlElement
    String password;


    public UserNew(String id, String name, String email, String password) {
        super();
        this.id = id;
        this.name = name;
        this.email = email;
        this.password = password;
    }

    // getter ,setter equals and hashCode follows.

This is UserManager:

@XmlRootElement
public class UserManager {

    @XmlElement
    String managerId;
    @XmlElementWrapper(name="users")
    ArrayList<UserNew> users;
    public UserManager(String managerId, ArrayList<UserNew> users) {
        super();
        this.managerId = managerId;
        this.users = users;
    }

    // getter ,setter equals and hashCode follows.

This is my REST API method in the Java adapter:

@GET
@Path("/getUser")
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XHTML_XML})
public UserManager getUser(){
    UserNew user1 =  new UserNew("101","user1","[email protected]","password!");
    UserNew user2 =  new UserNew("102","user2","[email protected]","password!");
    ArrayList<UserNew> userList = new ArrayList<>();
    userList.add(user2);
    userList.add(user1);

    return new UserManager("manager10101",userList);
}
1

There are 1 answers

0
Sarfaraz Khan On

You'll have to do 2 things, first you'll have to add no argument constructor in both the classes that is mandatory for Jaxb marshaling and unmarshaling. Second thing is you need to add @XmlRootElement(name="usermanager") in UserManager class. I've tried the following after that its serializing the objects.

@XmlRootElement(name="newuser")
@XmlAccessorType(XmlAccessType.NONE)
public class UserNew {

    @XmlElement
    String id;

    @XmlElement
    String name;

    @XmlElement
    String email;

    @XmlElement
    String password;
    public UserNew(){

    }

    public UserNew(String id, String name, String email, String password) {
        super();
        this.id = id;
        this.name = name;
        this.email = email;
        this.password = password;
    }
// getter ,setter equals and hashCode follows.



@XmlRootElement(name="usermanager")
@XmlAccessorType(XmlAccessType.NONE)
public class UserManager {

    @XmlElement
    String managerId;

    @XmlElementWrapper(name="users")
    ArrayList<UserNew> users;

    public UserManager() {
    }

    public UserManager(String managerId, ArrayList<UserNew> users) {
        super();
        this.managerId = managerId;
        this.users = users;
    }
  // getter ,setter equals and hashCode follows.

This the UserManager object that I serialized

  UserNew user1 =  new UserNew("101","user1","[email protected]","password!");
            UserNew user2 =  new UserNew("102","user2","[email protected]","password!");
            ArrayList<UserNew> userList = new ArrayList<>();
            userList.add(user2);
            userList.add(user1);
            UserManager um= new UserManager("manager10101",userList);

Got the below output after serialization

<UserManager>
<managerId>manager10101</managerId>
<users>
    <users>
        <id>102</id>
        <name>user2</name>
        <email>[email protected]</email>
        <password>password!</password>
    </users>
    <users>
        <id>101</id>
        <name>user1</name>
        <email>[email protected]</email>
        <password>password!</password>
    </users>
</users>