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,一个斜杠都不能少
注意事项:加入新方法的时候,需要重新根据说明书生成代码,要把之前的代码删除
启动成功: