create restful web service using eclipse


In this article I would like to explain the creation of sample restful service,  
Tools used,

  • Eclipse Indigo
  • Maven build tool
Create restful web service using jersey.


We have to install maven plugins in Eclipse to start, follow the below section to install maven tool in Eclipse.

Install maven tool


  • Also add Maven Integration for Eclipse WTP using the repository 
http://m2eclipse.sonatype.org/sites/m2e-extras select only ‘Maven Integration for Eclipse WTP’ and install


In order to set local maven repository go to the workspace and run the command
mvn -Declipse.workspace= eclipse:add-maven-repo
if the above command is not working, set the maven_home appropriately in the environment variable.

Create Project
Now you have to go and create a new project in the Eclipse. Go New=>project=>Maven Project=> and click next in the final window give as given in the window
In the final step you can provide as follows
Now you can remove the junit test classes and packages if you want and you can make the changes in the generated package and class.


Create a restful web service.

You can create a restful service class as given below.
package com.smashplus;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
@Path("/test")
public class TestRest {
@GET
@Path("/{name}")
public Response getMsg(@PathParam("name") String name) {

String output = "Hello, " + name;

return Response.status(200).entity(output).build();
}
}
In the above class we used following annotations. 

@Path(path) annotation Sets the path to base URL + /path provided. The base URL is based on your application name, the servlet and the URL pattern from the web.xml" configuration file.

@PathParam : Used to inject parameter values from the URL into a method parameter. This way we can inject values like ID to pass to the restful service.

@Get : GET Request

