Calling Spring Boot APIs from Liferay – Step-by-Step Integration Guide

In enterprise applications, it is very common to integrate Liferay Portal with Spring Boot microservices.
A typical use case is when a Liferay portlet or service needs to call an external REST API built using Spring Boot.

In this guide, you will learn:

  • how to call Spring Boot APIs from Liferay

  • common integration approaches

  • step-by-step code examples

  • best practices for production


🔍 Why Integrate Liferay with Spring Boot?

Liferay is a powerful portal and content platform, while Spring Boot is ideal for building scalable microservices.

Common scenarios:

  • Liferay UI calling Spring Boot backend

  • Liferay workflows invoking business services

  • Headless APIs consumed by Liferay

  • External system integration

This architecture keeps:

  • Liferay lightweight

  • business logic isolated

  • services independently scalable


🧭 Architecture Overview


📊 Diagram 1: Liferay Calling Spring Boot API



🚀 Approach 1: Calling Spring Boot API from Liferay Java Code

This is the most common and recommended approach.


Step 1: Add HTTP Client Dependency in Liferay

If you are using Liferay DXP 7.2+, you can use:

import com.liferay.portal.kernel.util.HttpUtil;

Or Apache HttpClient (inside OSGi module):

<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.14</version> </dependency>

Step 2: Call Spring Boot API

String apiUrl = "http://localhost:8080/api/customers"; Http.Options options = new Http.Options(); options.setLocation(apiUrl); options.setMethod(Http.Method.GET); String response = HttpUtil.URLtoString(options); System.out.println("Response: " + response);

Step 3: Parse JSON Response

JSONObject json = JSONFactoryUtil.createJSONObject(response); JSONArray customers = json.getJSONArray("data"); for (int i = 0; i < customers.length(); i++) { JSONObject obj = customers.getJSONObject(i); System.out.println(obj.getString("name")); }

🚀 Approach 2: Calling Spring Boot API from Liferay Service Builder

Use this when:

  • the call is part of a backend service

  • multiple portlets reuse it


public String fetchCustomerData() { String apiUrl = "http://localhost:8080/api/customers"; Http.Options options = new Http.Options(); options.setLocation(apiUrl); options.setMethod(Http.Method.GET); try { return HttpUtil.URLtoString(options); } catch (Exception e) { _log.error("Error calling Spring Boot API", e); return null; } }

🚀 Approach 3: Calling Spring Boot API from Liferay JavaScript (AJAX)

If Spring Boot exposes CORS-enabled APIs, Liferay frontend can call them directly.


fetch("http://localhost:8080/api/customers") .then(response => response.json()) .then(data => console.log(data)) .catch(err => console.error(err));

⚠️ Use this only for:

  • public APIs

  • authenticated APIs via tokens

  • not for sensitive backend logic


🔐 Security Considerations


1️⃣ CORS Configuration (Spring Boot)

@CrossOrigin(origins = "http://localhost:8080") @RestController public class CustomerController { }

2️⃣ Token-Based Authentication

Pass Authorization header:

options.addHeader("Authorization", "Bearer " + token);

3️⃣ HTTPS in Production

Never expose Spring Boot APIs over plain HTTP.


🧪 Error Handling & Timeouts

Always handle:

  • timeouts

  • 4xx / 5xx responses

  • network failures


options.setTimeout(5000); try { String response = HttpUtil.URLtoString(options); } catch (Exception e) { _log.error("API call failed", e); }

🚨 Common Issues & Fixes


❌ CORS Error

Fix: Enable CORS in Spring Boot.


❌ Timeout Exception

Fix: Increase timeout.


❌ JSON Parse Error

Fix: Validate API response.


❌ 401 Unauthorized

Fix: Add Authorization header.


✅ Best Practices for Production

✔ Use Service Builder or OSGi services
✔ Add retries and timeouts
✔ Use HTTPS
✔ Secure APIs with OAuth2
✔ Log API calls
✔ Handle failures gracefully
✔ Cache responses where possible


📌 Quick Summary (TL;DR)

Goal: Call Spring Boot API from Liferay
Options:

  1. Java backend call (recommended)

  2. Service Builder integration

  3. JavaScript frontend call

Production Tips:
Secure APIs, handle errors, add timeouts.


❓ Frequently Asked Questions (FAQ)


❓ Can Liferay directly call Spring Boot REST APIs?

Yes, via Java HTTP client or frontend AJAX.


❓ Which is better: backend or frontend call?

Backend call is safer and recommended.


❓ How do I secure Spring Boot APIs for Liferay?

Use OAuth2 or token-based auth.


❓ Can I call Spring Boot APIs from Liferay Workflow?

Yes, using Java service calls.


🔗 Related Articles

  • Liferay Service Builder Explained

  • Liferay OSGi Development Guide

  • Spring Boot REST API Best Practices


👩‍💻 Final Tip

Always keep business logic in Spring Boot
and presentation in Liferay.

That makes your system scalable and clean.

💼 Need Help with Liferay, Alfresco, Jira, or Enterprise Content Management?

I help teams solve real production issues and build scalable systems.

Services I offer:
• Liferay, Alfresco setup, customization, and integration
• Repository structure design and content modeling  
• Workflow implementation (jBPM / Activiti / APS)  
• Java backend development, multithreading, and concurrency optimization  
• Performance tuning and production issue troubleshooting  
• Java, Spring Boot & microservices architecture  
• Production issue troubleshooting  


📩 Email: ishikhanirankari@gmail.com | info@realtechnologiesindia.com

✔ Available for quick consulting calls and project-based support
✔ Response within 24 hours

🎥 Learn IT with Shikha on YouTube

Prefer learning through videos? Watch practical tutorials on Kafka, Camunda, Alfresco, Java, Spring Boot, Microservices and Enterprise Architecture.

▶ Subscribe to Learn IT with Shikha on YouTube

Comments

Popular posts from this blog

Top 50 Camunda BPM Interview Questions and Answers for Developers (2026 Guide)

10 BPMN Best Practices Every Camunda Developer Should Know

OOPs Concepts in Java | English | Object Oriented Programming Explained