tags:

views:

240

answers:

1

Hello,

As RESTful web services is url based and is not a object, we can't call methods on it. I have a simple web service with only one method in it with is @GET. I saw one screencast and it used some javascript library to consume the web service. But, how do i use it with my JSF project? I can't even inject it like a normal web service. Please help. I am new to REST. Can i not consume it in my managed bean?

If the only way to consume the webservice is through javascript, can anybody here give me details of how to consume it through JQuery?

Thanks in advance :)

+1  A: 

You can consume it in your managed bean with no problem. RESTful Web Services usually return JSON or XML formatted objects. You can call the restful web service and depending on the format of its response, parse it either using an XML parser or a JSON parser, or even better use a mapper to map the response to a Java object and use it elsewhere in your application.

Java-JSON mapping libraries are discussed here: http://stackoverflow.com/questions/338586/a-better-java-json-library

You can use JAXB for XML-Java mapping: https://jaxb.dev.java.net/tutorial/

An XML mapper, maps an XML document to a Java object.

For example, if the response from the Web Service you are using is:

<SampleResponse>
<firstName>James</firstName>
<lastName>Gosling</lastName>
</SampleResponse>

An XML mapper can convert that to an instance of the following class:

public class SampleResponse {
private String firstName;
private String lastName;
// setters and getters
}

In a way like this:

SampleResponse myResponseObj = mapper.fromXML(xmlRespnse);

JSON mappers work in a similar manner.

Bytecode Ninja
Sorry, but i did not understand what is mapper and what parsers? From where do i get those parsers? If you can give me some links to some tutorial i will be very grateful.
Ankit Rathod
JSON mappers are already discussed here: http://stackoverflow.com/questions/338586/a-better-java-json-libraryYou might want to use JAXB for mapping XML responses to Java objects: https://jaxb.dev.java.net/tutorial/
Bytecode Ninja