<dependencies> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> <version>2.25.1</version> </dependency> </dependencies>This dependency is required to develop RESTful web services in Java, using Jersey framework – an implementation of Java API for RESTful Web Services (JAX-RS).Use the version 2.25.x if your Tomcat running on JDK 8. In case you use JDK 11 or later, you should use newer version, e.g. Jersey 2.29.1 like this:
<dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> <version>2.29.1</version> </dependency> <dependency> <groupId>org.glassfish.jersey.inject</groupId> <artifactId>jersey-hk2</artifactId> <version>2.29.1</version> </dependency>For Jersey 2.26.x or newer, you must also declare the Jersey Inject dependency as shown above. So let use the Jersey version 2.29.1 because it works well both on JDK 8 and recent JDK versions (JDK 11 or newer).
package net.codejava; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("/bonjour") public class HelloResource { @GET @Produces(MediaType.TEXT_PLAIN) public String direBonjour() { return "Bonjour, tout le monde!"; } }Look, this is your first class for RESTful web services. Let me explain:The @Path annotation defines the relative URL that forms the URI that identifies a resource. You can use this annotation on both class level or method level.The @GET annotation specifies that the annotated method, direBonjour() handles HTTP GET request. Jersey provides annotations corresponding to HTTP methods: @POST, @PUT, @DELETE…The @Produces annotation specifies the content type of the response, which is text plain in our code. You can also specify text/xml, text/html, JSON, etc.And you can see the method direBonjour() returns a plain text, saying hello world in French – as response to the clients.
<servlet> <servlet-name>Jersey REST Service</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>net.codejava</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Jersey REST Service</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping>Note that we need to specify the package name that contains the classes that need to be exposed as RESTful web services, as an initialization parameter of Jersey servlet; and the URL pattern will be handled by Jersey servlet container.Now, you can add this project to Tomcat and start the server to test the web service.
http://localhost:8080/HelloREST/rest/bonjour
Then you should see the following page:You see, the browser displays the plain text response sent by the web service. Now, add second method to the HelloResource class:@GET @Produces(MediaType.TEXT_HTML) public String sayHTMLHello() { return "<html><title>Hello</title><body><h1>Bonjour, tout le monde!</h1><body></html>"; }This method returns a HTML response. Refresh the browser and you should see:You see, the browser now shows the HTML response sent from the web service (a web browser always expects Text/HTML response). That means with the same URI, the response representation can be different, depending on content type accepted by the clients.
<dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> <version>2.29.1</version> </dependency>Now, update the HelloResource class to have a new method that produces JSON response, as follows:
@GET @Produces(MediaType.APPLICATION_JSON) public String sayJsonHello() { return "{\"name\":\"greeting\", \"message\":\"Bonjour tout le monde!\"}"; }This method returns a simple piece JSON data. If you refresh the browser, you will see nothing changes because the browser doesn’t expect JSON response by default.
Spring Boot REST APIs Ultimate Course
Hands-on REST API Development with Spring Boot: Design, Implement, Document, Secure, Test, Consume RESTful APIs.
curl http://localhost:8080/HelloREST/rest/bonjour
Then you can see JSON response:{"name":"greeting", "message":"Bonjour tout le monde!"}You can use the –v option (versbose) to see more details such as request headers and response headers. For example:
curl -v http://localhost:8080/HelloREST/rest/bonjour
Then you can see the output something like this:You can use the –H option to specify a HTTP header in the request. For example:curl -H "Accept: text/html" http://localhost:8080/HelloREST/rest/bonjour
This command tells the server that the client expects response format to be of text/html. Hence the following response:<html><title>Hello</title><body><h1>Bonjour, tout le monde!</h1><body></html>And the following curl command will get plain text response from the server:
curl -H "Accept: text/plain" http://localhost:8080/HelloREST/rest/bonjour
<dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</artifactId> <version>2.29.1</version> </dependency> <dependency> <groupId>org.glassfish.jersey.inject</groupId> <artifactId>jersey-hk2</artifactId> <version>2.29.1</version> </dependency>Then code a simple RESTful web service client program as follows:
package net.codejava; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; import org.glassfish.jersey.client.ClientConfig; public class HelloClient { public static void main(String[] args) { String uri = "http://localhost:8080/HelloREST/rest/bonjour"; ClientConfig config = new ClientConfig(); Client client = ClientBuilder.newClient(config); WebTarget target = client.target(uri); String response = target.request() .accept(MediaType.APPLICATION_JSON) .get(String.class); System.out.println(response); } }This program simply sends a GET request to the server at the specified URI and reads the response. Run this program and you should see the following output:
{"name":"greeting", "message":"Bonjour tout le monde!"}
This is JSON response because the client expects application/json as the accepted media type.Congratulations, you have done your first hello world RESTful web services application, with both server and client. For your reference, you can download the sample project in the attachments section below, or get the sample codes from this GitHub repo.You can also watch the video version of this tutorial below: What's Next? I recommend you follow this one: Java CRUD RESTful Web Services Examples with Jersey and Tomcat