Migrate JAX-RS Tutorial
This tutorial provides the typical steps in migrating a JAX-RS resource to avoid dependency on JAX-RS. It also enables simpler code that can take advantage of all the OfficeFloor features.
Steps to migrate a JAX-RS resource
The JAX-RS resource to be migrated is taken from the previous tutorial:
@Path("/jaxrs")
public class JaxRsResource {
private @Inject JaxRsDependency dependency;
@GET
public String get() {
return "GET " + this.dependency.getMessage();
}
@GET
@Path("/path/{param}")
public ResponseModel path(@PathParam("param") String param) {
return new ResponseModel(param);
}
@PUT
@Path("/update")
@Consumes("application/json")
@Produces("application/json")
public ResponseModel post(RequestModel request) {
return new ResponseModel(request.getInput());
}
}
To migrate a JAX-RS resource:
- Replace the JAX-RS parameter annotations with WoOF annotations.
- @PathParam to @HttpPathParameter
- @QueryParam to @HttpQueryParameter
- @HeaderParam to @HttpHeaderParameter
- @CookieParam to @HttpCookieParameter
- Request body object's class to be annotated with @HttpObject
- Send response object to ObjectResponse parameter rather than returning it.
- Note that can also continue to return object. This object is then used as a parameter to the next linked procedure.
- Remove the remaining JAX-RS annotations.
- Move dependencies to parameters of the method.
The resulting migrated code is as follows:
public class MigratedResource {
public void get(JaxRsDependency dependency, ServerHttpConnection connection) throws IOException {
connection.getResponse().getEntityWriter().write("GET " + dependency.getMessage());
}
public void path(@HttpPathParameter("param") String param, ObjectResponse<ResponseModel> response) {
response.send(new ResponseModel(param));
}
public void post(RequestModel request, ObjectResponse<ResponseModel> response) {
response.send(new ResponseModel(request.getInput()));
}
}
Next
The next tutorial covers embedding WoOF inside an existing HttpServlet container.

