Public JAX-RS Application Implementation

Public JAX-RS Application Implementation

Overview

This article will explain how to create a JAX-RS REST application, which will be publicly accessible. 

Prerequisites 

Create a Liferay Gradle Workspace with a Target Platform enabled.


JAX-RS Module Implementation

Create a new ‘rest’ module:


Created module structure:

 

bnd.bnd file:

Bundle-Name: liferay-jax-rs-sample
Bundle-SymbolicName: com.liferay.jax.rs.sample
Bundle-Version: 1.0.0
Liferay-Configuration-Path: /configuration


build.gradle file:

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"
}

Once you deploy sample generated JAX-RS application, and try to access it by URL http://localhost:8080/o/greetings - you’ll see the “Access denied” error:

  

Fixing Access Denied Error


There are two ways to make the JAX-RS application work without authentication. 

1. Configuration approach. 

Go to Control Panel > Configuration > Service Access Policy:

Edit the “SYSTEM_DEFAULT” access policy.

Click “Switch to Advanced Mode”:

Add the JAX-RS application class methods signature to configuration:

Now the REST URL should work:

2. Programmatic approach

To make the JAX-RS application publicly available - you may set the following properties:

"auth.verifier.guest.allowed=true",
"liferay.access.control.disable=true"

Sample:

@Component(
  property = {
     JaxrsWhiteboardConstants.JAX_RS_APPLICATION_BASE + "=/greetings",
     JaxrsWhiteboardConstants.JAX_RS_NAME + "=Greetings.Rest",
     "auth.verifier.guest.allowed=true",
     "liferay.access.control.disable=true"
  },
  service = Application.class
)
public class JaxRSSampleAppApplication extends Application {
//
}

With these properties the JAX-RS application will be available without the additional configuration.

Enjoy 😏

Comments

Popular posts from this blog

Liferay Search Container Example

Liferay DXP - max upload file size

Liferay Keycloak integration