Fork me on GitHub

JAX-RS Tutorial

This tutorial demonstrates embedding a JAX-RS application into OfficeFloor.

The reason for embedding is to enable a phased migration to OfficeFloor. Forcing a rewrite of the application into OfficeFloor is typically expensive (plus falls under the anti-pattern of "big bang"). Therefore, the JAX-RS application can be embedded in OfficeFloor and functionality slowly migrated out of it to take advantage of OfficeFloor's IoCC.

Tutorial Source

JAX-RS Application

The example JAX-RS application to embed for this tutorial is located here.

It consists of the following resource:

public class JaxRsResource {

	private @Inject JaxRsDependency dependency;

	public String inject() {
		return "Inject " + this.dependency.getMessage();

	public String pathParam(@PathParam("param") String param) {
		return param;

	public JsonResponse json(JsonRequest request) {
		return new JsonResponse(request.getInput());

with the dependency:

public class JaxRsDependency {

	public String getMessage() {
		return "Dependency";

Embedding JAX-RS

To integrate JAX-RS functionality into OfficeFloor, include the following maven dependency:


Then to have the JAX-RS application end points be available, include the following maven dependency (see the WAR tutorial for more information):


This will load the web.xml of the JAX-RS WAR:

<web-app xmlns="" xmlns:xsi=""



That runs the JAX-RS application:

public class JaxRsApplication extends ResourceConfig {

	public JaxRsApplication() {
		this.register(new JaxRsBinder());

JAX-RS Dependencies

JAX-RS can bind in its own dependencies. Such as the case of the embedded application for this tutorial:

public class JaxRsBinder extends AbstractBinder {

	protected void configure() {

Dependencies are also injected from OfficeFloor. To enable OfficeFloor dependencies to be available to JAX-RS, the JAX-RS implementation Servlet / Filter must be loaded on start-up.


The following tests demonstrate embedding the JAX-RS application and invoking each end point.

	public static final MockWoofServerExtension server = new MockWoofServerExtension()
			.property(OfficeFloorWar.PROPERTY_WAR_PATH, getWarPath());

	public void inject() {
		MockWoofResponse response = server.send(MockWoofServer.mockRequest("/jaxrs/inject"));
		response.assertResponse(200, "Inject Dependency");

	public void pathParam() {
		MockWoofResponse response = server.send(MockWoofServer.mockRequest("/jaxrs/path/parameter"));
		response.assertResponse(200, "parameter");

	public void post() {
		MockWoofResponse response = server
				.send(MockWoofServer.mockJsonRequest(HttpMethod.POST, "/jaxrs/json", new JsonRequest("INPUT")));
		response.assertJson(200, new JsonResponse("INPUT"));


The next tutorial covers using JAX-RS end point methods as procedures.