Thursday, May 21, 2015

JAX-RS Using JERSEY -- Hello Jersey In Different Formats


In this example, we are going to use jersey for invoking "Hello Jersey" mentioned in different formats like :
a. HTML
b. Plain Text
c. XML

for this example we will create 4 files:
  1. HelloJersey_DifferentFormat.java
  2. web.xml
  3. HelloJerseyClientCode.java

HelloWorld_DifferentFormat.java

package com.tushar.jersey.Project;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("helloFormat")
public class HelloJersey_DifferentFormat{

       @GET
       @Produces(MediaType.TEXT_PLAIN)
       public String sayPlainTextHello() {
            return "Hello JerseyThis is plain text";
       }
       @GET
       @Produces(MediaType.TEXT_XML)
       public String sayXMLHello() {
            return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey , xml version" + "</hello>";
       }
      
      @GET
       @Produces(MediaType.TEXT_HTML)
       public String sayHTMLHello() {
            return "<html> " + "<title>" + "Hello Jersey" + "</title>" + "<body><h1>" + "Hello Jersey This is an HTML Page!" + "</body></h1>" + "</html> ";
       }

}



WEB.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>com.tushar.jersey.Project</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
              <servlet-name>jersey-serlvet</servlet-name>
              <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
              <init-param>
                   <param-name>com.sun.jersey.config.property.packages</param-name>
                   <param-value>com.tushar.jersey.Project</param-value>
              </init-param>
              <load-on-startup>1</load-on-startup>
       </servlet>


  <servlet-mapping>
              <servlet-name>jersey-serlvet</servlet-name>
              <url-pattern>/rest/*</url-pattern>
       </servlet-mapping>

</web-app>


Run your rest service

Start the server and You should be able to access your resources under the following URL: http://localhost:8080/com.tushar.jersey.Project/rest/helloFormat



Output

The browser will always request the HTML MIME type. 



For invoking Plain Text and xml we will create a client code.



Client Code.java :

The ClientCode.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.



package com.tushar.jersey.Project;

import java.net.URI;

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 javax.ws.rs.core.UriBuilder;

import org.glassfish.jersey.client.ClientConfig;

public class ClientCode { 

                public static void main(String[] args) {    
                                ClientConfig config = new ClientConfig(); 
                                Client client = ClientBuilder.newClient(config); 
                                WebTarget target = client.target(getBaseURI());  
                               

                                //Now printing the server code of different media type 

System.out.println(target.path("rest").path("helloFormat").request().accept(MediaType.TEXT_PLAIN).get(String.class)); 

System.out.println(target.path("rest").path("helloFormat").request().accept(MediaType.TEXT_XML).get(String.class));
System.out.println(target.path("rest").path("helloFormat").request().accept(MediaType.TEXT_HTML).get(String.class)); 

  } 

                private static URI getBaseURI() { 
    //here server is running on 8080 port number and project name is com.tushar.jersey.Project 
    return UriBuilder.fromUri("http://localhost:8080/com.tushar.jersey.Project").build(); 
  } 
}  





No comments:

Post a Comment