Foros de discusión

How to create portlet with JSON response

David Knol, modificado hace 7 años.

How to create portlet with JSON response

Junior Member Mensajes: 29 Fecha de incorporación: 19/06/15 Mensajes recientes
Hi,
I am trying to create portlet, which after invoking returns JSON with data from database. What I'm tryintg to do is pretty similar to GetLayoutsAction class, which extends JSONAction. Can you guide me step by step what should I do? I have created new MVCPortlet so far. What should I do next? I'm fresh with Liferay.
thumbnail
David H Nebinger, modificado hace 7 años.

RE: How to create portlet with JSON response

Liferay Legend Mensajes: 14919 Fecha de incorporación: 2/09/06 Mensajes recientes
You can return json using the serveResource() method in the portlet, but I'd suggest going the service builder route so you can leverage the whole /api/jsonws entry point rather than trying to do within a portlet.
David Knol, modificado hace 7 años.

RE: How to create portlet with JSON response

Junior Member Mensajes: 29 Fecha de incorporación: 19/06/15 Mensajes recientes
Ok, thank you. Another thing. I've genereted a new MVCPortlet using Blade, so I have something like that:

package example.portlet;

@Component(
	immediate = true,
	property = {
		"com.liferay.portlet.display-category=category.sample",
		"com.liferay.portlet.instanceable=true",
		"javax.portlet.display-name=Example Portlet",
		"javax.portlet.init-param.template-path=/",
		"javax.portlet.init-param.view-template=/view.jsp",
		"javax.portlet.resource-bundle=content.Language",
		"javax.portlet.security-role-ref=power-user,user"
	},
	service = Portlet.class
)
public class ExamplePortlet extends MVCPortlet {

	@Override
	public void serveResource(ResourceRequest request, ResourceResponse response) throws IOException, PortletException {
		...
	}
	
}


What I'd like to do is to call this portlet from existing JS file using A.io.request and process received JSON. What will be URL of that resource? Is there something more I should do to make it works?