Friday, 4 September 2015

Example Of Web Services

Web Services Jersey


Example


Step 1 : index.jsp
1
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2
    pageEncoding="ISO-8859-1"%>
3
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4
<html>
5
<head>
6
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7
<title>Insert title here</title>
8
</head>
9
<body>
10
11
<a href="rest/abc">Click Here</a>
12
13
</body>
14
</html>         
15
16


Step 2 : web.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2
<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_3_0.xsd" id="WebApp_ID" version="3.0">
3
 <servlet>
4
    <servlet-name>Jersey REST Service</servlet-name>
5
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
6
     <init-param>
7
        <param-name>jersey.config.server.provider.packages</param-name>
8
        <param-value>com.nitin.ex</param-value>
9
    </init-param>
10
    <load-on-startup>1</load-on-startup>
11
  </servlet>
12
  <servlet-mapping>
13
    <servlet-name>Jersey REST Service</servlet-name>
14
    <url-pattern>/rest/*</url-pattern>
15
  </servlet-mapping>
16
</web-app>    
17
18


Step 3 : ClientTest.java 
1
package com.nitin.client;
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
10
public class ClientTest {
11
  
12
  public static void main(String[] args) {
13
      ClientConfig config = new ClientConfig();
14
      Client client = ClientBuilder.newClient(config);
15
      WebTarget target = client.target(getBaseURI());
16
17
     System.out.println(target.path("rest").path("abc").request().accept(MediaType.TEXT_PLAIN).get(String.class));
18
      System.out.println(target.path("rest").path("abc").request().accept(MediaType.TEXT_XML).get(String.class));
19
      System.out.println(target.path("rest").path("abc").request().accept(MediaType.TEXT_HTML).get(String.class));
20
    }
21
22
    private static URI getBaseURI() {
23
      return UriBuilder.fromUri("http://localhost:8080/WSJersyDemo/").build();
24
    }
25
26
}
27
28
29


Step 4 : Welcome.java 
1
package com.nitin.ex;
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
7
@Path("/abc")
8
public class Welcome {
9
  @GET
10
  @Produces(MediaType.TEXT_PLAIN)
11
  public String nit()
12
  {
13
    return "welcome nitin";
14
  }
15
  
16
  @GET
17
    @Produces(MediaType.TEXT_XML)
18
    public String sayXMLHello() {
19
      return "<?xml version=\"1.0\"?>" + "<hello> nitin xml demo" + "</hello>";
20
    }
21
  
22
   @GET
23
    @Produces(MediaType.TEXT_HTML)
24
    public String sayHtmlHello() {
25
      return "<html> " + "<title>" + "Hello Jersey" + "</title>"
26
          + "<body><h1>" + "hello nitin text html" + "</h1></body>" + "</html> ";
27
    }
28
29
}
30
31
32



No comments:

Post a Comment