I have currently trying to learn the basics of Java REST
, using JAX-RS
.
Within the UserService
class (near bottom) of this example there is both an @GET
AND @PUT
method with the same @path
annotation:
@GET
@Path("/users")
@Produces(MediaType.APPLICATION_XML)
public List<User> getUsers() {
return userDao.getAllUsers();
}
and
@PUT
@Path("/users")
@Produces(MediaType.APPLICATION_XML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String createUser(@FormParam("id") int id,
@FormParam("name") String name,
@FormParam("profession") String profession,
@Context HttpServletResponse servletResponse) throws IOException {
User user = new User(id, name, profession);
int result = userDao.addUser(user);
if(result == 1) {
return SUCCESS_RESULT;
}
return FAILURE_RESULT;
}
How does the Program know which method to invoke, considering that they are both point at the same @path
?
Resource classes have methods
that are invoked
when specific HTTP
method requests
are made, referred to as resource methods
. In order to create Java methods that will be invoked with specific HTTP methods, a regular Java method must be implemented and annotated with one of the JAX-RS @HttpMethod
annotated annotations (namely, @GET, @POST, @PUT, and @DELETE).