Thursday 6 August 2015

rest api with spring + jersey + gradle

This tutorial is for beginners who wants to understand Restful Webservice using jersey framework with spring integration.

I am using gradle for setting up my project.

you should have below dependencies in build.gradle

dependencies {
testCompile 'org.springframework:org.springframework.test:3.2.0.RELEASE'
testCompile group: 'junit', name: 'junit', version: '4.+'

testCompile 'javax.servlet:servlet-api:2.5'

compile 'org.springframework:spring-context:3.2.6.RELEASE'
compile 'org.springframework:spring-context-support:3.2.6.RELEASE'
compile 'org.springframework:spring-web:3.2.6.RELEASE'
compile 'org.springframework:spring-webmvc:3.2.6.RELEASE'

compile 'org.slf4j:slf4j-log4j12:1.7.7'

compile 'com.sun.jersey:jersey-server:1.19'
        compile 'com.sun.jersey:jersey-client:1.19'
        compile 'com.sun.jersey:jersey-json:1.19'
        compile 'com.sun.jersey:jersey-core:1.19'
        compile 'com.sun.jersey:jersey-servlet:1.19'
        compile 'com.sun.jersey.contribs:jersey-spring:1.8'

compile 'commons-dbcp:commons-dbcp:1.3'
compile 'commons-codec:commons-codec:1.3'
}

----------------------------------------------------------------------------------------
below web.xml shows how to configure jersey-servlet and loads spring application context.


<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/test-restapi-servlet.xml</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.test.demo</param-value>
</init-param>
  <init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
  </init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet>
        <servlet-name>test-restapi</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
 
 
<servlet-mapping>
        <servlet-name>test-restapi</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

</web-app>

-------------------------------------------------------------------

below is your application context  test-restapi-servlet,xml.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:cxf="http://cxf.apache.org/core"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://cxf.apache.org/core
http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/transports/http/configuration
http://cxf.apache.org/schemas/configuration/http-conf.xsd ">

<context:component-scan base-package="com.test.demo.*" />

<mvc:annotation-driven />

</beans>

-----------------------------------------------------------------------------------------

Final Test class

package com.test.demo;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Component;


@Component
@Path("/test")
public class RestTest {

@GET
@Path("/getname")
public Response getName(){

return Response.status(200).entity("this is restful service").build();
}
}


Now deploy your application in tomcat and hit url as http://localhost:8080/{projectName}/rest/test/getname
it should return "this is restful service" in your browser.

No comments:

Post a Comment