web service

2018-04-24  本文已影响10人  RLM233
项目结构

1 创建服务端

package com.lm.ws;

public interface WeatherInterface {
    public String queryWeather(String cityName);
}
package com.lm.ws;
import javax.jws.WebService;

@WebService
public class WeatherInterfaceImpl implements WeatherInterface {

    public String queryWeather(String cityName) {
        System.out.println("服务端:"+cityName);
        String weather = "晴";
        return weather;
    }
}
package com.lm.ws;
import javax.xml.ws.Endpoint;

public class WeatherService {
    public static void main(String[] args) {
        Endpoint.publish("http://127.0.0.1:12345/weather", new WeatherInterfaceImpl());
        System.out.println("web service success");
    }
}

2 创建客户端

package com.lm.client;

import com.lm.ws.WeatherInterfaceImpl;
import com.lm.ws.WeatherInterfaceImplService;

public class WeatherClient {
    public static void main(String[] args) {
        //创建服务视图
        WeatherInterfaceImplService weatherInterfaceImplService = new WeatherInterfaceImplService();
        //获取服务实现类
        WeatherInterfaceImpl weatherInterfaceImpl = weatherInterfaceImplService.getPort(WeatherInterfaceImpl.class);
        //调用查询方法
        String weather = weatherInterfaceImpl.queryWeather("上海");
        System.out.println(weather);
    }
}
上一篇 下一篇

猜你喜欢

热点阅读