利用jdk的wsimport.exe生成WebServices客户端代码


//服务端发布

package com.ws.server.test;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService
public class MyEndPointer {
    public static void main(String[] args) {
        publish();
    }
  
    private static void publish() {
        Endpoint.publish("http://127.0.0.1:8989/mywebservice", new MyImpl());
        
    }
}
@WebService
public class MyImpl {
    @WebMethod
    public String sayHello(String name) {
        System.out.println("WS server: " + name);
        return "Hi " + name;
    }
}
//客户端调用,先用wsimport -keep [wsdl地址] 生成客户端代码
public class ClientTest {
    public static void main(String[] args) {
        test();
    }

    private static void test() {
        try {
            MyImplService implService = new MyImplService();
            MyImpl myImplPort = implService.getMyImplPort();
            String sayHello = myImplPort.sayHello("少林");
            System.out.println(sayHello);
        } catch (WebServiceException e) {
            System.out.println("连接失败");
        }
        
    }
}

部分截图如下:

原文地址:https://www.cnblogs.com/yushouling/p/4857112.html