Simply consuming a web service in Java - java

I have a very simple SOAP web service that I need to consume from a Java client. What is the easiest way to accomplish this without using any third party libraries? A requirement is that the host and port is read from the web.xml before every call to the ws.

I can recommend you CXF library. Using it you will have several options for calling web services:
Use dynamic proxy for calling (don't need to make Java stubs using wsdl2java).
DynamicClientFactory dcf = DynamicClientFactory.newInstance();
Client client = dcf.createClient("http://admin:password#localhost:8080"+
"/services/MyService?wsdl");
Object[] a = client.invoke("test", "");
System.out.println(a);
Using Java stub generated from WSDL, using wsdl2java.
If your server was created using CXF you can reuse your interface code directly (instead of using wsdl2java on the WSDL which was created from your interface!)
For both #2 and #3, the following code exemplifies the CXF usage:
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setAddress("http://admin:password#localhost:8080/services/MyService");
factory.setServiceClass(ITest.class);
ITest client = (ITest) factory.create();
client.test();

Depending on which version of JAVA you're using, some of the JAX-WS is built into it. JDK 6 has Java's JAX-WS standard implementation and you could just use it.
See the following:
JAX-WS 2.1 and JAXB 2.1 is available in JDK 6 Update 4 release
Getting Started with JAX-WS Web Services (tutorial to use the JDK built-in JAX-WS for deploying and consuming a web service)

If you can relax your "no 3rd party libraries" requirement, and you have a WSDL for the web service then Axis makes it really easy. Just compile the WSDL using wsdl2java, and you can use the generated Java classes to consume the web service.

Without using any third party libraries? Get to know the SOAP standard really well and learn to love SAX.
If you can't love SAX, then lax your no-third-party-libs requirement and use StAX (with woodstox) instead.
This approach might be the "easiest" (considering the no-third-party-libs requirement) but I don't think it will be easy.

Related

Creating web services client from WSDL in Java

I am given a WSDL file. I need to call a SOAP web service in the WSDL. I would like to know the different ways of creating a web service clients from a WSDL in java. It would be helpful if the options are given with examples, links, pros and cons. I am now confused with different options like wsimport, wsdl2java, java2wsdl, saaj, apache axis, cxf, spring etc. Your help is much appreciated.
SoapUI doesn't support WSDL 2.0
You may try an experimental wsdl-generic library from Apache Taverna
The library works with both WSDL 1.1 / 2.0 versions.
The experimental branch creates XML tree based on Apache XML Schema 2.0 library, so you can easily create your XML message.
Then it dynamically calls the service via JAX-WS.
It also has a command-line WS executor!!!
Cheers,
D.
I was working on a project using Salesforce SOAP API, and here is a link of the guide for using WSDL to generate java lib:
https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_quickstart_import_WSDLs.htm
Hope it will help you.
SoapUI is a quick solution with user interface to make initial tests for provided web service methods. Later you can use wsdl2java with specific options to generate Java classes from WSDL and make jar which you will include in your project.

Consume SOAP webservice in java, only WSDL in hand

I need to consume a web service in java/jsp code. Only the WSDL is available for me to start.
I understand I need to convert the WSDL into java client JAR file using AXIS2 / CXF but I cannot build the whole application on this.
Can someone provide a simple example or basic steps for me to start on this?
I am not able to join the dots here. WSDL, java client JAR, AXIS2.... All online tutorials point on 'creating' a web service.
There are a number of tools capable of doing this included in various frameworks and app servers (CXF, JBoss/Wildfly, etc.), but the JDK itself includes a tool called wsimport which can consume a WSDL file and produce the JAX-WS stubs you need to remotely-invoke the service endpoints via a Java client.
Here's one quick description: http://www.mkyong.com/webservices/jax-ws/jax-ws-wsimport-tool-example/; here is the Oracle documentation for the tool in JDK 7: http://docs.oracle.com/javase/7/docs/technotes/tools/share/wsimport.html.
WSDL is just the conract for the web service. You need to generate client code using it, later you can implement your code to call the web service. Like #maerics pointed out, you should use wsdl2java to generate your client code for AXIS2 and use your client to consume the web service.
You can check this link for an example of client stub generation for AXIS2.

How to connect WSDL file in my application using SOAP service?

I am doing a project using Java and BPEL. I successfully created webservices in Java and integrated them using BPEL. All i generated a single output WSDL file. Now, I have to use this output WSDL file in my application using SOAP communication. How can i do that? Is there any help out side for such scenarios? Walkthroughs are really appreciated..
Depending on the architecture of your application (Standard Java, Spring-based, ...) there might or not be a documented procedure to consume a SOAP-based webservice.
On the other hand, you're always free to pick a webservice development framework to handle that. For instance, you could pick either CXF or AXIS2 (I believe these are the two most popular frameworks for Java WebServices). Each of these frameworks provides a tool called "wsdl2java" that helps you generate client-side/server-side/both Java classes. Then, you can easily add those classes and the requireds libraries to your application.
Having used CXF in the past, It even does provide several way to consume a webservice
Generating the client-side classes
Using CXF dynamic client factory : basically, you'll retrieve an endpoint proxy from a factory object.
Hope that'll help
I start with SoapUI (or downloadable from sourceforge), that will let you consume the WSDL and fire off requests against your server. Typically I'm hitting someone else's webservice, and trying to figure out what the data looks like before I start wiring my code together, but in your case its just a verification that the services would be/are working.
Then, as #KHY said, you can automatically convert the wsdl into java with a wsdl2java and start coding (look under the Related list on the right panel of this SO screen)
If it is a Java application, then the easiest way to consume a service is using JAX-WS. It's really easy to create a Web service client from WSDL.
See this link
Once you deploy the BPEL project on server, then refer the WSDL with http://server:port/application/YourBPELProjectService?WSDL in the consuming application. You will need to write different client code based on the BPEL type - Synchronous, Asynchronous etc.

Spring WS Client - How to create mapping POJO for WSDLs without using Axis

I'm creating a Spring WS client. I have a huge WSDL from a third-party company. They provide a set of classes that maps to their WSDL via the Axis' WSDL2Java. I don't want to use Axis or any dependencies from them.
Since this is a web service client, I'm free to choose any framework. My problem is how do I generate a mapping class for the WSDL without using Axis's WSDL2Java. Am I stuck with manipulating pure XML? Take note I'm using Spring WS.
Edit:
Is it true if the service provider has built their service with Axis 1, you're basically stuck with making a client that's also based on Axis 1? I read it from this answer JAX-WS client with Axis service. I thought web services are supposed to be decoupled or at least independent from the platform that it was created, allowing you to have a .Net based web service to be accessed from a Java based client and vice versa?
If you want to use Spring-WS, then your best bet is to use the wsimport tool that ships with Java 6 (or use the JAX-WS RI, if you're on Java5). This will generate JAX-WS stubs for the web service. Included in these stubs will be standard JAXB bindings for the WSDL's schema, and those can be used with Spring-WS (wsimport will generate other service stubs that you won't need for Spring-WS).
If you are only interested in generating the POJOs from the WSDL, I think you could just get the XSD from the WSDL and use XJC tool to generate only the JAXB beans. This would avoid generating useless JAX-WS stubs.

standalone java webservice client

I am new to webservices in general. I am trying to write a Java stand-alone client which can get a response back from a webservice.
I tried searching SO and Google but now I got more confused. The below are the links I went through extensively.
Simple (standalone) Java SOAP web service client from WSDL using Maven
Java webservice (soap) client - use certificates
java webservice client
https://cwiki.apache.org/CXF20DOC/how-do-i-develop-a-client.html
Java Webservice Client (Best way)
Steps in creating a web service using Axis2 - The client code
I have a url like: http://api.something.com/remote/wsdl/SomeEncryptedText
I also have a SOAP request something like:
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<AuthUsername>someName#someWhere.com</AuthUsername>
<AuthPassword>mypassword</AuthPassword>
<Sid>12121</Sid>
<DynamicProductFeedsRequest xmlns="http://api.something.com/remote/SomeEncryptedText">
</DynamicProductFeedsRequest>
</soap12:Body>
</soap12:Envelope>
With this how do I write a stand-alone Java client which I would want to integrate with some web application at a later stage?
From the resources mentioned earlier looks there is a wide choice of softwares: SoapUI, WSDL2Java, Apache Axis, Maven Plugin, JAX-WS, Apache CXF.
I used http://www.soapclient.com/soaptest.html in one of the SO answers mentioned above and I am able to get a perfect html/xml file on the browser.
Now I am confused on which is the software I should use? The information in the links are little in bits and pieces which I am unable to correlate with one another since I do not know anything in SOA.
Could anyone please tell me the high level steps in writing a stand-alone Java client which takes in the WSDL URL and SOAP request and gives me the output of it?
Please let me know if I missed any information.
This question all depends on the following:
The JDK version of your Java compiler.
Your WSDL version (there's 1.0, 1.2 and 2.0).
Basically, if you are using Java annotations to generate web services, then you'll need Java 5 related Web Services libraries (which supports annotations).
Some articles on Using Java Web Services with annotations (JAX-WS):
Introducing JAX-WS 2.0 With the Java SE 6 Platform
JAX-WS 2.0
I'll start from generating Web Service client with Java that doesn't support annotations. The well known client that generates WSDL to Java is Apache Axis (the last version is 1.4 released in 22 April 2006). This basically takes a WSDL definition and generates it back to client. It supports the old version of WSDL (1.0) and crashes if you use the newer versions of WSDL (1.2 and 2.0).
What this basically does, it takes your WSDL and generates a java Proxy that communicates to your Web Service. It can allow RPC based as well as XML based communication.
For Java that supports annotations there are, effectively, 2 ways of doing this:
Using Java's own wsimport command (the executable is found under the JDK_HOME/bin/ folder).
Using 3rd Party libaries such as Apache Axis 2 (which effectively replaces Apache Axis and supports WSDL version 2.0) or Apache CXF (which supports WSDL up to 1.2).
To use wsimport, you basically need to go to a shell command (or write a script) and effectively do something of this effect:
wsimport -d [outputdir] wsdl_file
and your java proxy will be found in the [outputdir] folder.
wsimport is found in JDK 1.6 (I don't know if it exists in earlier versions). More source here, and here.
For Apache Axis, Apache Axis 2 or Apache CXF, there's a WSDL2Java class file that does source code generation.
Here's a guide on how to use WSDL2Java in Apache CXF and in Apache Axis 2.
I hope this helps you in some way as much as I spent like 30 minutes off work doing this. :-)

Categories

Resources