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).
The problem is here:
Your
User
class now is an inner class ofListUsersRestController
and it seems that JAXB fails to marshall inner classes (because they are more like an instance member ofListUsersRestController
than a real class). Either externalize it to be a normal class or make itstatic
: