We've been working in the last few days with our partner VASS defining the best design for a very large customer who had the requirement of avoiding full page reloads when the users interact with a given application.
The team at VASS had already decided to use Service Builder to save time developing the persistance layer and was looking for the best way to implement the frontend of the application. In this situation using the JSON API that is automatically generated for Service Builder is a perfect solution. It provides a ready-to-use JavaScript API that you can use as if it was local to the browser and it handles all the AJAX calls and serialization and deserialization. The implementation is in the server which allows you to use the full power of Java and associated technologies to implement any business requirement needed.
For those who have heard about DWR, this follows a similar strategy but if you are already using Service Builder everything is done for you and you just have to use the js API.
While thinking about this we realized that this is one of those little gems that Liferay has that is not very well known because it's not properly documented. So we decided to change that and let the world know about it. As an introduction here is a quick example.
Assuming you have the following method in the remote service:
public class ReportsEntryServiceImpl extends ReportsEntryServiceBaseImpl {
public void print(String msg) {
System.out.println("ReportsEntryService: " + msg);
}
....
}
The method could be invoked from a JSP using the following method (to make it simpler we are linking to the js file directly):
<script src="/html/js/liferay/ext_service.js" language="JavaScript"> </script>
<script language="JavaScript">
Liferay.Service.Reports.ReportsEntry.print(
{
msg: "Invoking a Liferay service via JavaScript",
},
function(message) {
var exception = message.exception;
if (!exception) {
// Process Success
}
else {
// Process Exception
}
}
);
</script>
For more detailed information check the new wiki article: JSON Service API
Have fun!

