Quick code example:
1) Add the javax.ws.rs dependency in your pom (if using Maven) or download it.
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>
2) Create an empty class to define the path of your service; for example for hearing at application/service/rest
would be
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/service/rest")
public class WebConfig extends Application {
}
3) Create the controller of your api. For example if we need these calls:
application/service/rest/resource/{id}
a simple code would be:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
@Path("resource/{id}")
public class ApiController {
/**
* Call: <code>/service/rest/resource/23</code>
* @return HTTP Response
*/
@GET
public Response getResource(@PathParam("id") String anId) {
Resource myResource = whatever.get(anId);
return Response.status(Status.OK).entity(myResource).build();
}
4) If we want to specify a JSON response be sure you have the getters for your resource and type:
@GET
@Produces("application/json")
public Response getResource(@PathParam("id") String anId) {
// the same
}