Webcenter Sites 12c Controllers as REST endpoints › Xumulus
post-template-default,single,single-post,postid-280,single-format-standard,select-core-1.5,pitch-child-child-theme-ver-1.2,pitch-theme-ver-3.4.2,ajax_fade,page_not_loaded,smooth_scroll,grid_1300,vertical_menu_with_scroll,blog_installed,wpb-js-composer js-comp-ver-6.6.0,vc_responsive

Webcenter Sites 12c Controllers as REST endpoints

The below was taken from the Sample site in Webcenter Sites 12c. It describes briefly how to create a rest service as an endpoint with a controller.  This means you don’t need to create any other entity such as a template to render the results, you can call this from a template (as shown) or you can call it as a JSON based rest service with JQuery or Angular.js etc. We found this very useful on our last project but a bit frustrating as it took us a while to figure out how to make POST work.  We will post more on that later.

Controllers can also be defined as REST resources. You can use the @Path annotation on any methods to define a subresource. These annotations are

dynamically scanned and made available as REST endpoints. The following example shows how to define one.

Sample Controller:

package oracle.webcenter.sites.controller

import java.util.Map;

import javax.ws.rs.FormParam;
import javax.ws.rs.*;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.*
import com.fatwire.assetapi.data.*
import com.openmarket.xcelerate.asset.*
import oracle.webcenter.sites.controller.Bean.UserBean;

public class cServices extends BaseController {
    public String welcome()
        return "Hello World";

    public Response doRegister(@FormParam("email") String email, @FormParam("password") String password, @FormParam("age") int age){
        String response = "doRegister called with user details :"+" Email : " + email + " Password : " + password + " Age : " + age
        return Response.status(200).entity(response).build()
    // it is invoked when this controller is associated to a template
    public void doWork(Map models) {  }

Sample Template

<%@ taglib prefix="cs" uri="futuretense_cs/ftcs1_0.tld"
%><%@ taglib prefix="satellite" uri="futuretense_cs/satellite.tld"   
            <satellite:form action="http://localhost:8080/cs/REST/controller/cServices/register" method="post">
                <input id="email" type="text" required placeholder="email" name="email"><br>
                <input id="password" type="password" placeholder="password" name="password"><br><br>
                <input type="number" placeholder="age" name="age" min="18" value = "22"><br>
                <input type="submit" value="Submit"><br>


In the above Controller, the @PATH annotation identifies the URI path to which the resource responds. @GET and @POST annotations specify that the resource can serve HTTP GET and POST requests. The @Produces and @Consumes annotations are used to specify the MIME media types that the resource can produce or consume.

The above Template renders a form and posts the form data to a controller resource.

http://localhost:8080/cs/REST/controller/cServices/register is the URI of the resource that responds to a PUT request. Cservices is the Controller, and /register is the path to the resource in the Controller defined by the annotation @PATH in the Controller, which in this case corresponds to the doRegister() method in the Controller. This method is then executed, and the response is sent back.

The @FormParam annotation can be used (in the Controller) to get the form parameters instead of explicitly sending them along with the Request.

The above code emits an HTML <form> tag suitable for using in a Satellite Server, Oracle WebCenter Sites, or mixed WebCenter Sites-Satellite Server environment.

WebCenter Sites uses the Jersey implementation of RESTful Web services. For more details about this implementation, refer to the Jersey user’s guide. 

More info to come on using these with POST and how to make the other REST verbs work.