Monday, May 29, 2017

How to expose Regex based rest service in AEM

How to expose Regex based rest service in AEM

This post will explain the approach to expose the regex based rest service in AEM. By default OSGI will not support exposing regex based rest services and it will only support the services based on the specified Path or Resource Type.

Install OSGI JAX-RS connector:

Install(/system/console/bundles) jersey-all, publisher, provider-security and other required bundles e.g. provider-gson for JSON support and make sure the bundles are in Active state.

The bundles can be downloaded from following URL -

Develop the Servlet with required path mapping:

package com.albinsblog.samples.core;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component(metatype = false)
@Path("/{catagroy}/{title}/p/{code : \\d{5}}")
public class RegexServlet {

public String getProductDetails(@Context HttpServletRequest request, @Context HttpServletResponse response,@PathParam("catagroy") String catagroy,@PathParam("title") String title,@PathParam("code") String code) {

  return "code="+code+";catagroy="+catagroy+";title="+title;


Add the following dependency in POM.xml


The servlet will accept the request with matching pattern - the Servlet path should be starting with /services

The Path Regex pattern specified in the Servlet will match for the following URL  - localhost:4502/services/categoryTest/Sampletitle/p/12345 (Code should be 5 digit)

No comments:

Post a Comment