Fork me on GitHub

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.

Tutorial Source

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);
	}

	@POST
	@Path("/update")
	@Consumes("application/json")
	@Produces("application/json")
	public ResponseModel post(RequestModel request) {
		return new ResponseModel(request.getInput());
	}
}

To migrate a JAX-RS resource:

  1. Replace the JAX-RS parameter annotations with WoOF annotations.
  2. Request body object's class to be annotated with @HttpObject
  3. 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.
  4. Remove the remaining JAX-RS annotations.
  5. 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 creating quick prototype applications.