package org.superbiz.rest;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
@Path("/greeting")
public class GreetingService {
@GET
public String message() {
return "Hi REST!";
}
@POST
public String lowerCase(final String message) {
return "Hi REST!".toLowerCase();
}
}
Simple REST
Defining a REST service is pretty easy, simply ad @Path annotation to a class then define on methods the HTTP method to use (@GET, @POST, …).
The Code
The REST service: @Path, @GET, @POST
Here we have a simple REST, we annotate the class with @Path("/greeting") to indicate the route corresponding to class GreetingService. So we define message() as @GET and lowerCase() as @POST for this /greeting route. There we have a service, pretty simple huh?
Testing
Test for the JAXRS service
The test uses Arquillian with the TomEE embedded adapter, so the REST service runs in a real TomEE.
The @Deployment method creates on the fly the web application to deploy. Here it only contains the GreetingService class but you can add your REST Application and a web.xml too. testable = false runs the test methods as a client of the deployed application, and @ArquillianResource injects its base URL.
Finally to test it we use cxf client API to call the REST service in get() and post() methods.
The container is configured in src/test/resources/arquillian.xml: httpPort set to -1 starts TomEE on a random port.
package org.superbiz.rest;
import org.apache.cxf.jaxrs.client.WebClient;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.IOException;
import java.net.URL;
import static org.junit.Assert.assertEquals;
@RunWith(Arquillian.class)
public class GreetingServiceTest {
@ArquillianResource
private URL base;
@Deployment(testable = false)
public static WebArchive app() {
return ShrinkWrap.create(WebArchive.class).addClass(GreetingService.class);
}
@Test
public void get() throws IOException {
final String message = WebClient.create(base.toExternalForm()).path("greeting/").get(String.class);
assertEquals("Hi REST!", message);
}
@Test
public void post() throws IOException {
final String message = WebClient.create(base.toExternalForm()).path("greeting/").post("Hi REST!", String.class);
assertEquals("hi rest!", message);
}
}
Running
Running the example is fairly simple. In the simple-rest directory run:
$ mvn clean install
Which should create output like the following.
Running org.superbiz.rest.GreetingServiceTest
INFO: Starting TomEE from: /tmp/arquillian-apache-tomee-20908
INFO: Starting Servlet engine: [Apache Tomcat/11.0.25]
INFO: Starting ProtocolHandler ["http-nio-52756"]
INFO: Configuring enterprise application: /path/to/tomee/examples/simple-rest/target/tomee-embedded/0/5cfb3d73-fabd-4bc4-8550-88f254fe1095
INFO: Enterprise application "/path/to/tomee/examples/simple-rest/target/tomee-embedded/0/5cfb3d73-fabd-4bc4-8550-88f254fe1095" loaded.
INFO: Deployed Application(path=/path/to/tomee/examples/simple-rest/target/tomee-embedded/0/5cfb3d73-fabd-4bc4-8550-88f254fe1095)
INFO: REST Application: http://localhost:52756/5cfb3d73-fabd-4bc4-8550-88f254fe1095/ -> org.apache.openejb.server.rest.InternalApplication
INFO: Service URI: http://localhost:52756/5cfb3d73-fabd-4bc4-8550-88f254fe1095/greeting -> Pojo org.superbiz.rest.GreetingService
INFO: GET http://localhost:52756/5cfb3d73-fabd-4bc4-8550-88f254fe1095/greeting -> String message()
INFO: POST http://localhost:52756/5cfb3d73-fabd-4bc4-8550-88f254fe1095/greeting -> String lowerCase(String)
INFO: Undeploying app: /path/to/tomee/examples/simple-rest/target/tomee-embedded/0/5cfb3d73-fabd-4bc4-8550-88f254fe1095
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.613 s -- in org.superbiz.rest.GreetingServiceTest
Results:
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0