How to use the web service client generetad by eclipse - java

I'm using Eclipse Juno to create a Web Service Client for consuming a WSDL SOAP web service.
It generated 2 packages, one with the web service name and another with "org.tempuri.dataset_ISI_xsd".
Inside the first package there are some classes like:
1- ServiceName
2- ServiceNameProxy
3- ServiceNameService
4- ServiceNameServiceLocator
5- ServiceNameSoapBindingStub
I'd like to know wich class should I use to call the web service methods and how to obtain the values returned by the method.
Thanks in advance

Instantiate the xxxProxy class, call the setEndpoint() method passing the url of the web service host, then call the method matching the name of the web service. The return type of the method will depend on the definition of the web service.

Related

Same Package, Same Class in two Maven Projects a WAR and a JAR, classpath issue

I am having some classpath issue. I have a Web Application which is a web service. It uses JaxB and CXF. The web service has a dependecy of another JAR which is a Web Service Client. Now both the client and the service codes are generated by using wsdl2java plugin. The problem looks like this:
Parent WebService WAR
--PackageA
--ClassB
Dependency Jar
--PackageA
--ClassB
So both of them have the same package and the same class name and since these are generated by the plugin, it makes difficult to refactor one of the package so that they would not be identical.
The WebService calls the client and in client code initializes the parent classB from web service instead of the classB from web service client Jar. The only problem in this ClassB is that they have one method which takes differnet parameter, in one class B it takes, Date whereas in another classB it takes XmlGregorianCalendar. So while calling the client i am getting nosuchmethodexception.
Here is what i tried so far without luck:
1: In the Client jar i tried giving the full package and class name to initialize the ClassB
2: In the Client jar i tried wiring the classes using Spring bean and surprisingly it is still wiring the class from the webService instead of the client
3: In the web service ClassB, i tried adding the same method that takes the right parameter. This works partially but result in another exception which is not good.
Looking forward for your help. Thanks!
Solved the issue by passing extra args to wsdl2java plugin while generating classes from the wsdl as per user2880879 suggestion like this:
<extraarg>-p</extraarg>
<extraarg>http://www.example.com=mypackagename</extraarg>
I believe you are creating webservice using top down approach, means write java class first using jaxws annotations and then create wsdl using cxf maven plugin or ant, and use this wsdl to create client ?
If you are following this approach then you can provide binding file when you generate web service client code. click here to know what is binding file and how to write.
In this binding file you can specify package name you want to change for client code.

How to call a local service in mvc portlet?

I have created a my-services-portlet in which i have AbcService, which I am calling from my templates like this
#set ($VeloToolsService = $serviceLocator.findService('my-services-portlet, 'com.mycompany.services.AbcServiceLocalService'))
#set ($article = $AbcService.getArticle($list))
$journalContentUtil.getContent($article.groupId, $article.articleId, 'view', $themeDisplay.language-id, $xmlRequest)
Now due to new requirement, i need to call the same service from another custom mvc portlet. I am not able to the service handle for the service. what is the proper way to get the service handle? so that i can call my existing service from the portlet.
copy the AbcService-service.jar file from first portlet to tomcat/lib/ext folder, restart liferay, and you get access to the service classes.
See http://www.liferay.com/de/community/forums/-/message_boards/message/4585610 the answer from Mika Koivisto.

Axis java web service: connect to database on startup

I have an axis java web service, which I use for inserting and receiving data from database. As far as I know, constructor of web service is called only when client connects, and when it disconnects, a "destructor" is called. So every time a client connects to receive or insert data, I need to run method connectToDatabase(). How can I connect only once, when web service is started, and when client connects simply insert/receive data? Is there any special way to do it in Eclipse?
You need to create an object which will be instantiated when your application first starts up. To do that you can use spring and expose your web service implementation class as a spring bean. Than create some king of object which will have a method connectToDatabase(), call that method in this object's constructor or specify it as an init method and declare this object in spring configuration. When application is being deployed spring creates that object for you and call this method only once.
To expose axis web service as spring bean do the following:
In axis server-config.wsdd file specify this:
<service name="YouServiceName" provider="java:SPRING" style="wrapped" use="literal">
Attribute provider="java:SPRING" tells axis to that the implementation class is exposed as spring bean.
Hope this helps.

Configure webservice URL for client from properties file with Netbeans 7 and Axis2

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.

Web service in servlet - #WebServiceRef annotation gives 500 error.

After playing around with example SOAP application Calculator from NetBeans, I began making my own app, using some third-party WSDL's as service models.
I managed to successfully create a web service classes from this WSDL, unfortunately, when I try to inject this service into my servlet (called ClientServlet), I got 500 error
"javax.servlet.ServletException: Error instantiating servlet class clients.ClientServlet".
The service interface methods are defined in Interface class MyServiceInterface. Don't ask me why it is that way - this service is made by third party.
When I comment out this annotation, and subsequent field declaration, then it works (but I can't use this service).
My code snippet:
#WebServiceRef(wsdlLocation="url/to/wsdl/of/my/service.wsdl")
public MyServiceInterface service;
Of course, I tried to do this without dependency injection, by direct creation of the instance of this class:
service1 = new MyService().getMyService();
MyService.java was created during importing a service from WSDL. GetMyService() method should return instance of the class that is implementing MyServiceInterface. But instead, Java throws me
exception at javax.xml.ws.Service.getPort(Service.java:92)
I am using Apache Tomcat 7.0 and Netbeans 7.0 IDE. What I should do now?
SOLVED - I just made it from scratch using GlassFish 3.1 server.

Categories