Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

Simply consuming a web service in 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I can recommend you CXF library. Using it you will have several options for calling web services:

  1. 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);
    
  2. Using Java stub generated from WSDL, using wsdl2java.

  3. 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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...