问题
I have been tasked at work to create a WADL and a WSDL in Java. I have a few questions though.
First a WSDL is an XML document that describes how a client requests information from a SOAP system. A WADL is an XML document that describes how a client request info from a REST system. Are both of those correct?
If so what exactly do they do? I understand how SOAP and REST work normally with HTTP, but I'm having trouble wrapping my head around what exactly is the point of a WADL and a WSDL , what they are for, and how I should go about creating one in java.
回答1:
When you have a method in your code and you need to call it, how do you call it? You look at the method signature and the javadoc. You see what the parameter names are, what they mean, what type they have, the javadoc tells you if there are some restrictions on the values, what exception you get if you don't respect that, etc.
Now consider a web service. Let's start with SOAP first. It's operations exposed over the network. How do you call this beast? All you can look at is an endpoint where you must send a properly formatted SOAP payload. Does that tell you the operation names? The parameter names and types? Restrictions on your values? No! It tells you absolutely nothing. You need a way to tell clients how to call this service.
You can have documentation, just like the javadoc. You use that to learn how to make the call. But this will be XML. Do you program in XML or in Java? You program in Java but you need to marshall your objects to XML and then unmarshall the response from XML back to Java objects. And you have to write all the code that does that. If you misunderstand the documentation you will build something wrong and it won't work the first time. You will have to read the documentation again, tweak your XMLs, debug it, try again, repeat until working, etc. This is unproductive!
Wouldn't it be nice if you could have a tool to generate the code for you so you concentrate on the actual business that needs to be accomplished instead of wasting time with XML? Enter WSDL.
A WSDL is a way to describe the SOAP web service. It's the signature, parameter names and types, restrictions and documentation, all in one. What's useful about it is that you can feed it to a tool and have the tool generate code from it that handles the XML marshalling/unmarshalling for you and exposes methods and object to your Java code instead.
Now REST is a different beast. To call a RESTful web service you need to "speak it's language", there isn't "a protocol" to follow. Clients need to understand MIME types in order to use the service. This is mostly documentation that you have to read and understand and then build the code. Because this too is mostly boilerplate code, people used with the features provided by SOAP WSDLs decided to create something similar for REST. This is the WADL.
WADL serves the same purpose for REST, as WSDL does for SOAP (note though that REST is more than a different way to do SOAP so when you try do do the same thing with REST, that you did with SOAP, you just reduce a RESTful web service - which is hypermedia driven - to a WebAPI).
As for the way to create WSDLs and WADLs, you can do it by hand if you have the knowledge (that's called "contract-first") or you could use JAX-RS and JAX-WS frameworks, write you services first and have the frameworks generate the WSDL automatically for you (this is called "contract-last").
回答2:
Just few points.
Strictly speaking RESTful Web Services are not services. That is just way see your interaction with the server.
In early days there was a RPC (Remote Procedure Call), so the accent was to describe the remote method in terms of its name, parameters, etc. When XML Schema came to the scene, instead of defining parameters we just send an XML "document" to the method (document style). Java maps the parameters to the XML document (JAX-WS) and this is the most "recommended" way of doing WS in Java (document/literal) style.
In REST there is no services, but a resource which may be created/read/updated/deleted (CRUD). In most of the cases HTTP protocol is used.
WSDL 1.1 is a "de-facto" standard to describe WS and one is supported by Java. WSDL 2.0 is the standard, although the is no big support from the industry.
Note that WSDL may be used to describe HTTP services (not resources!) via http binding. WSDL 1.1 is limited in HTTP terms support (only GET and POST) while WSDL 2.0 support all HTTP operations.
WADL is a proposal of SUN Microsystems to make a kind of WSDL for the REST. WADL is much simpler.
So the difference is remote execution (WS / WSDL) vs resource (REST / WADL).
Regarding Java. If you program your service with JAX-WS you usually do not need to create the WSDL by hand, cause JAX-WS generates it automatically from the code.
JAX-WS doesn't support WSDL 2.0 - you can use Apache Woden to parse it or tinyWSDL to parse/generate. http://ws.apache.org/woden/ http://sourceforge.net/projects/tinywsdl/
There is also java WADL project to work with WADL (can't post more than 2 links)
Cheers,
D.
来源:https://stackoverflow.com/questions/27327018/creating-a-wadl-and-wsdl-in-java