JAX RS get list of objects

1000 views Asked by At

Trying to play with JAX RS i want to implement CRUD operations with my data. First of all i want to get list of objects in json formate.

@Path("/users")
public class ListUsersRestController {

    @GET
    @Produces("application/json")
    public List<User> getUsers(){

        List<User> users = new ArrayList<>();
        users.add(new User("Dean", "Winchester"));
        users.add(new User("Sam", "Winchester"));
        users.add(new User("Bobby", "Singer"));
        return users;
    }

    @XmlRootElement
    public class User {

        @XmlElement(name="first-name")
        private String firstName;

        @XmlElement(name="last-name")
        private String lastName;

        public User(){

        }

        public User(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }

    }
}

When i request my URI i always get 500 server error and there are no any errors in log file (I am using glassfish).

1

There are 1 answers

0
mkrakhin On BEST ANSWER

The problem is here:

@XmlRootElement
public class User {
    ...
}

Your User class now is an inner class of ListUsersRestController and it seems that JAXB fails to marshall inner classes (because they are more like an instance member of ListUsersRestController than a real class). Either externalize it to be a normal class or make it static:

@XmlRootElement
public static class User {
    ...
}