WebService的服务端与客户端的创建

1、引入pom文件:

<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-frontend-jaxws -->
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>3.5.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http -->
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>3.5.0</version>
</dependency>
<!-- 日志查看-->
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-features-logging -->
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-features-logging</artifactId>
    <version>3.5.0</version>
    <!--<scope>test</scope>-->
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http-jetty</artifactId>
    <version>3.2.4</version>
</dependency>

1.1、服务端开发:

public interface WeatherInterface {
    /**
     * 根据城市查询天气
     * @param cityName
     * @return
     */
    public String queryWeather(String cityName);
}
import com.xdap.lyncko.gx.integration.service.WeatherInterface;
import org.springframework.stereotype.Service;
import javax.jws.WebService;

@WebService
@Service
public class WeatherInterfaceImpl implements WeatherInterface {
    @Override
    public String queryWeather(String cityName) {
        System.out.println("获取城市名"+cityName);
        String weather="暴雨";
        return weather;
    }
}

import javax.xml.ws.Endpoint;
public class WeatherServer {
    public static void main(String[] args) {
        Endpoint.publish("http://localhost:9091/weather", new WeatherInterfaceImpl());
        System.out.println("调用成功!");
    }
}

网页打开:http://localhost:9091/weather?wsdl

 2、客户端开发:

第一步:根据服务的访问地址生成客户端的代码:

打开cmd:

切换路径到客户端的src目录下:
C:\Users\ZhangLin>d:
D:>cd D:\1-CODE\Definesys\MyWebService\src
D:\1-CODE\Definesys\MyWebService\src>wsimport
wsimport -s .是选择要生成文件的目录,将url地址拼接进来访问即可
D:\1-CODE\Definesys\MyWebService\src>wsimport -s . http://localhost:9091/weather?wsdl

如下说明生成成功:

 第二步:代码生成如下:

第三步:修改接口中的className的路径,使之与变更后的文件路径保持一致 

第四步:根据地址发送请求:

调用方法一:

 调用方法二:QName中的第一个参数是targetNamespace,一个斜杠都不能少

 注意事项:加入新方法的时候,需要重新根据说明书生成代码,要把之前的代码删除

 启动成功:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值