We used cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it. What For?

« Back to Blogs

JAX-RS RESTful Web Services in Liferay DXP

JAX-RS (Java API for RESTful Web Services)

JAX-RS is a Java programming language API specification that provides support in creating web services. JAX-RS uses annotations, (@GET, @POST, @Path etc.)  for the development and deployment of web service clients and endpoints. Liferay DXP / 7 provides built-in support for creating and deploying JAX-RS based REST services.

 

  • About this Blog

In the following blog I have developed rest service that will provide user information of pased userId in liferay 7.

  • Prerequisite

Basic knowledge of Liferay IDE, Liferay Portal, Liferay Workspace and JAX-RS.

  • Environment requirements
    • Liferay IDE(3.1.0)
    • Liferay Plugins SDK  7.0 &  Liferay Portal 7.0 CE GA3
    • JDK 8

 

  • Creating JAVAX-RS Module
  • Create a liferay workspace and new rest module project as follow.

Create a liferay workspace and new rest module project

 

  • Provide suitable project name.
  • Select “rest” form “Project Template Name” drop down menu, as it will create rest module structure.
  • On the next page fill suitable component class name and package name.
  • Once you click on finish button, liferay IDE will create following structure.

liferay IDE Structure

 

  • Configuring CXF Endpoints

Liferay supports JAX-RS via the Apache CXF implementation. CXF endpoints are context paths where the JAX web services are deployed to and accessible from.

  • We can configure CXFEndppoint from /src/main/resources/configuration/com.liferay.portal.remote.cxf.common.configuration.

CXFEndpointPublisherConfiguration-cxf” file.

  • CXFEndpointPublisherConfiguration
contextPath=/JAVAX-RS
authVerifierProperties=auth.verifier.BasicAuthHeaderAuthVerifier.urls.includes=*

Where contextPath indicates path where JAX web services will be deployed to on the Liferay server.

For example,

For above configuration services will be deployed and can be accessible by following URL

http://localhost:8080/o/JAVAX-RS

 

  • Configuring REST Extenders
  • We can configure REST Extenders from “/src/main/resources/configuration/com.liferay.portal.remote.rest.extender.

configuration.RestExtenderConfiguration-rest” file.

  • RestExtenderConfiguration

 

contextPaths=/JAVAX-RS
jaxRsServiceFilterStrings=(component.name=com.st.rest.application.JavaxRSControllerApplication)

Where, contextPaths indicates location on liferay server where this REST EXTENDER add/update deployed services.

jaxRsServiceFilter Strings indicates component that implement javax.ws.rs.core.Application.

 

  • Publishing JAX-WS RS Services

The component you defined in RestExtenderConfiguration will publish JAX-RS web service.

Following class registers an OSGi component that publishes a JAX-RS web service at /user/user-info

 

package com.st.rest.application;

@ApplicationPath("/user")
@Component(immediate = true, service = Application.class,
configurationPid = "com.st.rest.application.JavaxRSControllerApplication")

public class JavaxRSControllerApplication extends Application {

 public Set<Object> getSingletons() {
  return Collections.<Object>singleton(this);
 }

 @GET
 @Path("/user-info/{userId}")
 @Produces("application/json")
 public String getUserInfo(@PathParam("userId")String userId) {
  JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
		
  User user = null;
  if(Validator.isNotNull(userId)) {
   try {
    user = UserLocalServiceUtil.getUser(Long.parseLong(userId));
   } catch (NumberFormatException e) {
    jsonObject.put("ERROR", "Please pass valid User ID");
   } catch (PortalException e) {
    jsonObject.put("ERROR", "No User Found !");
   }
  }

  if(Validator.isNotNull(user)) {
   jsonObject.put("emailId", user.getEmailAddress());
   jsonObject.put("Name", user.getFullName());
  }
  return jsonObject.toJSONString();

 }
}
  • Deploy the rest module in liferay server.

 

  • To access rest service use following examples:
http://localhost:8080/o/JAVAX-RS/user/user-info/{userId}

Where, you need to pass userId of liferay user and it will return user information of that user.

{"emailId":"[email protected]", "Name":"Test Test"}

 

For more implementation details or support, you may contact us at [email protected].

contact-us Request a callback WhatsApp