Cosuming Liferay JSON Web Services

In liferay you can write json web service  easily.

You can also consume those web service from other client such in java httpclient, javascript(like ajax jquery), dukescript and other client .

For example you have a portlet name test-portlet and you write a json webservice as below:

    @JSONWebService(value = "testService", method = "POST")
    public JSONObject testService(String test) throws SystemException {
        JSONObject result = JSONFactoryUtil.createJSONObject();
        result.put("test", test);
        return result;
    }

For consuming this service from several clients you can follow below steps:

1. In java client

Here you can use jersy.

 Client code given below:

       Client    client = ClientBuilder.newClient();
           HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic( username, password );
        client.register(feature);
        WebTarget target = client.target("http://localhost:7070/api/jsonws/" + "invoke");
            java.util.Map<String, Object> dataPost = new HashMap<String, Object>();
            java.util.Map<String, Object> dataSend = new HashMap<String, Object>();
            dataSend.put("test", "test");

            String invokeMethod = "\"/test-portlet.tesentity/testService\"";
           
            dataPost.put(invokeMethod, dataSend);

    Response response = target.request(MediaType.APPLICATION_JSON).post(Entity.entity(dataPost.toString(), MediaType.APPLICATION_JSON), Response.class);
        Object result = response.readEntity(Object.class);

2. Call from JavaScript as ajax request

        var datatosend = {'s':'s'};
        var datatest = {};
        datatest["/test-portlet.tesentity/testService"] = datatosend;
        $.ajax({
            url : "http://localhost:7070/api/jsonws/" + "invoke",
            dataType : "json",
            data :{
                cmd: ko.toJSON(datatest)
            },
            type : "post",
            async : true,
            success : function(data) {
                console.log(data);
            },
            headers: {
                'Access-Control-Allow-Origin': '*'
                },
            beforeSend : function(xhr) {
               
                 xhr.setRequestHeader ("Access-Control-Allow-Origin" ,"*");
                 xhr.setRequestHeader ("Authorization", "Basic " + btoa('username' + ":" + 'pasword'));
               
            },
            complete : function() {

            },
            error : function() {
                //errorCallBack();
            }

        }); 

3. In dukescript

@Model(className = "TestDataProvider", targetId = "", properties = {})

public class CountryDataProviderModel {
   
    @OnReceive(method = "POST", url = "{url}", data = java.util.Map.class, headers = {"Authorization: {auth}"})
    public static void testData(TestDataProvider testData, Map<String,Object> result, Handler<Map<String,Object>> callback) {
        callback.execute(result);
    }

}




        String serverBaseURL = "http://localhost:7070/api/jsonws/" + "invoke";
        String auth = "Authorization", "Basic " + btoa('username' + ":" +'pasword');

        java.util.Map<String, Object> dataPost = new HashMap<String, Object>();
        java.util.Map<String, Object> datasend = new HashMap<String,Object>();
        String invokeMethod = "/test-portlet.tesentity/testService";
        dataPost.put(invokeMethod, datasend);
     
TestDataProvider provider = new TestDataProvider();
provider.testData(serverBaseURL, auth, dataPost, handlerclass);


Enjoy..

Comments

Popular posts from this blog

How to run ofbiz as ubuntu Service.

JPA vs Spring JPA vs Spring Data JPA vs Hibernate

Java Array Interview Questions for Entry-Level Developers Part 01