How can i pass a complex object like Person class as an input parameter in restful web service??
I have gone through many links but didnt find the solution. Please help
Transform the object into JSON or XML (or any other format, but these are the most common), and send it into the body of the request.
There are Restful Client api's available in java
1.JAX-RS-API-->JersyImpl,Resteasy(Jboss) Impl etc
You can use any of these and can pass any java object as input to restful resource.
Related
I have an xml input file from the following URL,
http://maps.googleapis.com/maps/api/geocode/xml?address=new%20york%20&%20sensor=false;
i would like to read the latitude and longitude positions from the XML file and i intend to write a method in the RESTful web service which calculate the location with minimum distance from each other.
i am very new to web services but i am aware that i should write my logic within the GET method and the #consumes and #produces will be XML.
i need some help to understand how should i pass the XML to the web service.
Should i use JDOM to extract the value from the requires tag and pass it as a parameter to the web service ?
need some help !
HTTP GET method won't work here since you need to send some data in the payload. You can use POST method which allows you to send a message body.
I am new to restlet development. I want to develop an application using restlet which takes input as xml and returns xml as response.
I could not find it on official RESTLET website. Please someone guide how to proceed on this.
Thanks & Regards
I am not familiar with RESTLET, but according to their documentation page here: http://restlet.com/learn/tutorial/2.2/#part03
it is really easy to get XML in and out. XML is a simple String which follows some conventions. In the get method, just receive xml as String, parse it (ie. with SAXParser), get necessary information. And return value should also be String. Just make the XML String yourself and return it from the #Get annotated method as in example.
Say ,i have producer in java and consumer in dot net. Producer has a method that takes
Employee as method parameter and creates employee in db.
For SOAP based ws, dot net client will hit WSDL and creates the stubs (including employee data representation in dot net). Now it can fill
the object and send to producer.
I am not sure how it will work in restful webservices as there is no WSDL. How rest consumer will get to know what are the operations
exposed by producer without any WSDL contract and how dot net consumer will get stubs (like employee data object) so that it can simply fill it and send across?
I know there is WADL(parallel to WSDL) in rest but looks like its not very prominent and not a standard as of now.
I am not getting how client side code will generate EmployeeData class so that it can fill it and send to producer? Will client manually create extra class (instead of proxy EmployeeData that used to be generated on the basis of WSDL using utilities available at client side)? Even if client has to do it manually, how client will know what is the class definition of EmployeeData class without wsdl or wadl?
One important concept of REST is HATEOAS or Hypermedia as the Engine of Application State. What this means is that your client interacts with the REST service through hypermedia links that the service hands it.
Your REST web service has an entry point, say http://yourhost.com/rest. Your client will start by sending the request to that URL. Your service will respond with a resource that describes some or all the accessible resources and how to access them. You keep discovering and following links. This is how the API is published (and discovered).
Here's an awesome video describing this concept: Hypermedia APIs.
Through HATEOAS you can make your service API completely discoverable by just following hypermedia links.
There is no concept of top down/bottom up design in REST.
REST is about resources, not about method calls, which is basically what a WSDL describes.
Even if client has to do it manually, how client will know what is
the class definition of EmployeeData class without wsdl or wadl?
It won't need to create an EmployeeData class. Say you needed to create a new Employee, you would send a GET request to /employees which would possibly return a response containing how to do that. That might be an XHTML response like so (among other things)
<form class="new-employee" action="/context/employees" method="PUT" >
<input type="text" name="employee_name" />
<input type="text" name="employee_age" />
<input type="submit" name="submit" />
</form>
The response contains the exact format you need to follow to create a new employee. You need to submit the form to /context/employees with an HTTP PUT request containing those form parameters. This is HATEOAS. The hypermedia link is the /context/employees. The engine is following this link with a PUT request. The application state is that after this request, a new employee will exist.
Assuming that you're using the Json based WS - there is some tools that helps:
there are json parsers that can turn json, or json schema files into POJO classes, and put some annotations used by Json parsing libraries - take a look here: http://www.jsonschema2pojo.org/
I don't know any automated tool that can generate server stub in terms of all API calls etc. but there is a nice library to consume it - https://github.com/square/retrofit - you have to still put patches and methods signatures into interfase, but it's a way more convinient that playing with "pure" java.
There are also some quite nice tools helping to generate and format documentation for WS - one I like most is swagger: https://helloreverb.com/developers/swagger
There is no (or at least I don't know about it) tool that allow to generate stub, data classes etc. as it can ve usually done with WSDL file.
I am creating a simple RESTful web service with simple types successfully. Now I want to pass an object as argument for web service and get the object as response. My scenario is, Parse the XML message as object by using Jaxb and send the object as request for web service. After that in server side it process the requested object and generates the response xml file and send back it as object.
In URL path i give
"http://localhost:8080/SampleWS/rest/checkXML/username=visolve&password=visolve"
for simple type. But in object I don't know how to give the object reference in URL. Please help me how to solve my problem..
Regards
Bathakarai
Just define a very good-looking domain object. JAXB and JAX-RS will do the rest.
JAXB.
#XmlRootElement
class Regards {
#XmlElement
private long sincerely;
}
JAX-RS.
#Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
#POST
#Path("/sincerely")
#Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response sincerely(final Regards regards) {
regards.setSincerely(System.currentTimeMillis());
return Response.ok(regards).build();
}
Though you could certainly include the entire XML content in your URL, I would probably shy away from it.
Think of it this way: if you encode the XML in the URL you're potentially adding more work on both ends. Now the server and client will both need to know how to build the URL properly, and check to make sure everything lines up correctly. What if, in the future, you need to offer a JSON or YAML view of the same content? Now your URL might need to include the content-type as well. What about character-encoding?
All this to say, HTTP provides a terrific transport mechanism which already addresses these concerns. Include the XML as the entity body of the HTTP message, and use the HTTP header to identify what content-type you're sending, character-encoding, etc. This will work both ways (the server and client both can send the XML back/forth), and makes better use of HTTP.
Here's a related link which might help with some of the details. And another.
On a side note, please, please, please tell me you don't plan on sending user-credentials in plain text across an unencrypted link.
Is there any way to use soap-rpc web services such that the client is generated via a shared interface? Restful web services do it this way, but what about soap based? Do you always have to use a tool like Axis or CXF to generate your stubs and proxies, or is there something out there that will set it up dynamically?
Thanks.
EDIT #1:
To clarify, I'm looking to do something like this:
Common interface:
#WebService
public interface MyWebService {
#WebMethod
String helloWorld();
}
This common interface can already be used to create the server side component. My question is: can this type of common interface be used on the client side to generate dynamic proxies? Restful web services do it this way (Restlets & CXF) and it seems the .Net world has this type of functionality too.
I would see this tutorial of JAX-WS useful for your purposes:
In the example code the Web Services Client is configured by adding an annotation #WebServiceRef with a property pointing to the WSDL location to the client implementation class and no tools are needed to access the stuff from the Web Service that is referenced.
Was this the way you would like to have it, or did this even answer to right question?
Not exactly sure what you're looking for, but if you don't want to rely on JAX-WS/JAXB-generated artifacts (service interfaces and binding objects), you can make use of the Service and Dispatch APIs. For example:
QName serviceName = new QName(...);
Service service = Service.create(serviceName);
QName portName = new QName(...);
String endpointAddress = "...";
service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);
Dispatch<SOAPMessage> dispatch = service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);
SOAPMessage request = ...;
SOAPMessage response = dispatch.invoke(request);
Check Apache CXF. Configuring a Spring Client (Option 1).
When you want to call a webservice, you must have knowledge of methods implemented on it. For that, We need to make stubs OR we can read it from WSDL.
I have created a WS client, using AXIS2 libraries, which is without stubs. The thing is, for each diff. WS we need to create response handles.
You can call any WS method using SOAP envelops and handle the response.
//common interface for response handlers...
//implement this for diff. web service/methods
public interface WSRespHandler{
public Object getMeResp(Object respData);
}
//pass particular handler to client when you call some WS
public class WebServiceClient {
public Object getResp(WSRespHandler respHandler) {
...
return repHandler.getMeResp(xmlData);
}
}
Please check the link below, which shows the example interface for WS client.
http://javalibs.blogspot.com/2010/05/axis2-web-service-client-without.html
For every diff. WS method we can have diff. implementation for WSRespHandler interface, which will help parsing the response.
Not knowing java so well, but being forced to learn some to accomplish a task that I was given, I needed to consume a .Net service that I have already written, I had to do a little research.
I found that 99% of the examples/samples/problems with invoking a method call against a .Net service, or any service for that matter involved using J2EE (ServiceManager) or build classes and a proxy that reflect the service being invoked. Unfortunately for me, none of this would work. I was working "in a box". I could not add new classes, could not WSDL references, did not have J2EE, but DID have access to the standard java libs.
I am used to doing this sort of thing in pretty much every other language but java, but now there was no choice, and java it was.
A lot of digging and figuring out all new terminology, methods, classes, etc, I knew I was getting close, but was having issues with some small items to complete the task.
Then I came across this post: http://www.ibm.com/developerworks/xml/library/x-jaxmsoap/
As long as you have some sort of idea of what you need to send the soap service in term of the soap envelope, the above link will give you the information you need to be able to invoke a service without the classes, wsdl class generators and J2EE, apache or other dependencies.
In an hour from the time I read the mentioned article, I had a class working and about 10 minutes later, converted the code to the "in the box" solution.
Hope this helps
Apache Tuscany might help you, although it may be heavier than you want
http://tuscany.apache.org/