« Back

Liferay JAX-RS service

General Blogs August 16, 2017 By sejal patel

 Liferay 7 bolster for creating and deploying JAX-RS based REST services.

JAX-RS is the JEE standards based approach for creating REST web services. As far as building up the real administrations it basically utilizes an explanation based approach,where one improves strategies in Java classes with comments, for example, @GET, @POST, @Path and so forth.

In this post, we will examine about liferay JAX-RS service . Following are some prerequisite to implement liferay JAX-RS service 

Prerequisite:

1. Liferay 7 must be installed

2.Eclipse neon

3.Java 1.8 

Configure liferay JAX-RS service :

Now, lets understand the steps to generate JAX-RS Service as follow,

1.To create a liferay workspace in ide follow the following steps.

     File Menu ->New-> Select Liferay workspace

    Upon creation of the workspace, the structure should look like below 

 

2.To create rest module in ide follow the following steps

  File Menu -> New->Click on Liferay Module Project. Fill up the detail as follow,

  Project Name: enter appropriate name e.g :":Rest-Services-Demo-rest"

  Project Template Name: Select :"rest"

3. update gradle dependencies with below and run the gradle build task

dependencies {

 compileOnly group: "javax.ws.rs", name: "javax.ws.rs-api", version: "2.0.1"

 compileOnly group: "org.osgi", name: "org.osgi.service.component.annotations", version: "1.3.0"

 compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.0.0"

 compileOnly group: "com.liferay.portal", name: "com.liferay.util.java", version: "2.0.0"

 compileOnly group: "com.liferay.portal", name: "com.liferay.portal.impl", version: "2.0.0"

 compile group: 'com.liferay', name: 'com.liferay.osgi.util', version: '1.0.0'

 compileOnly project(":modules:Rest-Services-Demo:Rest-Services-Demo-api")

 compileOnly project(":modules:Rest-Services-Demo:Rest-Services-Demo-service")

}

4.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 CXFEndpoint from “/src/main/resources/configuration/com.liferay.portal.remote.cxf.common.configuration.CXFEndpointPublisherConfiguration-cxf “ file 

contextPath=/student-rest

authVerifierProperties=auth.verifier.BasicAuthHeaderAuthVerifier.urls.includes=*

5.Configuring REST Extenders

We can configure REST Extenders from /src/main/resources/configuration/com.liferay.portal.remote.rest.extender.configuration.RestExtenderConfiguration-rest file.

contextPaths=/student-rest

jaxRsServiceFilterStrings=(component.name=Rest.Services.Demo.rest.application.RestServicesDemoRestApplication)

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

http://localhost:8080/o/student-rest

6.Publishing JAX-WS RS Services

Liferay creates REST Component with sample methods, so edit the RestServicesDemoRestApplication class that returns Student objects as JSON

@ApplicationPath("/student")

@Component(immediate = true, service = Application.class,

configurationPid = "com.enprowess.rest.RestServicesDemoRestApplication",

configurationPolicy = ConfigurationPolicy.OPTIONAL,property={"jaxrs.application=true"})

public class RestServicesDemoRestApplication extends Application {

      public Set<Object> getSingletons() {

             return Collections.<Object>singleton(this);

      }

 private StudentLocalService studentLocalService;

  @Reference(unbind="-")

  public void setLeaveLocalService(StudentLocalService studentLocalService){

         this.studentLocalService=studentLocalService;

   }

   @GET

   @Path("/student-info/{studentId}")

   @Produces(MediaType.APPLICATION_JSON)

  public String getLeave(@PathParam("studentId") long studentId){

       Student student = null;

       String jsonString = null;

     try {

                student = studentLocalService.getStudent(studentId);

                jsonString = JSONFactoryUtil.serialize(student);

        } catch (Exception e) {

         e.printStackTrace();

       }

     return jsonString;

    }

}

7.Liferay REST Extender Configuration :

    Deploy the rest module in liferay server.

     Click on Control Panel -> Configuration -> System Settings 

     Search for REST ->Click on REST Extender

     Click on the configuration and you can see the deployed module specified context paths. 

 

To access rest service use following examples:

Liferay 7 begins with context /o unlike in Liferay 6 with/c

http://localhost:8090/o/student-rest/student/student-info/1

Threaded Replies Author Date
If I want to add authentication on particular... Abhishek Suthar August 28, 2017 7:18 AM
I would like to add a jaxrs filter,... Robert Smith November 13, 2017 11:14 AM

If I want to add authentication on particular path,how can I do that?
Posted on 8/28/17 7:18 AM.
I would like to add a jaxrs filter, specifically a ContainerRequestFilter to receive the request prior to it hitting the service. Any idea how to add a working filter?
Posted on 11/13/17 11:14 AM.