Fórum

How to create portlet with JSON response

David Knol, modificado 7 Anos atrás.

How to create portlet with JSON response

Junior Member Postagens: 29 Data de Entrada: 19/06/15 Postagens Recentes
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 7 Anos atrás.

RE: How to create portlet with JSON response

Liferay Legend Postagens: 14917 Data de Entrada: 02/09/06 Postagens Recentes
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 7 Anos atrás.

RE: How to create portlet with JSON response

Junior Member Postagens: 29 Data de Entrada: 19/06/15 Postagens Recentes
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?