A Simple Hello World example to know the basic jersey -RS web service
Steps:
1)Open eclipse and create a Dynamic web project with some project
name of your choice
2) Download jersey -RS 2.0 jars from this link https://jersey.java.net/download.html
3) Go to WebContent/WEB-INF/lib folder under your project and drop all jars present under jersey folder into lib directory
4)Add the jars to the build path and add java-json.jar along with other jars to buildpath
5) Now configure web.xml (deployment descriptor) under WEB-INF as follows
output3: Third URL calls toJsonHello() in HelloWorld it takes path param and construct Json and returns the response
Steps:
1)Open eclipse and create a Dynamic web project with some project
name of your choice
2) Download jersey -RS 2.0 jars from this link https://jersey.java.net/download.html
JerseyBeginner index.jsp HelloWorld org.glassfish.jersey.servlet.ServletContainer jersey.config.server.provider.packages com.practice.jersey HelloWorld /rest/*
Now create a package under src folder as com.practice.jersey(As given in <param-value> tags in web.xml
Create a class HelloWorld under package com.practice.jersey
Place code below in HelloWorld.java
package com.practice.jersey; 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; import javax.ws.rs.core.Response; import org.json.JSONException; import org.json.JSONObject; @Path("/hello") public class HelloWorld { @GET @Produces(MediaType.TEXT_PLAIN) public String sayHello(){ return "hello World"; } @Path("/{name}") @GET @Produces(MediaType.TEXT_XML) public String toXmlHello(@PathParam("name") String name){ return "Hello "+name+" "; } @Path("/{name}/json") @GET @Produces(MediaType.APPLICATION_JSON) public Response toJsonHello(@PathParam("name") String name) throws JSONException{ JSONObject sample=new JSONObject(); sample.accumulate("name", "Hello"+name); return Response.status(Response.Status.ACCEPTED).entity(sample.toString()).build(); } }
Now run the project on server open the browser and type the Ur L's as below
1)http://localhost:8080/JerseyBeginner/rest/hello
2)http://localhost:8080/JerseyHelloWorld/rest/hello/javasimplestuff
3)http://localhost:8080/JerseyHelloWorld/rest/hello/javasimplestuff/json
1)http://localhost:8080/JerseyBeginner/rest/hello
2)http://localhost:8080/JerseyHelloWorld/rest/hello/javasimplestuff
3)http://localhost:8080/JerseyHelloWorld/rest/hello/javasimplestuff/json
output 1: First URL calls sayHello() in HelloWorld
output2: Second URL calls toXmlHello() in HelloWorld @Pathparam takes the value passed from URL @Path indicates the URL pattern to call for that particular rest resouce
output2: Second URL calls toXmlHello() in HelloWorld @Pathparam takes the value passed from URL @Path indicates the URL pattern to call for that particular rest resouce
output3: Third URL calls toJsonHello() in HelloWorld it takes path param and construct Json and returns the response
No comments:
Post a Comment