I'm a bit confused about how to make a request to a webservice via java.
For now the only thing that I've understand is that webservices uses xml structured messages, but still I didn't quite understood how to structure my request.
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<getProductDetails xmlns="http://magazzino.example.com/ws">
<productId>827635</productId>
</getProductDetails>
</soap:Body>
</soap:Envelope>
Basically I've to send 2 parameters to the web service and in return I expect two other parameters.
I guess there are some jars that can do most of the job, but I didn't find any online. Can someone please explain me the basis?
This question is related to
java
web-services
soap
I have come across other similar question here. Both of above answers are perfect, but here trying to add additional information for someone looking for SOAP1.1, and not SOAP1.2.
Just change one line code provided by @acdcjunior, use SOAPMessageFactory1_1Impl
implementation, it will change namespace to xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/", which is SOAP1.1 implementation.
Change callSoapWebService
method first line to following.
SOAPMessage soapMessage = SOAPMessageFactory1_1Impl.newInstance().createMessage();
I hope it will be helpful to others.
When the WSDL is available, it is just two steps you need to follow to invoke that web service.
Step 1: Generate the client side source from a WSDL2Java
tool
Step 2: Invoke the operation using:
YourService service = new YourServiceLocator();
Stub stub = service.getYourStub();
stub.operation();
If you look further, you will notice that the Stub
class is used to invoke the service deployed at the remote location as a web service. When invoking that, your client actually generates the SOAP request and communicates. Similarly the web service sends the response as a SOAP. With the help of a tool like Wireshark, you can view the SOAP messages exchanged.
However since you have requested more explanation on the basics, I recommend you to refer here and write a web service with it's client to learn it further.
Source: Stackoverflow.com