How to create a Java client for Web Service?
Asked Answered
P

2

6

I have successfully created Web Service. Tested it and getting the WSDL file also. The client that will use this Web Service is a simple Java class.

I am able to create a jsp client and call the methods of Web Service. But I need to call the Web Service from a Java class.

How do I bind this Java client with Web Service?

The following steps I followed in NetBeans for creating the Java Client...

  1. I created a simple J2SE Application.
  2. Made it a Web Service Client of the WebService made by me.
  3. I'm getting the Web Service References of my WebService.

But I'm not able to call the method of the WebService. Here is the Client file...

package client_package;
public class client {

public static void main(String args[])
{
   System.out.println("1");
   System.out.println(hello("megha"));
   System.out.println("2");
}
private static String hello(String name) {


    WS_package.WebService1 service = new WS_package.WebService1(); //package WS_package does not exists
    WS_package.WebService1 port = service.getWebService1Port(); //package WS_package does not exists


 name =  port.hello(name);

return name;
}
}
Psychotic answered 25/2, 2012 at 7:0 Comment(3)
This may help (in general): https://mcmap.net/q/1620768/-java-webservice-clientHamrick
Do you get an exception when you run this code? If so, can we see the stack trace?Lumbricalis
@MichaelRighi, yes I'm getting an error that package WS_package does not exist. and so i'm not able to use methods of it. port is not getting created and hence method port.hello(name) is not being calledPsychotic
E
2

You could use wsimport tool to generate a client stub files, from command line:

wsimport -keep http://localhost:8080/webservices/helloService?wsdl

then import the generated files and use them like you did above

HelloServiceImplService helloService = new HelloServiceImplService();
HelloService hello = helloService.getHelloServiceImplPort();

There are also some frameworks arround to work with Webservices, like Apache CXF and Apache Axis

Update: Just noticed its an old question, if the OP knew the answer, he should update the topic.

Entablature answered 15/3, 2013 at 16:38 Comment(0)
S
0

You could try Jersey and its Client API

Subglacial answered 27/2, 2012 at 13:31 Comment(1)
I'm now able to bind the simple java client to my webservice. Both reside on single machine. what should I do to bindPsychotic

© 2022 - 2024 — McMap. All rights reserved.