Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
Cannot invoke service methods in MVC portlet class
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);
}}
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);
}}
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.
HTH!!
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!!
Where would I do that? Is that through the bnd.bnd file or build.gradle?
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.
@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.
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')
}
Copyright © 2025 Liferay, Inc
• Privacy Policy
Powered by Liferay™