Jersey 3 Hello World Example

September 24, 2021 |

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 getMenus() {
		return service.getAll();
	}

	@GET
	@Path("/menus/{menu_ID}")
	@Produces("application/json")
	public Response getMenu(@PathParam("menu_ID") Long menuId) {
		try {
			return Response.ok(service.get(menuId)).build();
		} catch (Exception e) {
			return Response.serverError().status(HttpStatus.BAD_REQUEST_400.getStatusCode(), e.getMessage()).build();
		}
	}

	@POST
	@Path("/menus")
	@Produces("application/json")
	public Response add(Menu c) {
		service.add(c);
		return Response.ok(c).status(HttpStatus.CREATED_201.getStatusCode(), "Created").build();
	}

	@PUT
	@Path("/menus/{menu_ID}")
	@Produces("application/json")
	public Response update(@PathParam("menu_ID") Long menuId, Menu c) {
		try {
			return Response.ok(service.update(menuId,c.getName())).build();
		} catch (Exception e) {
			return Response.serverError().status(HttpStatus.BAD_REQUEST_400.getStatusCode(), e.getMessage()).build();
		}
	}

	@PATCH
	@Path("/menus/{menu_ID}")
	@Produces("application/json")
	public Response update2(@PathParam("menu_ID") Long menuId, @QueryParam("name") String name) {
		try {
			return Response.ok(service.update(menuId,name)).build();
		} catch (Exception e) {
			return Response.serverError().status(HttpStatus.BAD_REQUEST_400.getStatusCode(), e.getMessage()).build();
		}
	}

	@DELETE
	@Path("/menus/{menu_ID}")
	@Produces("application/json")
	public Response delete(@PathParam("menu_ID") Long menuId) {
		try {
			service.delete(menuId);
			return Response.ok().status(HttpStatus.OK_200.getStatusCode()).build();
		} catch (Exception e) {
			return Response.serverError().status(HttpStatus.BAD_REQUEST_400.getStatusCode(), e.getMessage()).build();
		}
	}

	@OPTIONS
	@Path("/menus")
	@Produces("text/plain")
	public String touch() {
		return "options";
	}

	@HEAD
	@Path("/menus")
	@Produces("text/plain")
	public String head() {
		return "head";
	}
}
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 menus;

	private MenuService() {
		menus = new HashSet<>();
		menus.add(new Menu(1L, "Menu One"));
	}

	public static MenuService getInstance() {
		return instance;
	}

	public void add(Menu menu) {
		menus.add(menu);
	}

	public List getAll() {
		return new ArrayList(menus);
	}

	public Menu get(Long id) throws Exception {
		Iterator it = menus.iterator();
		while (it.hasNext()) {
			Menu curr = (Menu) it.next();
			if (curr.getId() == id)
				return curr;
		}
		throw new Exception("Object not found");
	}

	public boolean delete(Long id) throws Exception {
		Iterator it = menus.iterator();
		while (it.hasNext()) {
			Menu curr = (Menu) it.next();
			if (curr.getId() == id) {
				it.remove();
				return true;
			}
		}
		throw new Exception("Object not found");
	}
	
	public Menu update(Long id, String update) throws Exception {
		Iterator it = menus.iterator();
		while (it.hasNext()) {
			Menu curr = (Menu) it.next();
			if (curr.getId() == id) {
				curr.setName(update);
				return curr;
			}
				
		}
		throw new Exception("Object not found");
	}
}
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.