How to POST XML using CXF client? - java

I'm experienced in creating soap webservice clients with cxf and jaxb.
However now I have a jaxb java mapping class, and have to send this as XML using HTTP POST/1.1 to a URL path.
Question: can this be done using cxf? Or if not, with spring? I especially need (de)-serialization of request and response, automatic logging, etc. Just as it is the case with cxf soap clients.

Yes, you can use CFX for JAXWS clients. You simple need the WSDL from the service provider. Then you use wsdl2java tool to turn the WSDL into Java stub code that you then compile along with your application.
There is a really good guide here.

Related

How to test cxf generated SOAP client?

I have generated classes from a wsdl using wsdl2java from CXF. This wsdl has securitypolicy which uses security token though.
Could someone guide me how to test this client? I am stuck as the client tries to obtain a token from a mock server I built using WireMock, but it cannot as the mock server does not know how to respond.
Thanks!
Try generating server side classes using cxf and implement the server side method to respond with proper response.
Again this is kind of simulator which you build using cxf.
I'm answering my post again.
So there is no straightforward way of doing this as it is very difficult to mock a server with the security policies implemented.
Thus, what I did is create another service without the authentication part and tested it. We can also say that we don't need to test the authentication part as cxf anyways handles it.

Based on WSDL, how can I generate SOAP request on the fly during runtime?

I know there are some tools can help me generate the Web Services client. But, this approach does not work for me, since WSDL is dynamic in my case. So, based on WSDL, is there a Java library can help me generate SOAP request on the fly during runtime?
spring ws with the xsd you can create on fly wsdl.
The trikky part is the configuration of the soapwebservice but here you can find the 80% of the cases.
Regards

How to use WSDL url to create a request for data

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

When to use Java SOAP API

edited the question to improve clarity.
I am trying to learn SOAP based Java Web services.
I created a simple web service using the #WebService annotation. I published it in my local machine and I consumed the service in my local machine. I found out that the WSDL file is auto generated and the SOAP messages are 'under the hood'. I was able to track the SOAP messages only through TCP/IP monitor.
I later found that Java SOAP API give the option to create SOAP messages ourselves and transmit them using classes/interfaces like MessageFactory and SOAPMessage.
My question is, if WSDL and SOAP messages are generated and handled automatically, why would we need SOAP handlers to manually create and send SOAP messages using the Java SOAP API?
My question is, if WSDL and SOAP messages are generated and handled
automatically, why would we need SOAP handlers to manually create and
send SOAP messages using the Java SOAP API?
Because you might want to have more control over SOAP communication, building a SOAP message etc. When mentioning MessageFactory and SOAPMessage, you're actually referring to SAAJ. Compared to JAX-WS, SAAJ is operating at lower level with all pros and cons that this approach brings. From Java rocking:
JAX-WS versus SAAJ
From a practical standpoint, using SAAJ means that you don’t use tools
such as 'wsimport' or 'wsdl2java'. Those are for use with JAX-WS, and
are the means by which a client can generate domain objects and
operate almost as if they were not using web services at all. With
SAAJ, you have no domain view of a service. You are really working
with the plumbing. Development with JAX-WS can be much quicker and
easier, and generally does not cause you any loss in control. But
JAX-WS is a convenience layer, and it can be comforting to know that
if you wield some command of SAAJ, you’ll be ready to do anything that
a WSDL interface requires of you.
Personally, I would always go with JAX-WS.
You don need to use Java SOAP API explicitly when you use JAX-WS. But you choose to write SOAP messaging applications directly, then these APIs come into picture.
An hypothetical example would be that you want more control over the SOAP message parsing. You do not want to process the entire XML but a part of it using xpath.

Mapping Requests to a Nonstandard Webservice

I'm writing a bridge between two old application on our network. One has a webservice that takes URL encoded parameters (GET) and returns an XML document. Like this:
http://mytest.com/getData/?format=xml&dateStart=2012-01-01
My question is this - I can use the XSD for the xml returned and marshall it into Java objects (xjc defined).. but is there any way to map the requests/responses to a jax-ws webservice (or similar?) It's not SOAP - so I can't go the WSDL, CXF/JAX-WS route, can I?
I was really hoping for an elegant solution to this without having to code it all from scratch (URL request , returned stream, then marshal). Is there a framework out there that would allow me configure a request? I thought WSDL supported verb="GET" but sadly, I can't seem to get it working with Apache CXF and WSDL2JAVA.
Am I totally off base here?
I think JAX-RS may be of use here. Just create XSD schemas and convert them to Java classes, and use a REST client for that site.
You can probably do it with CXF too. See here.
Check out WSGen or you can add ?WSDL to the end of your JAX-WS endpoint to get the generated WSDL. This way all you have to do is create your JAX-WS annotated classes similar to your JAX-RS ones and the WSDL is generated and it should be able to handle your XJC generated objects with no problems.
http://metro.java.net/guide/ch02.html#create-a-metro-web-services-endpoint

Categories