Saturday, 6 June 2015

1. Jersey REST WebService : QuickStart

This is very basic tutorial to get simple Jersey REST webservice working using maven archetype.This tutorial is based on below software
  • eclipse-jee-mars-2-win32
  • apache-maven
  • apache-tomcat
  • Jersey version 2
As first step, create new Maven project:
 

Select ‘jersey-quickstart-webapp’ archtype and click next:

Enter details as below(Maven GAV details) and click finish:


This will create project with structure like below:

Deploy project on tomcat server and hit url http://localhost:8080/invoicedetails ; welcome page will be like below:


Click on link ‘Jersey resource’, response will be like below:


By clicking on that link, we have sent our first REST webserivce request and received successful response. This is good starting point to build our own resources on top of this which we will cover in next tutorials.So when we click on link, we send request to url http://localhost:8080/invoicedetails/webapi/myresource , Jersey locates the resource for path “myresource”. MyResource is available on path “myresource” as declared using annotation @PathAs this is GET request, method annotated with @GET is called and resource is returned.

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package in.blogspot.ashish4java.invoicedetails;

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

/**
 * Root resource (exposed at "myresource" path).
 */
@Path("myresource")
public class MyResource {

    /**
     * Method handling HTTP GET requests. The returned object will be sent
     * to the client as "text/plain" media type.
     *
     * @return String that will be returned as a text/plain response.
     */
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt() {
        return "Got it!";
    }
}

Note down annotation @Produces which describes the type of output this method supports. In this case, it will return plain text to client.Last important part of this tutorial is 'web.xml'. This mentions Jersey servlet class, package to look for resources and url-pattern for request.

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
     see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>in.blogspot.ashish4java.invoicedetails</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>
</web-app>

Our first simple tutorial finishes here. In next tutorials, we will build on top of this project. Thanks for going through this tutorial and Stay tuned!

No comments:

Post a Comment