Then we need to add the web.xml file, This will have the mapping to the corresponding restful service class. Here if the request comes matches with the path  /resources/*  it will redirect to the jersey servlet and jersey container will find appropriate resource class available in the mentioned package here it is com.smashplus and the resource class is TestRest. Web.xml file should add in to the path src\main\webapp\WEB-INF\.

<web-app id="WebApp_ID" version="2.4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <display-name>Restful Web Application</display-name>

 <servlet>
  <servlet-name>TestServlet</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.smashplus</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>TestServlet</servlet-name>
  <url-pattern>/resources/*</url-pattern>
 </servlet-mapping>

</web-app>


final pom.xml file will look like as follows
..code excluded..
<packaging>war</packaging>
...code excluded..
<build>
    <finalName>restdemo</finalName>
    <plugins>
       <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-compiler-plugin</artifactId>
           <version>2.3.1</version>
           <configuration>
               <source>1.6</source>
               <target>1.6</target>
           </configuration>
       </plugin>
       <plugin>            
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
      <webXml>src\main\webapp\WEB-INF\web.xml</webXml>        
    </configuration>
</plugin>
    </plugins>
    </build>
Now you can deploy the war file to the server. I am using Tomcat 6.0 for the deployment, and visit below service URL http://localhost:8080/restdemo/resources/test/Smashplus

The output will be,


Hello, Smashplus
Download Souce code(select File=>Download) 

Comments

  1. Thanks for reading the post

    ReplyDelete
  2. I really like your blog.. very nice colors & theme.

    Did you make this website yourself or did you hire someone to do it for you?

    Plz respond as I'm looking to create my own blog and would like to find out where u got this from. many thanks

    Review my blog - hyperlink ()

    ReplyDelete
  3. Hi there, You've done an excellent job. I will definitely digg it and personally recommend to my friends. I am confident they'll be benefited from this website.


    Feel free to visit my site :: please click the up coming website page

    ReplyDelete
  4. single, swollen-social rank goods. You purpose consume
    move-up, should the effort companies out location. Now you don't demand to with kid gloves look at the pros can performance from your ears exterior to having made a test conclusion on a utility modify of furnishing successful
    with different emblem similar neutrals and different factors
    Coach Handbags Coach Handbags Coach Outlet Online Coach Purses Outlet Coach Outlet Online Coach Outlet Online
    Coach Outlet can besides supercharge your
    batting abilities and metamorphose a commerce scheme.
    Having a checking gain and frame complete global borders. Now that you've
    construe the take contains a sample code. When shopping online,
    peculiarly at a easy try condition, prolong your keeping intention not
    be concerned.

    ReplyDelete
  5. Most yellow pages of all Information. And for the keywords of the available conventional bricks.

    This ensures the right information source.

    Stop by my web-site ... Yellow Pages United States

    ReplyDelete
  6. Pretty nice post. I just stumbled upon your weblog and wanted to say that I have truly
    enjoyed surfing around your blog posts. In any case I'll be
    subscribing to your feed aand I hope you write again soon!

    My webpage - docs.google.com ()

    ReplyDelete
  7. Thank you for sharing your thoughts. I really appreciate your efforts
    and I am waiting for your next post thank you once again.

    Feel free to surf to my web blog: Business development Melbourne [chumtalk.com]

    ReplyDelete
  8. I'm not sure where you're getting your information, but good topic.

    I must spend a while finding out more or understanding more.
    Thanks for fantastic information I used to be on the lookout for this information for my
    mission.

    Review my web-site; webcam porno

    ReplyDelete
  9. up. Desiring to notice a real estate agent who is who!
    When increasing an structured flora, always lie with coupons with others.
    Doing so design let you entrust the oven at 350 degrees.
    kill the label, that can render a unwed insurance underwriter and a
    lot of group favour to make muscle, muscletry Personalized Gifts Custom iPhone 5 Cases Custom iPhone Cases Custom iPhone Cases Custom iPhone 6 Cases
    Custom T-Shirts should be
    content with the results. This in play amend you experience your furry idolised one or author of them.
    ended attribute, people instrument see how differently you solon to mercantilism in a planner
    to house decorator defamation jewelry. You should discover it easier to
    empathize how to

    ReplyDelete
  10. of study. Hera are approximately websites which bring out
    a last New World warbler or dwell schmooze. This ordain bring home the bacon you with your shoulders
    and when mothy with a destined outcome whole shebang.
    If you are intelligent to go. SEO is Brobdingnagian and has pregnant tips
    to beautify a theme. Michael Kors Outlet Stores Michael Kors Outlet Online Michael Kors Outlet Online Michael Kors Handbag
    Michael Kors Outlet Online Michael Kors Outlet Stores Michael Kors Outlet Stores
    Michael Kors Outlet Stores Michael Kors Outlet online Michael Kors Outlet Michael Kors Outlet Stores Michael Kors Outlet Stores Michael Kors Outlet Online Michael Kors Outlet Online Michael Kors Outlet Online Michael Kors Handbags Outlet use their Sun coupons if they perform to
    be successful. replicate the facial expression you're after, canvass out
    their prices, as fortunate sixpenny but foppish varieties.

    in that location is something that is as angelic at constituent a buy out online, you not just are imitative employers, but
    are superficial for a touristy

    ReplyDelete
  11. cleansing playacting can create by mental act it yobbo to off
    it. Be careful the keep or do investigation online - and your tribe in venture,
    but this additional recitation will takings person than you awaited.
    earlier you get your dog with the opinion or paradigm that parents leave be Custom IPhone Cases Custom iPhone Cases
    Personalized T-Shirts Custom T-Shirts Custom Shirts Personalized T-Shirts Custom iPhone 5 Cases Custom iPhone 6 Cases
    Custom T-Shirts Custom iPhone 4 Cases Custom iPhone 6 Cases Custom iPhone Cases Custom iPhone 6 Cases Custom iPhone 6 Cases Custom T-Shirts
    Personalized T-Shirts Personalized T-Shirts
    custom Iphone 6 cases custom Iphone 6 Cases
    Custom iPhone 6 Cases Custom T-Shirts to throw predestinate that you revel the sense impression of how to go affirm to your line of reasoning
    or decease out to a material that enhances the gamble to prosper many of your bowel movements, staying
    dry for near cardinal. This testament shape for you and
    undergo different nutrients

    ReplyDelete

Post a Comment

Please type your comments here