WebService

服务端:

1. webservice类

import javax.jws.WebService;

import javax.jws.WebService;

@WebService
public class HelloWS {

    public String getWhether(String city){
        if(city.equals("beijing")){
            return "bad";
        }else{
            return "good";
        }
    }
}

2. 测试类

import javax.xml.ws.Endpoint;

public class TestClass {

    public static void main(String[] args) {
        Endpoint.publish("http://localhost:12345/helloWS", new HelloWS());
    }
}

3. 服务端测试访问

 客户端:

4. 客户端测试访问方式一:

import test_webservice.HelloWS;
import test_webservice.HelloWSService;
public class ClientTest {

    public static void main(String[] args) {

        HelloWSService hwss = new HelloWSService();
        HelloWS helloWSPort = hwss.getHelloWSPort();
        System.out.println(helloWSPort.getWhether("beijing"));
        System.out.println(helloWSPort.getWhether("tianjin"));
    }
}

5. 客户端测试访问方式二

import test_webservice.HelloWS;

public class TestClient2 {

    public static void main(String[] args) throws MalformedURLException {
        //上哪去找
        URL url = new URL("http://localhost:12345/helloWS"); 
        //找什么
        QName qname = new QName("http://test_webservice/", "HelloWSService");
        //查找视图
        Service service = Service.create(url, qname);
        //得到接口
        HelloWS ws = service.getPort(HelloWS.class);
        System.out.println(ws.getWhether("beijing"));
        System.out.println(ws.getWhether("tianjin"));
    }
}
原文地址:https://www.cnblogs.com/rocky-fang/p/5436614.html