What is JAX-RS
I love learning about technology and sharing that with others
Overview
- there are multiple ways to implement RESTFUL API in java we can use Spring or we can use JAX-RS
- JAX-RS is a specification , set of interfaces and annotations offered by JAVA EE.
Lightweight Wars
Always know your Server
- Glassfish comes with Jersey
- Wildfly or jboss comes with RESTEasy.
Annotations
@GET and @POST
@Path
@PathParam
@Consumes
- it is like an input to a method , it tells what media type the HTTP method takes in
@Produces
- It is like an output to a method , means media type produced by the method
Note
- It is good practice to add both for each method
Code Sample
@GET
@Path("/get/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response getNotification(@PathParam("id") int id) {
return Response.ok()
.entity(new Notification(id, "john", "test notification"))
.build();
}
@POST
@Path("/post/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response postNotification(Notification notification) {
return Response.status(201).entity(notification).build();
}

