In previous post, I have worked on introducing all basics about JSON Web Services.
In this post the focus is on accessing and consuming the the services and example
To Restrict a method from being exposed as service, just annotate the method with:
@JSONWebService(mode = JSONWebServiceMode.IGNORE)
To define custom HTTP method name instead of using the default, just annotate the method with:
@JSONWebService(value = "get-user", method = "GET")
public User getUserById( ...
Thus above service will be exposed and accessed as
http://localhost:8080/api/jsonws/user/get-user
instead of
http://localhost:8080/api/jsonws/user/get-user-by-id
To further customize the URL and remove the serviceClassname, just annotate the method with:
@JSONWebService("/get-user")
public User getUserById(..
Thus above service will be exposed and accessed as
http://localhost:8080/api/jsonws/get-user
instead of
http://localhost:8080/api/jsonws/user/get-user-by-id
json.web.service.enabled=true
by default the services are enabled and the property is set to true.
Enabling/Disabling particular HTTP methods
jsonws.web.service.invalid.http.methods=DELETE,POST,PUT
causes portal to accept only GET requests and ignore the HTTP methods specified above.
Below is the comma separated list of public service methods that can be accessed by unauthenticated user:
For all methods as unauthenticated use below
jsonws.web.service.public.methods=*
For is methods as unauthenticated use below
jsonws.web.service.public.methods=is*
Go to Next post Part 3: Custom Liferay Plugin JSON Web Services
References:
The article is my summarized view while working on the topic and references have been taken from Liferay community and documentation
In this post the focus is on accessing and consuming the the services and example
To Restrict a method from being exposed as service, just annotate the method with:
@JSONWebService(mode = JSONWebServiceMode.IGNORE)
To define custom HTTP method name instead of using the default, just annotate the method with:
@JSONWebService(value = "get-user", method = "GET")
public User getUserById( ...
Thus above service will be exposed and accessed as
http://localhost:8080/api/jsonws/user/get-user
instead of
http://localhost:8080/api/jsonws/user/get-user-by-id
To further customize the URL and remove the serviceClassname, just annotate the method with:
@JSONWebService("/get-user")
public User getUserById(..
Thus above service will be exposed and accessed as
http://localhost:8080/api/jsonws/get-user
instead of
http://localhost:8080/api/jsonws/user/get-user-by-id
Portal Configuration
JSON Web Services can be enabled or disabled by setting the below portal property:json.web.service.enabled=true
by default the services are enabled and the property is set to true.
Enabling/Disabling particular HTTP methods
jsonws.web.service.invalid.http.methods=DELETE,POST,PUT
causes portal to accept only GET requests and ignore the HTTP methods specified above.
Below is the comma separated list of public service methods that can be accessed by unauthenticated user:
For all methods as unauthenticated use below
jsonws.web.service.public.methods=*
For is methods as unauthenticated use below
jsonws.web.service.public.methods=is*
Accessing service via HTML form
<form action="http://localhost:8080/api/jsonws/user/get-user-by-id" method="GET"> <input type="hidden" name="userId" value="10172"/> <input type="submit" value="submit"/> </form>When submitted will result in response consisting of User object as JSON string.
Accessing service via Java based Apache HttpClient API
Below is the example to access the Liferay services using the Apache HTTPClient API
public static void get() throws Exception { HttpHost targetHost = new HttpHost("localhost", 8080, "http"); DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getCredentialsProvider().setCredentials( new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("test", "test")); // Create AuthCache instance AuthCache authCache = new BasicAuthCache(); // Generate BASIC scheme object and add it to the local // auth cache BasicScheme basicAuth = new BasicScheme(); authCache.put(targetHost, basicAuth); // Add AuthCache to the execution context BasicHttpContext ctx = new BasicHttpContext(); ctx.setAttribute(ClientContext.AUTH_CACHE, authCache); HttpPost post = new HttpPost( "/api/jsonws/user/get-user-by-id"); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("userId", "10172")); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8"); post.setEntity(entity); HttpResponse resp = httpclient.execute(targetHost, post, ctx); System.out.println(resp.getStatusLine()); resp.getEntity().writeTo(System.out); httpclient.getConnectionManager().shutdown(); }When submitted will result in response consisting of User object as JSON string.
Go to Next post Part 3: Custom Liferay Plugin JSON Web Services
References:
The article is my summarized view while working on the topic and references have been taken from Liferay community and documentation
Hi,
ReplyDelete1. I want to access liferay jsonws service with jquery or ajax call like below code.
If i use localhost then it is working. But if i use 127.0.0.1 in place of localhost it is throwing error.
I am getting the [Exception... "Access to restricted URI denied" code: "1012" nsresult: "0x805303f4 (NS_ERROR_DOM_BAD_URI)" location: ""]
but i configured all properties in portal-ext property file and i configured to enable cors
my sample code works if localhost is there otherthan ip address
$.ajax({
url: 'http://127.0.0.1:8080/api/jsonws/country/get-countries',
dataType: "json",
data:{},
type: "get",
headers: {
'Access-Control-Allow-Origin': '*'
},
success: function(data){
$.each(data, function(index,item) {
alert(item.name);
});
},
error: function(e){
alert(e.statusText);
}
});
Your new valuable key points imply much a person like me and extremely more to my office workers. With thanks from every one of us.
ReplyDeleteBest AWS Training in Chennai | Amazon Web Services Training in Chennai
All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.
ReplyDeleteangularjs Training in bangalore
angularjs Training in btm
angularjs Training in electronic-city
angularjs Training in online
angularjs Training in marathahalli
Resources like the one you mentioned here will be very useful to me ! I will post a link to this page on my blog. I am sure my visitors will find that very useful
ReplyDeletepython interview questions and answers | python tutorials
Awesome! Education is the extreme motivation that open the new doors of data and material. So we always need to study around the things and the new part of educations with that we are not mindful.
ReplyDeleteJava training in Indira nagar | Java training in Rajaji nagar
Java training in Marathahalli | Java training in Btm layout
informative blog , keep posting java course in satara
ReplyDeletegreat, valuable post , i have learned many intresting thing about sql from your blog, thanks . keep posting and checking out my blog if your intresting in python course in satara
ReplyDeleteNice post.
ReplyDeleteSQL training in Pune