在网上招聘看到有些要会 web service ,发现网上这方面的教程好少,学了一下,学的迷迷糊糊的,也不知道为什么要这样做,这样做有什么用。
不管了还行写下笔记,万一以后会用到呢。
准备
创建一个java项目,普通web都行,这里使用的是maven创建的
导入pom
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.5</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>javax.xml.soap</groupId>
<artifactId>saaj-api</artifactId>
<version>1.3.4</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency>
webService:获取城市天气情况的程序
- 点击java文件夹
使用
public class Main {
public static void main(String[] args) throws Exception {
//建立 第一个接口实例类
WeatherWebServiceLocator sevice = new WeatherWebServiceLocator();
WeatherWebServiceSoap_PortType portType = sevice.getWeatherWebServiceSoap();
String[] citys = portType.getWeatherbyCityName("广安");
for (String city : citys) {
System.out.println(city);
}
}
}
效果
自己建立
@WebService
public class Function {
String name;
public String transWords(String words){
String res = "";
for(char ch : words.toCharArray()){
res += ch+",";
}
return res;
}
public static void main(String[] args) {
// 使用Endpoint(终端)类发布webservice
Function function = new Function();
function.name = "展示";
Endpoint.publish("http://localhost:8089/service/function", function);
System.out.println("Publish Success");
// http://localhost:8089/service/function?wsdl
}
}
访问地址 http://localhost:8089/service/function?wsdl