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


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


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


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

Number Of Unique Visitor

Free counters!