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.
The example JAX-RS application to embed for this tutorial is located here.
It consists of the following resource:
@Path("/jaxrs") public class JaxRsResource { private @Inject JaxRsDependency dependency; @GET @Path("/inject") public String inject() { return "Inject " + this.dependency.getMessage(); } @GET @Path("/path/{param}") public String pathParam(@PathParam("param") String param) { return param; } @POST @Path("/json") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public JsonResponse json(JsonRequest request) { return new JsonResponse(request.getInput()); }
with the dependency:
public class JaxRsDependency { public String getMessage() { return "Dependency"; } }
To integrate JAX-RS functionality into OfficeFloor, include the following maven dependency:
<dependency> <groupId>net.officefloor.spring</groupId> <artifactId>officespring_jaxrs</artifactId> </dependency>
Then to have the JAX-RS application end points be available, include the following maven dependency (see the WAR tutorial for more information):
<dependency> <groupId>net.officefloor.jee</groupId> <artifactId>officeservlet_war</artifactId> </dependency>
This will load the web.xml of the JAX-RS WAR:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>Jersey</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>net.officefloor.tutorial.jaxrsapp.JaxRsApplication</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>Jersey</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
That runs the JAX-RS application:
@ApplicationPath("/") public class JaxRsApplication extends ResourceConfig { public JaxRsApplication() { this.register(JaxRsResource.class); this.register(new JaxRsBinder()); } }
JAX-RS can bind in its own dependencies. Such as the case of the embedded application for this tutorial:
public class JaxRsBinder extends AbstractBinder { @Override protected void configure() { this.bind(JaxRsDependency.class).to(JaxRsDependency.class); } }
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.
@RegisterExtension public static final MockWoofServerExtension server = new MockWoofServerExtension() .property(OfficeFloorWar.PROPERTY_WAR_PATH, getWarPath()); @Test public void inject() { MockWoofResponse response = server.send(MockWoofServer.mockRequest("/jaxrs/inject")); response.assertResponse(200, "Inject Dependency"); } @Test public void pathParam() { MockWoofResponse response = server.send(MockWoofServer.mockRequest("/jaxrs/path/parameter")); response.assertResponse(200, "parameter"); } @Test 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.