Our Work

How to Implement Spring Rest services in Liferay 6.1

Updated today

In liferay 6.1, we know that all services will be generated from Service Builder but in some scenarios we may need to enable services as REST using Spring Framework. In this article we will talk about how to do it.
Steps to create Spring Rest services :

Step 1 :
Create Liferay MVC portlet by using eclipse Liferay IDE
Step 2 :
Add spring dependencies to liferay-plugin-package.xml file

name=Spring Rest Api
module-group-id=liferay
module-incremental-version=1
tags=
short-description=
long-description=
change-log=
page-url=http://www.liferay.com
author=Liferay, Inc.
licenses=LGPL
liferay-versions=6.1.1+
portal-dependency-jars=\
    aopalliance.jar,\
    commons-io.jar,\
    commons-logging.jar,\
    jstl-api.jar,\
    jstl-impl.jar,\
    log4j.jar,\
    slf4j-api.jar,\
    spring-aop.jar,\
    spring-asm.jar,\
    spring-beans.jar,\
    spring-context-support.jar,\
    spring-context.jar,\
    spring-core.jar,\
    spring-expression.jar,\
    spring-web-portlet.jar,\
    spring-web-servlet.jar,\
    spring-web-struts.jar,\
    spring-web.jar

Step 3 :
Modify web.xml to include Front Controller and URL Pattern.

<servlet>
     <servlet-name>springRestApi</servlet-name>
     <servlet-class>com.liferay.portal.kernel.servlet.PortalDelegateServlet</servlet-class>
     <init-param>
<param-name>servlet-class</param-name>
<param-value>org.springframework.web.servlet.DispatcherServlet</param-value>
     </init-param>
     <init-param>
<param-name>sub-context</param-name>
<param-value>springRestApi</param-value>
     </init-param>
     <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springRestApi</servlet-name>
     <url-pattern>/restServices/*</url-pattern>
</servlet-mapping>

Step 4 :
Create a xml file in WEB-INF folder with servlet name and appended with ‘-servlet’
Example: 
Servlet Name in web xml : springRestApi
XML File Name : springRestApi-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: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">
<bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
    <bean class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<context:component-scan base-package="com.aapa" />
<mvc:annotation-driven />
</beans>

Step 5 :
Create a controller class (Spring 3.X as this version is available in Liferay 6.1)
com.aapa.rest.controller.AapaRestController.

As below we need to annotate controller to the class

@Controller
public class AapaRestController {
}

We are producing the service with an endpoint get-my-org-info

@RequestMapping(value="get-my-org-info",method=RequestMethod.GET)
@ResponseBody
public String getMyOrgInfo(){
}

Looking for Liferay Developer?

KTree is the best offshore Liferay development company with extensive experience in Liferay Portal Development services along with Upgrade & Migration. Hire Liferay developers or Liferay development services from LiferayDeveloper.

Request For Quote