I'm relative new to the webservices world and my research seems to have confused me more than enlighten me, my problem is that I was given a library(jar) which I have to extend with some webservice functionality.
This library will be shared to other developers, and among the classes in the jar will be classes that have a method which calls a webservice (that essentially sets an attribute of the class, does some business logic, like storing the object in a db, etc and sends back the object with those modifications). I want to make the call to this service as simple as possible, hopefully as simple so that the developer using the class only need to do.
Car c = new Car("Blue");
c.webmethod();
I have been studying JAX-WS to use on the server but seems to me that I don't need to create a wsimport
in the server nor the wsimport
on the client, since I know that both have the classes, I just need some interaction between classes shared in both the server and the client. How do you think makes sense to do the webservice and the call in the class?
Best Answer
I understand your problem boils down to how to call a SOAP (JAX-WS) web service from Java and get its returning object. In that case, you have two possible approaches:
wsimport
and use them; orAbout the first approach (using
wsimport
):I see you already have the services' (entities or other) business classes, and it's a fact that the
wsimport
generates a whole new set of classes (that are somehow duplicates of the classes you already have).I'm afraid, though, in this scenario, you can only either:
wsimport
generated code to make it use your business classes (this is difficult and somehow not worth it - bear in mind everytime the WSDL changes, you'll have to regenerate and readapt the code); orwsimport
generated classes. (In this solution, you business code could "use" the generated classes as a service from another architectural layer.)About the second approach (create your custom SOAP client):
In order to implement the second approach, you'll have to:
java.net.HttpUrlconnection
(and somejava.io
handling).Creating a SOAP client using classic
java.net.HttpUrlConnection
is not that hard (but not that simple either), and you can find in this link a very good starting code.I recommend you use the SAAJ framework:
See below a working example (run it!) of a SOAP web service call using SAAJ. It calls this web service.
About using JAXB for serializing/deserializing, it is very easy to find information about it. You can start here: http://www.mkyong.com/java/jaxb-hello-world-example/.