Reference Guide

Table Of Contents
Now that the JSON factory is in place, update the SwitchResource to use the JsonService to
encode and decode Switch objects. The SwitchResource.java Using JSON Codecs listing shows a
modification of SwitchResource that uses a JSON codec to encode Switch objects.
SwitchResource.java Using JSON Codecs:
package com.hp.hm.rs;
import com.hp.sdn.json.JsonService;
...
@Path("switches")
public class SwitchResource extends ControllerResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response list() {
SwitchService service = get(SwitchService.class);
List<Switch> switches = service.find(null, null);
JsonService jsonService = get(JsonService.class);
String result = jsonService.toJsonList(switches, Switch.class, true);
return ok(result).build();
}
...
}
JsonService Module Dependencies:
<dependency>
<groupId>com.hp.sdn</groupId>
<artifactId>sdn-cmmon-api</artifactId>
<version>${sdn.version}</version>
</dependency>
The following listing, SwitchResourceTest.java Using JSON Codecs, is a modification of the
SwitchResourceTest.java that also uses JsonService to complete the tests.
SwitchResourceTest.java Using JSON Codecs:
package com.hp.hm.rs;
import com.hp.sdn.json.JsonService;
...
public class SwitchResourceTest extends ControllerResourceTest {
private static final String BASE_PATH = "switches";
private SwitchService switchServiceMock;
private JsonService jsonServiceMock;
public SwitchResourceTest() {
187