Search This Blog

Follow by Email

Saturday, February 10, 2018

Jersy Rest Webervice with eclipse and Weblogic 12c or tomcat


How to work with jersy rest webservice using eclipse and weblogic server/tomcat

Step 1: Create a dynamic web project in eclipse and provide project name





Step 2: open web.xml and add below configuration code for rest webservices.



  jersyRestService
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
      
        Jersey Web Application
        com.sun.jersey.spi.container.servlet.ServletContainer
    
    
        Jersey Web Application
        /rest/*
    



Step 3: Create one file name called weblogic.xml and add the below code.



12.2
    
        jax-rs
        2.0
        2.22.1.0
        false
    



Step 4: Download rest api related jar -http://repo1.maven.org/maven2/com/sun/jersey/jersey-archive/1.19.1/jersey-archive-1.19.1.zip Unzip the same and place the jar inside lib folder of the project.


Step 5: Create a class name called RestServiceAPI.java add below code.

package com.siva.restservice;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
/**
 * 
 * @author Siva
 *
 */

@Path("/helloRest")
public class SimpleRestService {
	
	
	@GET
	@Produces(MediaType.TEXT_PLAIN)
	public String getDetails(){
		System.out.println("This is Get request");
		return "Hello Welcome to Rest webservice";
	}
	@GET
	@Path("/getDetail/{requestParam}")
	@Produces(MediaType.TEXT_PLAIN)
	public String getDetailsWithParameter(@PathParam("requestParam") String requestParam){
		System.out.println("USer request paramater["+requestParam +"]");
		System.out.println("This is Get request");
		return "Hello Welcome to Rest webservice you have given input as -"+requestParam;
	}

}



Step 6: Create one config class called- ApplicationConfig.java
Right click on src-> Webservice->RestWebservice->select Classname- which class needs to be configured in ApplicationConfig Click Next-> Finish

package rest.application.config;

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
/**
 * 
 * @author Siva
 *
 */
@ApplicationPath("resources")
public class ApplicationConfig  extends Application{

	public Set> getClasses(){
		return getRestClasses();
	}
	
	private Set> getRestClasses(){
	  Set> resources = new HashSet>();	
	  resources.add(com.siva.restservice.SimpleRestService.class);
	  return resources;
	}
	
}



Step 7: This is very important step

Run the weblogic server and open the console
http://localhost:7001/console provide username and password and login the same into weblogic server admin console

Step 8: Click on the Deployments- the install the jax-rs-2.0.war, which is inside-
Oracle_home/wlserver/common/deployable-libraries
If you are working on jsr311 then deploy jsr311-api-111.war which is available on same path.


For Tomcat Deployment Step 3 , 6,7,8 not required

Right click on the Project and Run AS - server- select appropriate server and deploy the same.

if you want to test in browser use-
http://localhost:8080/jersyRestService/rest/helloRest
http://localhost:8080/jersyRestService/rest/helloRest/getDetail/helloRequest


Step 9: Create rest client class which get the test results for rest webservice
package com.siva.restservice.test;

import javax.ws.rs.core.MediaType;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
/**
 * 
 * @author Siva
 *
 */
public class RestServiceClient {
	
	public static void main(String[] args) {
		String uri = "http://localhost:8080/jersyRestService/rest/helloRest";
		try{
			
			Client client = Client.create();
			WebResource resource = client.resource(uri);
			String response = resource.type(MediaType.TEXT_PLAIN).get(String.class);
			System.out.println("Response from Rest webservice["+response+"]");
			String request = "GetRestWithParam";
			uri = uri.concat("/getDetail/").concat(request);
			resource = client.resource(uri);
			String response1 = resource.type(MediaType.TEXT_PLAIN).get(String.class);
			System.out.println("Response from Rest webservice with parameter["+response1+"]");
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
	}

}


Step 10: You can see the output as below

Response from Rest webservice[Hello Welcome to Rest webservice]
Response from Rest webservice with parameter[Hello Welcome to Rest webservice you have given input as -GetRestWithParam]


Thank you very much for viewing this post. If you like, don't forget to share/comment.


AddToAny

Contact Form

Name

Email *

Message *