Custom REST service in XPages using a service bean
Talking to your backend using JSON and REST is all the rage for contemporary development. Domino has supported, at least reading, this access for quite a while using
However, as a result, your front-end application now needs to deal with the Domino way to present data, especially the odd use of @ in JSON keys (which e.g. jquery isn't fond of). Contemporary approaches mandate that you minimize the data you send over the wire and send data in your business structure, not in your database format. Furthermore, when sending data back, you want to validate and act on the data.
In the Extension Library there is the REST control, that you can use instead of the DAS service. It allows you to define what you want to expose as XML or JSON. There are a number of predefined service, but my favorite is the
You need to extract the method (GET, POST, PUT, DELETE) from the HttpServletRequest so you can take the appropriate action.
As usual YMMV
?ReadViewEntries[&OutputFormat=JSON]
. Using Domino Access Services (DAS) this has been extended to read/write support for documents as well.
However, as a result, your front-end application now needs to deal with the Domino way to present data, especially the odd use of @ in JSON keys (which e.g. jquery isn't fond of). Contemporary approaches mandate that you minimize the data you send over the wire and send data in your business structure, not in your database format. Furthermore, when sending data back, you want to validate and act on the data.
In the Extension Library there is the REST control, that you can use instead of the DAS service. It allows you to define what you want to expose as XML or JSON. There are a number of predefined service, but my favorite is the
customRestService
. When you use the custom service, you can write JavaScript for all events happening: doGet
, doPost
, doPut
and doDelete
, but you also can use a service bean. A service bean is not a managed bean, so you don't need to specify anything in your faces-config.xml
. However it is a little special. A sample XPage could look like this:
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core"
xmlns:xe="http://www.ibm.com/xsp/coreex">
<h1>This is the landing page of the orgSearch Service</h1>
<p>Please use "search.xsp/json" for the actual query</p>
<xe:restService id="JSONSearch" pathInfo="json" state="false">
<xe:this.service>
<xe:customRestService contentType="application/json"
serviceBean="com.notessensei.demo.CustomSearchHelper">
</xe:customRestService>
</xe:this.service>
</xe:restService>
</xe:restService>
</xp:view>
if your page name is demo.xsp
then the access to the service based on the pathInfo property is demo.xsp/json
.To implement the service bean your java class needs to extend the CustomServiceBean
. There is only one method you need to overwrite: renderService
. It gives you access to the CustomService object, that sits on the XPage and the RestServiceEngine, that provides access to the request/response. It can look like this:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ibm.domino.services.ServiceException;
import com.ibm.domino.services.rest.RestServiceEngine;
import com.ibm.xsp.extlib.component.rest.CustomService;
import com.ibm.xsp.extlib.component.rest.CustomServiceBean;
public class CustomSearchHelper extends CustomServiceBean {
@Override
public void renderService(CustomService service, RestServiceEngine engine) throws ServiceException {
HttpServletRequest request = engine.getHttpRequest();
HttpServletResponse response = engine.getHttpResponse();
response.setHeader("Content-Type", "application/json; charset=UTF-8");
// Your code goes here!
}
}
You need to extract the method (GET, POST, PUT, DELETE) from the HttpServletRequest so you can take the appropriate action.
As usual YMMV
Posted by Stephan H Wissel on 22 October 2014 | Comments (6) | categories: XPages