So far we have discussed the basics of Web services. In the just previous chapter we have seen an example in JAX-WS.This chapter would give a simple JAX-RS Tutorial.
Overview to JAX-RSJAX-RS provides support to RESTful Web Services. REST stands for Representational State Transfer. JAX-RS 2.0 is the latest JAX-RS specification
Features of RESTful Web Service
There are several implementations for JAX-RS. Some of them are listed below
Annotation support in JAX-RSNow let us see a sample RESTful web service.
Software Tools NeededEclipse Indigo :- Download Link
Apache Tomcat 7 :- Download Link
Java EE SDK 1.6 :- Download Link
Mozilla Firefox :- Download Link
Steps1)Extract eclipse and Tomcat to suitable directories.Open eclipse in a suitable workspace.
2)Now create a dynamic web project in eclipse . Give a suitable name.In this case let it be RESTSample.
3)Press Next Button . In the next window also press Next.Then in the coming window select the check box.Then press Finish button
4)Now right click in the server console and create a new Tomcat instance.
5)Locate the server location and add the newly created project to the server.And then press Finish
6)Now double click on the server instance in the console and then select the highlighted radio button.And then save the server details by pressing CTRL + S
7) Now download RESTEasy API from sourceforge. In this example we are using version 3.0 Final. Download it and extract it to suitable folder. Take the jar files from the lib and embedded-lib directories . Then put those jar files in WebContent/WEB-INF/lib directory.
8)Now create a package in src.Give a suitable name. In this case it is com.rest.services
9)Now create a Java file in the newly created package.To be simple , we can discuss a GET service which returns a String message when an http request hits.
import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; @Path("sampleService") public class SampleService { public SampleService() { } @GET @Path("getService") @Produces("application/txt") public String getService() { String message = "Hello ! This is a message from REST service"; return message; } }
10)Now edit the web.xml file in the WebContent/WEB-INF directory.
RESTSample index.html rest-servlet org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher 1 rest-servlet /* resteasy.scan true resteasy.servlet.mapping.prefix / org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
10)Now build and publish the application to server.Then start the server.If an error ‘No class def found error : javax/enterprise/context/spi/Contextual’ then remove the resteasy-cdi jar from the lib .Then rebuild and republish.
11)So the server is started.Now we need to test the locally deployed rest service.For that we can use REStClient plugin of Mozilla Firefox.Download it from Mozilla update site.
12)Open the RESTClient in firefox. Select the method name as GET . Type the url as :http://localhost:8080/RESTSample/sampleService/getService
13)Now Press the send button.
14)If we click on the Response Body tab in the RESTClient we can see the response string message.
See Related Discussions WebService Overview JAX-WS TutorialRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4