Search code examples
javaweb-servicesliferayliferay-service-builder

Build Liferay web service with optional parameter


I need to extend an existing Liferay webservice (created with Service Builder) to handle an additional optional parameter.

With Service Builder, you have to specify every parameters inside the method signature:

public String getList(String param1){ .. }

This creates a get-list web service accepting a parameter named param1. You have to specify every parameters when making the call or else the call will fail. If you need optional parameters, just pass an empty value and handle the missing parameter inside the code.

My problem is backward compatibility: this web service is already used by a mobile app and I cannot change the call made by the app. The additional parameter must be handled without changing the method signature.

Taking a look at BaseServiceImpl, I tried to obtain the parameter in this way:

HttpServletRequest request = com.liferay.util.axis.ServletUtil.getRequest();
String value = ParamUtil.getString(request, "param-name");

But it throws a NoClassDefException regarding com.liferay.util.axis.ServletUtil.

Is there a way to actually do this?


Solution

  • To enhance and retain backward compatibility of your code, one way is to overload getList() method which accepts additional parameter. You can achieve this by following:

    1. Move your general pre-logic code of getList() to getList(String param1) method.
    2. Add filter for param1 in getList(String param1) to handle case when parameter is not null / empty.
    3. Call getList(null) from getList().

    While you can call getList(String param1) directly when you require to pass additional parameter.

    Original method:

    public String getList(){
        return getList(null);
    }
    

    Overriden method:

    public String getList(String param1){
    
        if(param1 != null){
            // logic for param1
        }
    
        // rest of your general code
    }