RE: Cannot invoke service methods in MVC portlet class

Kevin Neibarger, modified 6 Years ago. Regular Member Posts: 105 Join Date: 2/2/18 Recent Posts
So, I'm facing an issue that is probably most likely with configuration and architecture of the service-builder. Essentially, I'm creating a service-builder "Liferay Module Project" and it creates two sub-projects, the *-api and *-service gradle projects. 

Within that same folder I'm creating another "Liferay Module Project", this time it's a mvc-portlet gradle project. From all the tutorials I've put together a main gradle build script that cleans, compiles, builds and deploys 3 osgi module JARs.

TestServiceBuilder
          TestServiceBuilder-api
          TestServiceBuilder-service
          TestServiceBuilder-web

When I build the 3 JARs and deploy them the portlet appears and loads fine into portal page. I am having issues calling a method on the FooLocalService from a Java class that extends MVCPortlet, the service Object is null everytime. I'm using ServiceReference annotation to get an instance of the class. Can anyone tell me why the below throws a NullPointerException?? Is there some configuration I'm missing?

public class TestServiceBuilderWebPortlet extends MVCPortlet {

@ServiceReference(type = com.test.service.builder.service.FooLocalService.class)
protected static com.test.service.builder.service.FooLocalService _fooLocalService;

@Override

public void render(RenderRequest renderRequest,        RenderResponse renderResponse) throws PortletException, IOException {
    try {       

System.out.println("\n -- TestServiceBuilderWebPortlet:render - Getting Foo service, is it null? "+ (_fooLocalService == null ? "YES" : "NO") + " -- \n");   

​​​​​​​List<Foo> fooList = _fooLocalService.getAllFoo();
​​​​​​​

    } catch (Exception e) {
        throw new PortletException(e);    }
    super.render(renderRequest, renderResponse);
}}
thumbnail
Ketan Savaliya, modified 6 Years ago. Regular Member Posts: 117 Join Date: 3/3/11 Recent Posts
Hi Kevin,

Have you given TestServiceBuilder-api module dependency to your TestServiceBuilder-web module  ? 


If no then you need give it.

If yes then you can use your service class referance like below. 

@Reference
    private UserLocalService _userLocalService;



​​​​​​​HTH!!
Kevin Neibarger, modified 6 Years ago. Regular Member Posts: 105 Join Date: 2/2/18 Recent Posts
Where would I do that? Is that through the bnd.bnd file or build.gradle?
thumbnail
David H Nebinger, modified 6 Years ago. Liferay Legend Posts: 14933 Join Date: 9/2/06 Recent Posts
Neither of those, you do it in place of the @ServiceReference annotation.

@ServiceReference is used only within service builder code, it is not an OSGi annotation.

You need to use the basic @Reference annotation to have OSGi inject the reference into your class at runtime.
Kevin Neibarger, modified 6 Years ago. Regular Member Posts: 105 Join Date: 2/2/18 Recent Posts
build.gradle in TestServiceBuilder-web

dependencies {

compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "3.0.0"

compileOnly group: "com.liferay.portal", name: "com.liferay.util.taglib", version: "2.6.0"

compileOnly group: "com.liferay.portal", name: "com.liferay.util.java", version: "3.0.0"

compileOnly group: "javax.portlet", name: "portlet-api", version: "2.0"

compileOnly group: "javax.servlet", name: "javax.servlet-api", version: "3.0.1"

compileOnly group: "jstl", name: "jstl", version: "1.2"

compileOnly group: "org.osgi", name: "osgi.cmpn", version: "6.0.0"

compileOnly project(':TestServiceBuilder-api')

compileOnly project(':TestServiceBuilder-service')

}