I have a java web service client consuming web services. The client is generated using WSDL2Java tool. All works fine until the web service provider changes one of the schema objects by adding a non-required field. When the web service is called, the response is supposed to return an array of these schema objects and since the returned objects have an extra field - everything breaks. From what I understand there is a way to allow for such changes without breaking the client application.
Could you point me in the right direction on how to solve this?
Any suggestion is appreciated.
Thank you,
Nadia
Try try using different binding options for your client. If I am not wrong using JAXBRI (od JiBX) should solve the issue. try WSDL2Java -d jaxbri
The default is adb more info is in here
The web service is not conforming to the WSDL?
Maybe you can edirt the WSDL and add the non-required field
Or is the WSDL changed?
If the WSDL was changed you need to re-generate your code by WSDL2Java tool.
This problem is generally hard to solve, which is why client-facing XML schemas shouldn't change very often. You can try disabling schema validation, but if the schema changes too much, your client aplication will still blow up.
The recommended enterprisey way to do this (in the jargon: 'loose coupling') is to have a separate layer of domain objects and map between the generated Java classes and your domain objects. You just update the mappings when the schema changes, which shouldn't penetrate too far into the layers of your application. But you still need to recompile the application every time the schema changes.
Related
I have a WSDL url(http:localhost:8080/userdata?wsdl) and I want to create a request to this webservice so I can fetch the data for further processing. Can I do this without wsimport?
If I have to create package from wsimport, how I can create a client which will use generated classes to create the XML request?
If I can do this without wsimport , how I can create a client which will create the XML request?
New to webservices, links to documentation would be appreciated. I am trying to understand this at the moment http://java.dzone.com/news/5-techniques-create-web-servic
You can use CXF wsdl2Java to generate a client code for the web service.
Once you run the wsdl2java , you will get a set of java classes generated for you. You can then use those classes to call the services without any explicit conversion of XML - the underlying framework will do it for you automatically. You can start with http://cxf.apache.org/docs/how-do-i-develop-a-client.html
I proposed CXF while you can look for many other alternatives - However, i have found CXF to be very feature rich and will help you in developing/working with web services.
On top of what Akhilesh said you can also create a Dynamic client for invoking WSDL. I have done it recently and i found it a little bit better then using CXF as dynamic client does not generate any code whatsoever. You just pass in the parameters to it and it does all the job for you. You can find a "shell" to build your own client HERE
Before anything else, I want you to know that I can already connect to the web service server. I'm asking this question because I want to gain a deeper knowledge on how a wsimport generated client works. Based from my research, wsimport uses JAXWS. Please note that I have no knowledge from JAXWS.
I generated my client using wsimport. The WSDL I used is from an Axis2 web service and was automatically generated by Axis2. The classes below are the results of wsimport:
Under com.datamodel.xsd
DataBeanRequest.java
DataBeanResponse.java
ObjectFactory.java
package-info.java
Under com.service
MyWebService.java
MyWebServicePortType.java
MyMethod.java
MyMethodResponse.java
ObjectFactory.java
package-info.java
With the classes above, I can that tell that com.datamodel.xsd contains the beans used by the web service server (excluding ObjectFactory and package-info). Meanwhile, MyMethod and MyMethodResponse are also beans used to set the request and response parameter of the web service method/operation.
Below are my questions: (You don't really have to answer all of it if you don't know the answers on some of my questions. :) And please feel free to share any info that you think I might find useful.)
Am I correct with
Am I correct with my assumptions above?
What are the function of the other classes?
I inspected MyWebService and it contains an annotation referring to the absolute location of the WSDL I used to generate the client. What is the relevance of specifying the wsdllocation in the client? How does the client use that info?
I noticed that the actual URL of the web service is not declared in any of the classes generated. How does the client know where it needs to connect to?
Was the WSDL file annotated so that the client can read the URL on the WSDL file upon connection? If so, then does it mean that the WSDL file is always read when a new connection must be established?
Since there's a need for me to compile my application and install it on a different server, the will become invalid. Can I set it to a relative path instead of an absolute path? How? (Answer: Yes, it can be set to a relative path. The wsimport command has a wsdllocation attribute wherein the value of the wsdllocation can be specified.)
What if I need to connect to an HTTPS. How can I set the server certificate?
Is there any difference when I generate my client using wsimport and when I generate it using Axis2 or Apache CXF.
Before I answer the questions, some clarification: JAX-WS is a specification for implementing web services in Java. It describes how WSDL artifacts can be mapped to Java classes and how this mapping can be applied using annotations. You can download the specification here. The tool wsimport is part of the reference implementation of this specification and the reference implementation is part of the Java class library. There are several alternative implementations, such as Axis2, CXF or Metro, that enhance the basic JAX-WS support with support for additional standards such as WS-ReliableMessaging or WS-Security.
Now to your questions:
Am I correct with my assumptions above?
Yes, you are.
What are the function of the other classes?
The package-info exists to map the XML namespace used in the web service to the package in which your implementation classes reside. The namespace normally looks different from a Java package name (normally, it is a URL) and this makes the mapping necessary.
The ObjectFactory allows you to create any of the messages sent and received by the service. You need this if you want to hook in code in front of your stub class, provide modified messages or similar things.
I can't see the content of your classes, but if I understand it right MyWebServicePortType is an interface that resembles the portType in your WSDL. That is, it maps the operations and their signatures in the WSDL to Java methods. If you want to provide the service (which you don't, you are asking about the client), you would need to implement this interface. As you implement the client, you simply use it.
Finally, the class MyWebService contains the client stub you need if you want to invoke the web service.
I inspected MyWebService and it contains an annotation referring to
the absolute location of the WSDL I used to generate the client. What
is the relevance of specifying the wsdllocation in the client? How
does the client use that info?
The interface you generated contains the signature of the portType of the service, but it does not explain how you can talk to the service. This is part of the binding in the WSDL. The most basic setting is a document/literal style for the messages using SOAP over HTTP. Other configurations, such as SOAP over JMS, are possible and your client needs to know what protocol to use. Therefore it needs the binding WSDL. Also, as you state later, there is no endpoint address in your Java files. This address is also read from the WSDL.
I noticed that the actual URL of the web service is not declared in
any of the classes generated. How does the client know where it needs
to connect to?
It reads the address from the port of the service in the WSDL. This is located of the end of the WSDL.
Was the WSDL file annotated so that the client can read the URL on the
WSDL file upon connection?
No, the port is a typical element of a concrete web service endpoint. There's nothing special needed here.
If so, then does it mean that the WSDL file is always read when a new
connection must be established?
Well, there could be caching at the client side (I don't know about the details of the reference implementation on this one). From a conceptual point of view: yes, it is.
What if I need to connect to an HTTPS. How can I set the server
certificate
This can be tricky, I can't give you an out-of-the-box answer. I would suggest to read through questions on this topic, such as this one.
Is there any difference when I generate my client using wsimport and
when I generate it using Axis2 or Apache CXF
Yes, there is. wsimport is better, don't use wsdl2java. Here is a description, why.
You asked: I noticed that the actual URL of the web service is not declared in any of the classes generated. How does the client know where it needs to connect to?
If the WSDL was downloaded using a browser and passed as input to wsimport, then the local wsdl file location is embedded into the generated code. That is why you don't see the actual service location in the generated code. It also means if you deleted the local copy of the wsdl file the generated code will not work (when inovked using a main method) .
If the URL of the wsdl was passed as input to wsimport then that URL is embedded in the generated code, which is further used to get the actual service location. The idea is that the WSDL locations are fixed. They are expected to be in a UDDI or as a local file. This allows the actual services to move around and if they do move, you just have to modify the local copy of the wsdl file alone or update the wsdl in the UDDI. [mostly this does not happen as service locations are never IP but DNS names]
This is why it is never a good idea to publish the wsdl in the same server where your webservice is running
I'm creating a web-service client. I used a WSDL file to generate the client side stubs.
But now I have to authenticate to the web-service, and invoke methods.
I've checked some tutorials on how this should be done, but they always explain generating the client code and server code then having them work together.
I was wondering if all the info needed to authenticate and invoke requests is contained within the WSDL file(and thus auto generated code)? Or do I have to also have knowledge of the web-service code?
Any help appreciated.
Generally speaking, a WSDL should be all you need (assuming it's been written by someone who knows what they're doing).
A well-written WSDL should have sensible method and parameter names such that the generated client bindings are more or less self-explanatory. Through the <annotation><documentation></documentation></annotation> attribute, comments should be added to resolve any ambiguities. In other words, think about a WSDL just like a JavaDoc API page. You shouldn't need to care about what's inside the black box, just so long as you know what you need to put in and what you'll get out of it.
As for authentication mechanisms there are really two cases to consider: the web service level authentication and application server level authentication.
At an application server level (e.g. Tomcat or GlassFish), the WSDL won't give you indication of the authentication method used (because the WSDL is at a level above the application server). In this case, you can try debugging by accessing the WSDL file in a browser (or maybe try invoking the service in SoapUI) and seeing what you need to be authenticated.
At a web service level the security mechanism should be described in the WSDL. I'm not aware of any IDEs that can automatically recognise the authentication mechanism described in the WSDL and then prompt you to what you need to supply (although I only really use NetBeans). However, you should be able to work it out - either by examining the WSDL or by looking at the error messages your web service client spits out when you try and access a protected resource.
A WSDL file does not contain information on the order of invoking certain functions (if any). So, in my opinion there should always be additional documentation.
I have a POJO which is being deployed as a webservice in Axis2 - Tomcat combo. What is the best way to supply runtime configurations to the service? All servcie related config parameters are in XML file -
What's the best location to keep this config file? Note I want the service to be completely self contained.
How do I get the physical location of the service home i.e. Tomcat 6.0\webapps\axis2\WEB-INF\services\MyService?
How good is Axis2 i.e. is it a good choice to select Axis2 as webservice platform? The project is with aggressive timelines (what's new? :)) so really do not wish to dive into the Contract First etc stuff. Need a quick solution where I can drop in POJO and use it as webservice.
As always, Your help is very much appreciated!
I used JNDI to control the runtime configuration options for simple web services. How to configure JNDI depends on the container you are using, Jetty/Tomcat etc.
More complicated projects I embed spring and manage both configuration and other persistent objects.
Axis2 is a good platform for developing stand-alone web services. I'd still recommend having a look at contract first stuff. It is of course more complicated but gives more control over the XML messages exchanged between client and server. The neat thing about Axis2 is that you can start with POJOs and get more complicated later.
Note I want the service to be completely self contained.
Do u mean u would like to move this modules on other container ... which do not support TOMCAT based dir structure...... bit difficult
If Axis hosting concern is of TOMCAT alone then the best way to use the configurations is via an Property /XML file stored at the root of the Java package and haven a Startup servlet with load on startup 1 to read the property file using
context.getRealPath("") + "/WEB-INF/classes/abcd.xml" or
context.getRealPath("") + "/WEB-INF/classes/abcd.propertyfile
Store the same in a key /value based Hashtable ...further store the Hash table in Servletcontext.
On any where required fetch the Valu by passing the Key to the Hash table and use the same.
However the startup servlet should run Once only only by using Load on startup tag in web-xml ...
hope this helps
Im a little confused on the varying definitions and implementations of web services available as implementations. Need some clarification please.
Ones I have used till now:
If a vendor gives me a specific format of XML that I can send populated with data to request and I make a simple HTTP POST over the internet passing in the XML String as the payload, is this a web service call ? If so, is there a specific name to it, this kind of web service ? Because obviously, it does not use anything like Axis, WSDL or SOAP to establish this connection.
A variant of this is If the vendor gives me an XSD, I use JAXB to make a java class out of it and pass in the serialized version of the object, which eventually works out to be the same as option 1.
RESTful web service: Vendor gives me a URL like http://restfulservice/products and I can make HTTP Requests to the URL and depending on what HTTP verb I use, the appropropriate action is called and the response sent over the wire.
Ones I have only read about\ have a vague idea about
SOAP. How does this work?.. Ive read the W3Schools tutorial and I undertsand that there is a very specific form of XML that is standardized according to W3C standards that we use to pass the same kind of messages as we did in option 1. But how does this work in real life? Vendor sends me what? Do I generate classes? Do I serialize some objects and http post them over to an address? Or do the generated objects themselves have connection methods that will do them for me?
What about WSDL? When does a vendor send me WSDL and what do I do with it ? I guess I can generate classes from it. If yes, then what do I do with the generated classes ?
When do I need that axis jar to generate classes from something that the vendor sends ?
As you can see, I have some clear and other mostly vague ideas about the different kinds of web services available. would help if someone ould clarify and\or point to more real-world resources. I've looked a little bit into Java Web Services on the internet and the numerous four letter acronyms that get thrown at me make me dizzy.
Thanks
If a vendor gives me a specific format
of XML that I can send populated with
data to request and I make a simple
HTTP POST over the internet passing in
the XML String as the payload, is this
a web service call ? If so, is there a
specific name to it, this kind of web
service ?
This is still a web service, yes. It doesn't have an "official" name, I usually refer to it as XML-over-HTTP, mainly because I can't think of a better name.
SOAP. How does this work?.. Ive read
the W3Schools tutorial and I
undertsand that there is a very
specific form of XML that is
standardized according to W3C
standards that we use to pass the same
kind of messages as we did in option 1
SOAP provides a standard wrapper layer around the sort of messages you were sending in (1). This wrapper provides information such as an indication as to which operation you are invoking. It can also provide security services, transaction information, and so on. It's a pretty thin layer, usually.
What about WSDL? When does a vendor
send me WSDL and what do I do with it
? I guess I can generate classes from
it. If yes, then what do I do with the
generated classes ?
Again, WSDL is a pretty thin layer, this time around an XML Schema. It defines the operations that SOAP messages will invoke at runtime, as well as the Schema types of the requests and responses. Its a way of formalising the XML document exchange interface.
Say, for example, you had an XML Schema, and have a web service as you described
Using JAXB to generate java source from the schema
Send XML documents conforming to that schema over HTTP to the web service
With WSDL and SOAP, you would extend this a bit :
Write a thin WSDL wrapper around the XML Schema, formalising which operations are available.
Use a WSDL import tool to generate client/server stubs for that WSDL/Schema. In Java, this often incorporates JAXB.
Use a SOAP client/server to invoke the web service
As you can see, it's essentially the same process. The difference is that SOAP/WSDL provides additional information and context to the tools, allowing those tools to do more of the work for you. It's not hugely different, though.
If you get a WSDL document from somewhere, all you really need to know is that it defines a service interface. You run it through your favourite language's binding generator to make some code that you can use to call the service. Typically that means you'll be talking over the wire to the service using SOAP messages over HTTP. SOAP's just a wrapper round sending pretty arbitrary XML messages.
Axis is a library for doing this stuff in Java (both client and server side). I suspect that there are better implementations in other libraries.