Skip to main content

{Beginner Help} How can I call my web service without using annotations.

1 reply [Last post]
phenderson
Offline
Joined: 2005-02-16

I'm new to web services so please be gentle ;-)

I have created a simple EJB stateless session bean and marked it with @WebService.

<br />
package testwebservice;</p>
<p>import javax.annotation.security.RolesAllowed;<br />
import javax.ejb.Stateless;<br />
import javax.jws.WebService;</p>
<p>@Stateless<br />
@WebService<br />
public class HelloWorldSessionBean implements HelloWorldSessionLocal {<br />
//    @RolesAllowed({"HelloWorldTestRole"})<br />
    public String businessMethod(final String firstname, final String lastname) {<br />
        System.out.println("IN businessMethod firstname=" + firstname + " lastname=" + lastname);<br />
        return "Hello " + firstname + " " + lastname;<br />
    }<br />
}<br />

When deployed in Glassfish all looks good.

On to the client
First I ran

<br />
wsimport <a href="http://localhost:8080/HelloWorldSessionBeanService/HelloWorldSessionBean?wsdl" title="http://localhost:8080/HelloWorldSessionBeanService/HelloWorldSessionBean?wsdl">http://localhost:8080/HelloWorldSessionBeanService/HelloWorldSessionBean...</a><br />

Which made some class files.
<br />
-rw-r--r-- 1 peter peter  804 2007-03-06 09:29 BusinessMethod.class<br />
-rw-r--r-- 1 peter peter  763 2007-03-06 09:29 BusinessMethodResponse.class<br />
-rw-r--r-- 1 peter peter  780 2007-03-06 09:29 HelloWorldSessionBean.class<br />
-rw-r--r-- 1 peter peter 1406 2007-03-06 09:29 HelloWorldSessionBeanService.class<br />
-rw-r--r-- 1 peter peter 1695 2007-03-06 09:29 ObjectFactory.class<br />
-rw-r--r-- 1 peter peter  242 2007-03-06 09:29 package-info.class<br />

I add these to the classpath of the client application in net beans.

Now to call the web service..
The important thing here is that I cannot use annotations to specify the server address,
as that will come from elsewhere.
(And besides hard coding values in to an application is normally bad)

<br />
            URL wsdlLocation = new URL("http://localhost:8080/HelloWorldSessionBeanService/HelloWorldSessionBean?wsdl");<br />
            QName serviceName = new QName("http://localhost:8080/HelloWorldSessionBeanService", "HelloWorldSessionBean");<br />
            HelloWorldSessionBeanService service = new HelloWorldSessionBeanService(wsdlLocation, serviceName);<br />
            HelloWorldSessionBean bean = service.getHelloWorldSessionBeanPort();<br />
            String res = bean.businessMethod("Peter", "Henderson");<br />
            System.out.println("res = " + res);<br />

This code fails on the
HelloWorldSessionBeanService service = new HelloWorldSessionBeanService(wsdlLocation, serviceName);
line with a
<br />
Exception in thread "AWT-EventQueue-0" Cannot create Service instance, service {http://localhost:8080/HelloWorldSessionBeanService}HelloWorldSessionBean not found in the WSDL <a href="http://localhost:8080/HelloWorldSessionBeanService/HelloWorldSessionBean?wsdl" title="http://localhost:8080/HelloWorldSessionBeanService/HelloWorldSessionBean?wsdl">http://localhost:8080/HelloWorldSessionBeanService/HelloWorldSessionBean...</a><br />
        at com.sun.xml.internal.ws.client.ServiceContextBuilder.build(ServiceContextBuilder.java:66)<br />
        at com.sun.xml.internal.ws.client.WSServiceDelegate.(WSServiceDelegate.java:125)<br />
        at com.sun.xml.internal.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:52)<br />
        at javax.xml.ws.Service.(Service.java:57)<br />
        at testwebservice.HelloWorldSessionBeanService.(HelloWorldSessionBeanService.java:36)<br />
        at testwebserviceclient.MainFrame.jButton1ActionPerformed(MainFrame.java:79)<br />

Seems that no matter what I put for the QName serviceName I always get the same error.

So what am I doing wrong, or is what I'm trying todo just not possible?

Many Thanks

Peter Henderson

PS
This is the WSDL

</p>
<p>−</p>
<p>−</p>
<p>−</p>
<p>−</p>
<p>−</p>
<p>−</p>
<p>−</p>
<p>−</p>
<p>−</p>
<p>−</p>
<p>−</p>
<p>−</p>
<p>

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
phenderson
Offline
Joined: 2005-02-16

Well I think I've solved it.

[code]
public static void main(String args[]) {
try {
String server = "localhost:8080";

if (args.length>0) {
server = args[0];
}
System.out.println("Server = '" + server + "'");

long start = System.currentTimeMillis();

//URL wsdlLocation = new URL("http://localhost:8080/HelloWorldSessionBeanService/HelloWorldSessionBean?wsdl");
URL wsdlLocation = new URL("http://" + server + "/HelloWorldSessionBeanService/HelloWorldSessionBean?wsdl");
QName serviceName = new QName("http://testwebservice/", "HelloWorldSessionBeanService");
HelloWorldSessionBeanService service = new HelloWorldSessionBeanService(wsdlLocation, serviceName);
long e1 = System.currentTimeMillis();

HelloWorldSessionBean bean = service.getHelloWorldSessionBeanPort();
long e2 = System.currentTimeMillis();

String res = bean.businessMethod("Peter", "Henderson");
long e3 = System.currentTimeMillis();

System.out.println("res = " + res);

System.out.println("e1 " + (e1-start));
System.out.println("e2 " + (e2-e1));
System.out.println("e3 " + (e3-e2));
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
}
[/code]