Solon v3.0.5

Java 原生用法

</> markdown

此方案是 java 自带的功能,不需要引入任何框架(或者引入 javaee 的 jws 接口包)。下面的代码可在 java8 下直接运行(建议开启编译参数:-parameters,避免参数名变成 arg0...)。

示例源码详见:

1、服务端

不需要引入任何依赖(java 自带的能力),但是服务端需要独占一个端口

  • 定义一个服务组件
//文件 demo/server/WsInterfaceImpl.class

import javax.jws.WebService;

@WebService(name = "WsInterface", serviceName = "WsInterface", targetNamespace = "http://impl.xcc.com/")
public class WsInterfaceImpl {
    public String sayInputName(String name) {
        return "input: " + name;
    }
}
  • 启动服务端
//文件 demo/server/ServerTest.class

import javax.xml.ws.Endpoint;

public class ServerTest {
    public static void main(String[] args) {
        WsInterfaceImpl ws = new WsInterfaceImpl();

        Endpoint.publish("http://localhost:8888/service-ws/demo", ws);

        System.out.println("server 启动成功: http://localhost:8888/service-ws/demo?wsdl");
    }
}

2、客户端

注意 “targetNamespace” 要前后保持一至。

  • 生成类(也可以手写)

通过 wsimport 指令导入 http://localhost:8888/service-ws/demo?wsdl 后会生成一批类,只需要其中的:

//文件 demo/client/WsInterface.class

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService(targetNamespace = "http://impl.xcc.com/")
public interface WsInterface {
    @WebMethod
    public String sayInputName(String name);
}
//文件 demo/client/WsInterfaceImplService.class

import javax.xml.namespace.QName;
import javax.xml.ws.*;
import java.net.MalformedURLException;
import java.net.URL;


@WebServiceClient(name = "WsInterface", targetNamespace = "http://impl.xcc.com/", wsdlLocation = "http://localhost:8888/service-ws/demo?wsdl")
public class WsInterfaceImplService extends Service {

    private final static URL WSINTERFACEIMPLSERVICE_WSDL_LOCATION;
    private final static WebServiceException WSINTERFACEIMPLSERVICE_EXCEPTION;
    private final static QName WSINTERFACEIMPLSERVICE_QNAME = new QName("http://impl.xcc.com/", "WsInterface");

    static {
        URL url = null;
        WebServiceException e = null;
        try {
            url = new URL("http://localhost:8888/service-ws/demo?wsdl");
        } catch (MalformedURLException ex) {
            e = new WebServiceException(ex);
        }
        WSINTERFACEIMPLSERVICE_WSDL_LOCATION = url;
        WSINTERFACEIMPLSERVICE_EXCEPTION = e;
    }

    public WsInterfaceImplService() {
        super(__getWsdlLocation(), WSINTERFACEIMPLSERVICE_QNAME);
    }

    public WsInterfaceImplService(WebServiceFeature... features) {
        super(__getWsdlLocation(), WSINTERFACEIMPLSERVICE_QNAME, features);
    }

    public WsInterfaceImplService(URL wsdlLocation) {
        super(wsdlLocation, WSINTERFACEIMPLSERVICE_QNAME);
    }

    public WsInterfaceImplService(URL wsdlLocation, WebServiceFeature... features) {
        super(wsdlLocation, WSINTERFACEIMPLSERVICE_QNAME, features);
    }

    public WsInterfaceImplService(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

    public WsInterfaceImplService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
        super(wsdlLocation, serviceName, features);
    }

    @WebEndpoint(name = "WsInterfacePort")
    public WsInterface getWsInterfacePort() {
        return super.getPort(new QName("http://impl.xcc.com/", "WsInterfacePort"), WsInterface.class);
    }

    @WebEndpoint(name = "WsInterfacePort")
    public WsInterface getWsInterfacePort(WebServiceFeature... features) {
        return super.getPort(new QName("http://impl.xcc.com/", "WsInterfacePort"), WsInterface.class, features);
    }

    private static URL __getWsdlLocation() {
        if (WSINTERFACEIMPLSERVICE_EXCEPTION != null) {
            throw WSINTERFACEIMPLSERVICE_EXCEPTION;
        }
        return WSINTERFACEIMPLSERVICE_WSDL_LOCATION;
    }
}
  • 启动客户端
//文件 demo/client/ClientTest.class

public class ClientTest {
    public static void main(String[] args) {
        WsInterface ws = new WsInterfaceImplService().getWsInterfacePort();
        String name = ws.sayInputName("demo");
        System.out.println(name);
    }
}