Wednesday, June 8, 2022

Creating Rest API in Liferay 7 (JAX-RS Web Services)

 

Today we will discuss how to create Rest Services in Liferay. By using JAX-RS we will create Rest Services that can be used by other applications. 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 

 




After that provide Class Name(DemoRest) and package name (com.portal.rest)





And Click Finish.

You will see new project is created and Application is appended with class as well as package.

Now Class name become DemoRestApplication and package name become com.portal.rest.application.


DemoRestApplication.java


Explanation:-

1)In Line 21 we define base URL as /greetings
2) JAX_RS_NAME is Greetings.Rest

 

Step 2:- Register JAX-RS Web Service

Your JAX-RS web service requires authorization by default. To enable this, you must create an OAuth 2.0 application to provide a way to grant access to your service.

Go to Control panel->Security->OAuth 2 Administration



then Click on + icon 




Provide
Application Name : Greeting
Client Profile : Headless Server
and click save.





When you click save clientId and Client Secret is Generated automatically.Now Click on scopes tab




Because we provide JAX_RS_NAME as Greetings.Rest so we will see a entry in scopes 



 
Check the check box and Click Save.

Step 3:- Test Your Services 

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 URL call

        URL :  http://localhost:8080/o/greetings/ 




C) Specific URL call

        URL :  http://localhost:8080/o/greetings/morning




D) URL with Path and Query Param

URL :  http://localhost:8080/o/greetings/morning/:name?drink=Cofee





No comments:

Post a Comment

Total Pageviews

Number Of Unique Visitor

Free counters!