Restful API using Jersey 3 Jakarta EE 9
This article walks through a sample REST application built using Jersey3 and then we will see how to test the rest endpoints. Last not but least how to deploy it on Server.
Project Structure
Technologies Used
- Java 15
- Mvn 3.5.0
- Jersey 3.0.2
- JUnit 5.3.1
Java Classes
JakartaRestfulApp.java
Main Jersey Application Class, registers MenuResource class.
public class JakartaRestfulApp extends Application {
private final Set> classes;
public JakartaRestfulApp() {
HashSet> c = new HashSet>();
c.add(MenuResource.class);
classes = Collections.unmodifiableSet(c);
}
@Override
public Set> getClasses() {
return classes;
}
}
MenuResource.java
Our Resourse class which exposes Restful endpoints for the Menu object. It uses MenuService to store and retrieve Menu items.
@Path("/rest")
public class MenuResource {
MenuService service = MenuService.getInstance();
@GET
@Path("/menus")
@Produces("application/json")
public List
MenuService.java
This class is used to store and retrieve Menu objects. It uses a HashSet to store objects created.
public class MenuService {
private static MenuService instance = new MenuService();
private static HashSet
Menu.java
Menu object representing a restaurant menu containing some basic information like id and name.
public class Menu {
private Long id;
private String name;
public Menu() {
}
public Menu(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
JDKSEServer.java
Jersey supports a number of deployment options. For this example, we will use a lightweight JDK server that comes with JDK.
public class JDKSEServer {
private static final Logger LOGGER = Logger.getLogger(JDKSEServer.class.getName());
/**
* Starts the JDK HTTP server serving the JAX-RS application.
*/
static HttpServer startServer() throws IOException {
ResourceConfig config = new ResourceConfig(MenuResource.class);
HttpServer server = JdkHttpServerFactory.createHttpServer(getBaseURI(), config);
return server;
}
public static void main(String[] args) {
try {
final HttpServer httpServer = startServer();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
LOGGER.info("Shutting down the application...");
httpServer.stop(0);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, null, e);
}
}));
LOGGER.info("Application started.%nStop the application using CTRL+C");
Thread.currentThread().join();
} catch (Exception ex) {
LOGGER.log(Level.SEVERE, null, ex);
}
}
/**
* Gets base {@link URI}.
*/
public static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost/").port(8080).build();
}
}
Unit Testing
TestMenuResource.java
We will use JerseyTest to test some of the methods that MenuResource supports.
public class TestMenuResource extends JerseyTest {
@BeforeEach
void init() throws Exception {
super.setUp();
}
@Override
protected Application configure() {
//to start a new container in s different port
forceSet(TestProperties.CONTAINER_PORT, "0");
return new ResourceConfig(MenuResource.class);
}
@Test
public void get_one() throws Exception {
Response response = target("/rest/menus/1").request().get();
System.out.println("sd " + response.getStatus());
assertEquals(response.getStatus(), 200);
assertEquals(response.readEntity(Menu.class).getName(), "Menu One");
}
protected TestContainerFactory getTestContainerFactory() {
return new JdkHttpServerTestContainerFactory();
}
}
Executing Code
mvn clean compile exec:java
Sep 24, 2021 4:57:57 PM org.glassfish.jersey.server.wadl.WadlFeature configure
WARNING: JAX-B API not found . WADL feature is disabled.
Sep 24, 2021 4:57:57 PM jersey.server.JDKSEServer main
INFO: Application started.%nStop the application using CTRL+C
References
Summary
In this article, we saw how to build and test Jakarta Restful API with Jersey 3.