Wednesday, June 8, 2022

JAX-RS Web Services with Spring in Liferay 7 (DXP)

Share it Please

 

We already know how to create Rest API in Liferay 7 (JAX-RS Web Services). Today we will see how we can use spring and add objects in Liferay databases. Here we will create a Rest service that will add data in Liferay database. Before reading this blog it is highly recommended to read how to create Rest API in Liferay 7 (JAX-RS Web Services). Here i am using Liferay 7.3.

So lets start this step by step :-


Step 1:- Create a simple Rest Portlet

Create a simple Liferay module Project and select Rest Template provide class name and package name and Click Finish. Now open your java file and paste this :- 


EmployeeRestApplication.java

package com.liferay.employee.application;
import com.liferay.employee.bean.Employee;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import java.util.Collections;
import java.util.Set;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.jaxrs.whiteboard.JaxrsWhiteboardConstants;
import org.springframework.web.bind.annotation.RequestBody;
/**
* @author adit2
*/
@Component(
property = {
JaxrsWhiteboardConstants.JAX_RS_APPLICATION_BASE + "=/employee",
JaxrsWhiteboardConstants.JAX_RS_NAME + "=Employee.Rest"
},
service = Application.class
)
public class EmployeeRestApplication extends Application {
public Set<Object> getSingletons() {
return Collections.<Object>singleton(this);
}
//http://localhost:8080/o/employee/
@GET
@Produces("text/plain")
public String working() {
return "Get API by Aditya..";
}
//http://localhost:8080/o/employee/add-employee
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/add-employee")
public Response addEmployee(@RequestBody Employee employee) {
System.out.println("Hello : "+employee);
//Add Employee by using LocalServiceUtil
JSONObject obj = JSONFactoryUtil.createJSONObject();
obj.put("message", "Employee Added Successfully..");
return Response.status(Response.Status.CREATED).entity(obj.toString()).build();
}
}


Explanation:-

1)In Line 28 we define base URL as /employee

2) JAX_RS_NAME is Employee.Rest

So we register this with same name in OAuth2 Administration.

Note:- How to register our web service you can read my  previous blog.

During registration select create/Read/Update all checkboxes






3) Here in line 51, addEmployee method we use @RequestBody which belong to Spring Frame work. Also we use JSONFactoryUtil so we need to add the dependencies in  build.gradle 

Step 2:- Add Dependencies in build.gradle

Now open build.gradle and paste the dependencies :- 


build.gradle

dependencies {
compileOnly group: "javax.ws.rs", name: "javax.ws.rs-api"
compileOnly group: "org.osgi", name: "org.osgi.service.component.annotations"
compileOnly group: "org.osgi", name: "org.osgi.service.jaxrs"
//Add these two dependencies
compileOnly group: "com.liferay.portal", name: "release.dxp.api"
implementation group: 'org.springframework', name: 'spring-web', version: '4.3.11.RELEASE'
}
view raw build.gradle hosted with ❤ by GitHub


Step 3:- Create Employee Bean

Create Employee.java that contain setter and getters which can parse Json to Employee object and used with @RequestBody 


Employee.java

package com.liferay.employee.bean;
public class Employee {
private int eid;
private String firstName;
private String lastName;
private String gender;
private int age;
public int getEid() {
return eid;
}
public void setEid(int eid) {
this.eid = eid;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Employee [eid=" + eid + ", firstName=" + firstName + ", lastName=" + lastName + ", gender=" + gender
+ ", age=" + age + "]";
}
}
view raw Employee.java hosted with ❤ by GitHub


Step 4:- Test your API

Deploy your project and test the API using postman

Now you call your services but first you need to generate access_token by using client_id and client_secret which is generated in step 2


A)Call to generate access_ token

        URL :  http://localhost:8080/o/oauth2/token 





Now this access token is used with Bearer in next Calls.

B) Base/Get call

    URL :  http://localhost:8080/o/employee/



C) Add Employee/Post  call

    URL :  http://localhost:8080/o/employee/add-employee

In body Pass JSON



In Header Pass Authorization and Content-type




On Console you will see Employee Object


Project Structure

           



No comments:

Post a Comment

Total Pageviews

1038748

Number Of Unique Visitor

Free counters!