Saturday, February 25, 2017

Jersy Restful Example

package com;

import java.util.ArrayList;
import java.util.List;

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

import pojo.Address;
@Path("/aa")
public class AllDemo {

@GET
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
@Path("/bb")
public Address demoOne()
{
return new Address(100,"Chennai","TN");
}


@POST
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.TEXT_PLAIN})
@Path("/cc")
public String demoTwo(Address address)
{
System.out.println(address.getState());
return " Welcome to " + address.getCity();
}

@POST
@Produces({MediaType.APPLICATION_JSON})
@Path("/dd")
public List demoThree()
{
List list = new ArrayList(Address)();
Address address1 = new Address(10,"Chennai","TN");
Address address2 = new Address(20,"Bangalore","KA");
Address address3 = new Address(10,"Hyderabad","AP");
list.add(address1);
list.add(address2);
list.add(address3);
return list;
}

@POST
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
@Path("/ee")
public List(Address) demoFour(Address address)
{
System.out.println(address.getState());
List(Address) list = new ArrayList(Address)();
Address address1 = new Address(10,"Chennai","TN");
Address address2 = new Address(20,"Bangalore","KA");
Address address3 = new Address(10,"Hyderabad","AP");
list.add(address1);
list.add(address2);
list.add(address3);
address.setCity("updated City");
address.setState("Updated State");
list.add(address);
return list;
}

@GET
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
@Path("/ff")
public List(Address) demoFive()
{

Address address1 = new Address(10,"Chennai","TN");
Address address2 = new Address(20,"Bangalore","KA");
Address address3 = new Address(30,"Hyderabad","AP");

List(Address) list = new ArrayList(Address)();
list.add(address1);
list.add(address2);
list.add(address3);

return list;
}
}


package main;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class RESTClientExample {
   
    public static void main(String[] args) {
       
        try {

URL url = new URL("http://localhost:9090/JSonRest/rest/aa/bb");
               
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/xml");

if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}

BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));

String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}

conn.disconnect();

 } catch (MalformedURLException e) {

e.printStackTrace();

 } catch (IOException e) {

e.printStackTrace();

 }

}
}


package pojo;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement
//@XmlType(propOrder = {  "doorNo", "city","state" })
public class Address {
private int doorNo;
private String city;
private String state;

public Address() {
super();
// TODO Auto-generated constructor stub
}

public Address(int doorNo, String city, String state) {
super();
this.doorNo = doorNo;
this.city = city;
this.state = state;
}

public int getDoorNo() {
return doorNo;
}

public void setDoorNo(int doorNo) {
this.doorNo = doorNo;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

}

(?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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)Participants_RestService(/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 REST Service(/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(/param-value)
(/init-param)
(init-param)
(param-name)com.sun.jersey.api.json.POJOMappingFeature(/param-name)
(param-value)true(/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)
(/web-app)


import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

public class RestServiceMain {

private static URI getBaseURI() {
   return UriBuilder.fromUri("http://localhost:9090/Participants_RestService/").build();
 }

public static void main(String[] args) {
   ClientConfig config = new DefaultClientConfig();
     Client client = Client.create(config);
     WebResource service = client.resource(getBaseURI());
     // Fluent interfaces
     System.out.println(service.path("rest").path("hello").accept(MediaType.TEXT_PLAIN).get(ClientResponse.class).toString());
     // Get plain text
     System.out.println(service.path("rest").path("hello").accept( MediaType.TEXT_PLAIN).get(String.class));
   
     // Get XML
   
     //System.out.println(service.path("rest").path("hello").accept(MediaType.TEXT_XML).get(String.class));
   
     // The HTML
   
     System.out.println(service.path("rest").path("hello").accept(MediaType.TEXT_HTML).get(String.class));
}

}


No comments:

Post a Comment

உப்பு மாங்காய்

சுருக்குப்பை கிழவி. சுருக்கங்கள் சூழ் கிழவி. பார்க்கும் போதெல்லாம் கூடையுடனே குடியிருப்பாள். கூடை நிறைய குட்டி குட்டி மாங்காய்கள். வெட்டிக்க...