Reference Guide

Table Of Contents
Implementation of the REST API is located in the hm-rs module. Now, create the Switch REST API
which is named SwitchResource The suffix Resource is used to denote REST web services. The
following listing shows an extract of the resource. For the moment use fake data, in later
information replace the fake implementations by more realistic ones. In order to implement REST
web services the module needs to declare some dependencies. Open the hm-rs/pom.xml file and
add the XML extract from the REST Module Dependencies listing to the <dependencies> node;
after updating the POM file update the Eclipse project dependencies (see Updating Project
Dependencies on page 146).
SwitchResource.java (REST API):
package com.hp.hm.rs;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.hp.sdn.rs.misc.ControllerResource;
...
@Path("switches")
public class SwitchResource extends ControllerResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getAll() {
return ok("{\”switches\”:[]}").build();
}
@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response get(@PathParam("id") long id) {
return ok("{\”switch\”:{}}").build();
}
@POST
@Produces(MediaType.APPLICATION_JSON)
public Response add(String request) {
return ok("{\”switch\”:{}}").build();
}
@DELETE
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
170