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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.portal.rest.application; | |
import java.util.Collections; | |
import java.util.Set; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.PathParam; | |
import javax.ws.rs.Produces; | |
import javax.ws.rs.QueryParam; | |
import javax.ws.rs.core.Application; | |
import org.osgi.service.component.annotations.Component; | |
import org.osgi.service.jaxrs.whiteboard.JaxrsWhiteboardConstants; | |
/** | |
* @author adit2 | |
*/ | |
@Component( | |
property = { | |
JaxrsWhiteboardConstants.JAX_RS_APPLICATION_BASE + "=/greetings", | |
JaxrsWhiteboardConstants.JAX_RS_NAME + "=Greetings.Rest" | |
}, | |
service = Application.class | |
) | |
public class DemoRestApplication extends Application { | |
public Set<Object> getSingletons() { | |
return Collections.<Object>singleton(this); | |
} | |
//http://localhost:8080/o/greetings/ | |
@GET | |
@Produces("text/plain") | |
public String working() { | |
return "It works!"; | |
} | |
//http://localhost:8080/o/greetings/morning | |
@GET | |
@Path("/morning") | |
@Produces("text/plain") | |
public String hello() { | |
return "Good morning!"; | |
} | |
//http://localhost:8080/o/greetings/morning/:name?drink=Cofee | |
@GET | |
@Path("/morning/{name}") | |
@Produces("text/plain") | |
public String morning( | |
@PathParam("name") String name, | |
@QueryParam("drink") String drink) { | |
String greeting = "Good Morning " + name; | |
if (drink != null) { | |
greeting += ". Would you like some " + drink + "?"; | |
} | |
return greeting; | |
} | |
} |
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