5.开发webservice

package com.atguigu.ws;

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

/**
 * 
 * @author Administrator
 *
 */
@WebService
public interface HelloWS {
    
    @WebMethod
    public String sayHello(String name);
}
package com.atguigu.ws.impl;

import javax.jws.WebService;

import com.atguigu.ws.HelloWS;

@WebService
public class HelloWSImpl implements HelloWS {

    @Override
    public String sayHello(String name) {
        System.out.println("server sayHello() "+name);
        
        return "Hello "+name;
    }
    
}
package com.atguigu.server;

import javax.xml.ws.Endpoint;

import com.atguigu.ws.impl.HelloWSImpl;

/**
 * 发布webservice
 * @author Administrator
 *
 */
public class ServerTest {

    public static void main(String[] args) {
        String address = "http://192.168.0.114:8989/day01_ws/hellows";
        //Endpoint发布
        Endpoint.publish(address, new HelloWSImpl());
        System.out.println("发布webservice成功");
    }
}

 

1).打开cmd工具窗口,进入客户端src目录(可拖拽进入)执行wsimport -keep url命令,进入eclipse刷新客户端src目录

package com.atguigu.ws.client.test;

import com.atguigu.ws.impl.HelloWSImpl;
import com.atguigu.ws.impl.HelloWSImplService;

/**
 * 调用webservice
 * @author Administrator
 *
 */
public class ClientTest {

    public static void main(String[] args) {
        /*
         * 产生代理对象
         */
        HelloWSImplService factory = new HelloWSImplService();
        HelloWSImpl helloWS = factory.getHelloWSImplPort();
        System.out.println(helloWS);
    
        String result = helloWS.sayHello("Jack");
        System.out.println(result);
    }
}

测试结果:

客户端:

服务器端:

原文地址:https://www.cnblogs.com/tingbogiu/p/6041726.html