Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

JAX-RS Example Jersey


We can create JAX-RS example by jersey implementation. To do so, you need to load jersey jar files or use maven framework.

In this example, we are using jersey jar files for using jersey example for JAX-RS.


Click me to download jersey jar files.


There are created 4 files for hello world JAX-RS example:

  1. Hello.java
  2. web.xml
  3. index.html
  4. HelloWorldClient.java

The first 3 files are created for server side and 1 application for client side.


JAX-RS Server Code

File: Hello.java

  1. package com.Kreationnext.rest;
  2. import javax.ws.rs.GET;
  3. import javax.ws.rs.Path;
  4. import javax.ws.rs.Produces;
  5. import javax.ws.rs.core.MediaType;
  6. @Path(“/hello”)
  7. public class Hello {
  8.   // This method is called if HTML and XML is not requested
  9.   @GET
  10.   @Produces(MediaType.TEXT_PLAIN)
  11.   public String sayPlainTextHello() {
  12.     return “Hello Jersey Plain”;
  13.   }
  14.   // This method is called if XML is requested
  15.   @GET
  16.   @Produces(MediaType.TEXT_XML)
  17.   public String sayXMLHello() {
  18.     return “<?xml version=\”1.0\”?>” + “<hello> Hello Jersey” + “</hello>”;
  19.   }
  20.   // This method is called if HTML is requested
  21.   @GET
  22.   @Produces(MediaType.TEXT_HTML)
  23.   public String sayHtmlHello() {
  24.     return “<html> “ + “<title>” + “Hello Jersey” + “</title>”
  25.         + “<body><h1>” + “Hello Jersey HTML” + “</h1></body>” + “</html> “;
  26.   }
  27. }

File: web.xml

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <web-app xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  3. xmlns=“http://java.sun.com/xml/ns/javaee”
  4. xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee
  5. http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd”
  6. id=“WebApp_ID” version=“3.0”>
  7.  <servlet>
  8.     <servlet-name>Jersey REST Service</servlet-name>
  9.     <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
  10.     <init-param>
  11.         <param-name>jersey.config.server.provider.packages</param-name>
  12.         <param-value>com.Kreationnext.rest</param-value>
  13.     </init-param>
  14.     <load-on-startup>1</load-on-startup>
  15.   </servlet>
  16.   <servlet-mapping>
  17.     <servlet-name>Jersey REST Service</servlet-name>
  18.     <url-pattern>/rest/*</url-pattern>
  19.   </servlet-mapping>
  20. </web-app>

File: index.html

  1. <a href=“rest/hello”>Click Here</a>

Now run this application on server. Here we are using Tomcat server on port 4444. The project name is restfuljersey.

After running the project, you will see the following output:

jaxrs-jersey-output

JAX-RS Client Code

The ClientTest.java file is created inside the server application. But you can run client code by other application also by having service interface and jersey jar file.

File: ClientTest.java

  1. package com.Kreationnext.restclient;
  2. import java.net.URI;
  3. import javax.ws.rs.client.Client;
  4. import javax.ws.rs.client.ClientBuilder;
  5. import javax.ws.rs.client.WebTarget;
  6. import javax.ws.rs.core.MediaType;
  7. import javax.ws.rs.core.UriBuilder;
  8. import org.glassfish.jersey.client.ClientConfig;
  9. public class ClientTest {
  10.   public static void main(String[] args) {
  11.     ClientConfig config = new ClientConfig();
  12.     Client client = ClientBuilder.newClient(config);
  13.     WebTarget target = client.target(getBaseURI());
  14.     //Now printing the server code of different media type
  15.     System.out.println(target.path(“rest”).path(“hello”).request().accept(MediaType.TEXT_PLAIN).get(String.class));
  16.     System.out.println(target.path(“rest”).path(“hello”).request().accept(MediaType.TEXT_XML).get(String.class));
  17.     System.out.println(target.path(“rest”).path(“hello”).request().accept(MediaType.TEXT_HTML).get(String.class));
  18.   }
  19.   private static URI getBaseURI() {
  20.     //here server is running on 4444 port number and project name is restfuljersey
  21.     return UriBuilder.fromUri(“http://localhost:4444/restfuljersey”).build();
  22.   }
  23. }

Output:

Hello Jersey Plain
<?xml version="1.0"?><hello> Hello Jersey</hello>
<html> <title>Hello Jersey</title><body><h1>Hello Jersey HTML</h1></body></html> 

Click me to download JAX-RS example using eclipse without jersey jar files.

Comments

comments