I have following WSDL URL :
https://www.deanumber.com/Websvc/deaWebsvc.asmx?wsdl
There are many WSDL operations as you can see when you hit the URL. But , I just want to run GetQuery operation and get the response (GetQueryResponse in the WSDL ).
I am able to successfully test this in SoapUI. The response is coming properly. I want to do the same in my existing web project (Java) in Eclipse.
As per How do you convert wsdl to java classes using Eclipse? , I used Eclipse to auto-generate the WSDL client code from the URL and 20+ Java files got created in my web project including GetQuery.java and GetQueryResponse.java. I know how to instantiate GetQuery.java , but do not know how to get the response. Is there any standard way to call the already generated client code?
You can find a good HelloWorld example for a soap client generated using wsimport tool in the following link.
Using wsimport command to generate web service client
Sample code quoted from the given link above
public class HelloWorldClient {
public static void main(String[] args) {
HelloWorldServerImplService service = new HelloWorldServerImplService();
HelloWorldServer server = service.getHelloWorldServerImplPort();
System.out.println(server.sayHello("Satej"));
}
}
Where HelloWorldServerImplService and HelloWorldServer are auto-generated java files from a given WSDL using wsimport tool
Related
From a colleague I have received a WSDL file that describes the web service he is offering, and which I am supposed to call from my code. I would now like to do two things:
1) Implement the client
2) Have a stub server that I can use for testing, until I have access to the real server.
What I tried is the following:
wsimport -clientjar foo.jar foo.wsdl
This gives me a jar file that contains the Datatype that will contain the data to be posted to the web service, and also an ObjectFactory. I guess I will have to use it as follows:
TestDataType testDataType = new TestDataType();
testDataType.setFoo("foo");
testDataType.setBar("bar");
ObjectFactory objectFactory = new ObjectFactory();
JAXBElement<TestDatatype> request = objectFactory.createTestRequest(testDatatype);
Now how do I send this request?
Also a class TestDemoService annotated with #WebServiceClient has been generated. How do I run this class?
Any advice is highly appreciated.
You've created the client. To use this client you need to set the endpoint(if not already set) and call the service using the generated api.
This site should be a good reference.
Using wsimport
I am trying to generate the client code from a WSDL.
I need to work on the Jdeveloper 10g IDE.
I generated client code from WSDL using the inbuilt tool Oracle JAX-WS 2.1.5.
For a particular WSDL this tool is generating around 122 java classes that does not include the Locator class. While running the code, I get a class not found exception for WebServiceLocator class.
However, when I am using Apache Axis 1.4 ( Eclipse IDE ), it is generating 63 java classes and it also includes the WebServiceLocator class, but this is not generating all the required methods that are needed for authentication, though ,Oracle JAX-WS 2.1.5. is generating those methods.
I tried merging the java classes generated from Eclipse into Jdeveloper, but then I am getting a lot of errors related to method signatures.
Is there some particular reason why these two different tools are generating different client codes, and none of them complete?
This is probably a bit late, but for those running into the same issues, this might be helpful.
If you really want to use Axis 1.x authentication is done on your client class as follows:
MyLocator bindingService = new MyLocator();
bindingService .setPortEndPointAddress(myEndpoint);
MyPort port= bindingService.getMyPort();
((Stub)port)._setProperty(Stub.USERNAME_PROPERTY, myUsername);
((Stub)port)._setProperty(Stub.PASSWORD_PROPERTY, mPassword);
//Perform your query here
I would personally use JAX-WS. Use wsimport to generate the stubs (SOAPUI has a nice plug-in to simplify this). For authentication on a JAX-WS client its easiest to save the wsdl locally and export it as part of the jar then load it in the client. This removes the need for HTTP authorization required to access the wsdl in the first place.
URL myWsdlUrl = getClass().getClassLoader().getResource("wsdl/myWsdlFile.wsdl");
MyService service = new MyService(myWsdlUrl , new QName("uri", "localpart"));
myPort port= service .getPort();
BindingProvider bp= ((BindingProvider)vPort);
bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, myEndPoint);
bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, myUsername);
bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, myPassword);
//Perform query here
I have been doing some reading up on web services programming with Java, Eclipse, etc. and I found one particular example where the person created the web service and client by doing the following:
define the web service java class (interface + impl)
deploy the web service using Endpoint.publish
grab the wsdl from the url of the web service (eg, localhost://greeting?wsdl)
use wsimport to generate stubs
create a client class using generated stubs
Is there another way to generate the wsdl without having to publish the web service and download it? Perhaps a maven plugin to auto-generate wsdl and client stubs?
Update: Rather than creating a new question I am just going to piggyback on this one.
I have created my web service by defining an interface:
#WebService
public interface HelloWorldWs {
#WebMethod
public String sayHello(String name);
}
and an impl class:
#WebService(endpointInterface = "com.me.helloworldws.HelloWorldWs")
public class HelloWorldWsImpl implements HelloWorldWs {
#Override
#WebMethod
public String sayHello(String name) {
return "Hello World Ws, " + name;
}
}
When I run wsgen I get the following error:
The #javax.jws.WebMethod annotation cannot be used in with #javax.jws.WebService.endpointInterface element.
Eclipse seems to be okay with it.
Any idea why?
Note, I originally did not have the annotation but when I tried to call my webservice I got the following error:
com.me.helloworldws.HelloWorldWsImpl is not an interface
The JSR 224 says in 3.1 section:
An SEI is a Java interface that meets all of the following criteria:
Any of its methods MAY carry a javax.jws.WebMethod annotation (see 7.11.2).
javax.jws.WebMethod if used, MUST NOT have the exclude element set to true.
If the implementation class include the javax.jws.WebMethod, then you cant put #WebMethod(exclude=true) and that in not possible, according to specification.
Depends of custom version of Eclipse, shows a warning for this. e.g. Rational Application Developer for Websphere shows:
JSR-181, 3.1: WebMethod cannot be used with the endpointInterface
property of WebService
While programming/building a project (with some advanced IDE) normally you should be able to find it between auto-generated stuff - the IDE should generate it. Just check carefully.
I'm new to webservice development. I'm using Netbeans 7.0 with the Axis2 plugin and Tomcat 7.
I created one project for the server components where I define the web methods, and then created another project for the client components. The client was created in Netbeans by selecting New -> Webservice Client.
When you select New -> Webservice Client in Netbeans, it asks you right then for a WSDL URL. So of course I gave it the WSDL URL from my local Tomcat installation. But what about when I distribute this as a real application? The users aren't going to point their clients at http://localhost:8080/axis2/services/?wsdl. I mean, when running the client from the IDE, it all works fine, but when I distribute this (it's a labor management application by the way where you clock in / out at one or more clients and time cards are written to a central DB), each client needs to be able to point at the webservice URL of whatever production server it's supposed to connect to.
I'd like to store the webservice URL in a properties file, but don't really know what all to do programmatically at the client to make the call to the URL that's loaded from the properties file.
In my client's dist folder, if I open the JAR that netbeans created with WinZip, I see a file name jax-ws-catalog.xml that has the URL in it (which is pointed at localhost). I assume this is where the URL that's used at runtime comes from.
So what's the correct way to go about this? I've searched around, but the things I've found looking on google searches tend to show webservice calls being made in a completely different way than the auto-generated code that Netbeans puts together, and I'd like some info specific to how Netbeans creates a webservice client so that I don't end up making changes just to have the IDE overwrite them.
Thanks! Sorry for the long explanation.
-Jim
I actually figured this out in a different way, and it's probably kind of specific to the way Netbeans does things. The answer Shott85 provided is a good one also, but I think this is more specific to the way Netbeans' auto-generates code.
So I have a project where all the web methods reside named SimplyLaborServer, and a project that has the webservice client named SimplyLaborClient.
In Netbeans, under the SimplyLaborClient project in the "Generated Sources (jax-ws)" node, they have a SimplyLaborServer.java file that has a class which extends Service. It has a private URL that is hard coded to my local server's URL as follows...
url = new URL("http://localhost:8080/axis2/services/SimplyLaborServer?wsdl");
And in the default constructor, it uses this URL. But it also provides a constructor as follows where I can specify the URL...
public SimplyLaborServer(URL wsdlLocation) {
super(wsdlLocation, SIMPLYLABORSERVER_QNAME);
}
So when I have an auto-generated method that looks like this in my client...
private static String testConnection() {
simplylaborclient.SimplyLaborServer service = new simplylaborclient.SimplyLaborServer();
simplylaborclient.SimplyLaborServerPortType port = service.getSimplyLaborServerHttpSoap12Endpoint();
return port.testConnection();
}
I can simply load a Properties object that has the endpoint URL and change the one line to something like below, where props is a Properties object that has endpointUrl defined with the correct URL.
simplylaborclient.SimplyLaborServer service = new simplylaborclient.SimplyLaborServer(new URL(props.getProperty("endpointUrl")));
My only concern is that these methods are kind of auto-generated when you drag and drop them from the "Web service references" node. I don't want them to be overwritten if I make additional changes server-side.
So I'm still open to feedback if this is the correct thing to do here or not.
Thanks
This has been answered before:
How to change webservice url endpoint?
NetBeans uses plain JAX-WS to generate client code, so the answer above should work for you. You just need to add some code to get the endpoint URL from a properties file.
You are using local (http://localhost:8080/axis2/services) WSDL for just creating the required classes of the web service.
After your developments completed you can host your web service application anywhere in the web or within the local network.
After you complete the developments of web service you can locally deploy it and use the service to create the clients required classes. When you creating the client you just has to create a URL object and pass your web service URL (hosted one) as below.
PropertyResourceBundle resoureceBundle = (PropertyResourceBundle) PropertyResourceBundle.getBundle(‘Property file name and path’);
URL serviceURL = resoureceBundle. getString("Hosted_URL_Name");
ServiceClass service = new ServiceClass(serviceURL);
ServicePort servicePort = new ServicePort(service);
servicePort.getItems();
Netbeans IDE will create many classes when you created the web service client automatically.
In above sample code ServiceClass is the web service main class which is you create initially by using the local URL. Name and constructor parameters will be vary as your web service but you have to pass the web service URL (newly hosted URL) as a string.
Then with the service class you can create a port object and accessing all the available web methods you need.
I am looking for a way to easily generate SOAP requests from a wsdl file. for example, something like this:
WSDLObject myWsdl = new WSDLObject("http://www.whatever.com/myService?wsdl");
SOAPRequest myRequest = myWsdl.generateSOAPRequest();
Is there anything like that?
I am trying to do it dynamically via another application, so tools like WSDL2Java dont work for me (at least I think). I need to be able to generate these requests from user input, and then work with them from there.
any help is appreciated.
Please see this answer: How to get response from SOAP endpoint?
What you basically want to do is use the wsimport tool that ships with the JDK. So long as Java is on your system's classpath, you should be able to go to any terminal or console and do:
wsimport http://www.whatever.com/myService?wsdl -p com.company.whateveruwant -d . -keep
With a choice of options (-d specifies the directory to output the generated code). This being done, you'll be able to invoke the web service with the auto-generated code quite simply, such as like:
CustomInterface soap = new CustomEndpoint().getCustomInterface();
System.out.println(soap.getAnswerFromWs("ParamValue"));
You can with WSDL2Java, the unique change is that you work with Java proxy objects and set the attributes of this objects to generate the SOAP request. Other way is use SOAPUI, for example to generate the SOAP message, and directly construct your SOAP message as StringBuffer and use directly a socket to call the service composing the complete HTTP/SOAP message from scratch.