首先想創建Webservice和調用Webservice需要以下幾個包
所需要的jar在最底下
1.先創建一個class作爲服務端
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
/**
* @author xiaoxiang
* @date
*/
@WebService
public class webServiceController {
/** 供客戶端調用方法 該方法是非靜態的,會被髮布
* @param name 傳入參數
* @return String 返回結果
* */
@WebMethod(operationName = "getValue")
public String getValue(@WebParam(name = "name") String name){
return "歡迎你! "+name+"-------------";
}
/**
* 方法上加@WebMentod(exclude=true)後,此方法不被髮布;
* @param name
* @return
*/
@WebMethod(exclude=true)
public String getHello(String name){
System.out.println("==============");
return "你好! "+name;
}
/** 靜態方法不會被髮布
* @param name
* @return
*/
public static String getString(String name){
return "再見!"+name;
}
//通過EndPoint(端點服務)發佈一個WebService
public static void main(String[] args) {
/*參數:1,本地的服務地址;
2,提供服務的類;
*/
Endpoint.publish("http://192.168.1.155:8085/Service/ServiceHello", new webServiceController());
System.out.println("發佈成功!");
//發佈成功後 在瀏覽器輸入 http://192.168.1.155:8085/Service/ServiceHello?wsdl
}
}
附圖:
先把項目跑起來(也可以不跑),然後執行main方法。效果如下:
這樣就說明你的WebService接口已經創建好了
然後在瀏覽器地址中訪問你設置的地址後面加上?wsdl
例:http://192.168.1.155:8085/Service/ServiceHello?wsdl
執行結果
出現這個說明你的webservice接口已經調通了
2.調用WebService接口
前面的該鋪墊的都已經鋪鋪墊好啦
剩下的就交給代碼執行啦
代碼如下:
import java.net.HttpURLConnection;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.encoding.XMLType;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
/**
* @author xiaoxiang
* @date 2019/9/27 16:45
*/
public class text {
public static void main(String[] args) throws Exception{
String endpoint = "http://192.168.1.155:8085/Service/ServiceHello?wsdl";
HttpURLConnection connection = (HttpURLConnection) new URL(endpoint).openConnection();
if(HttpURLConnection.HTTP_OK == connection.getResponseCode()){//測試是否通了
try {
//直接引用遠程的wsdl文件 //以下都是套路
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new URL(endpoint));
/**注意這裏,要設置Namespace
* new QName(namespace,WSDL裏面描述的接口名稱)*/
call.setOperationName(new QName("http://text/","getValue"));//WSDL裏面描述的接口名稱
call.addParameter("name", XMLType.XSD_STRING, ParameterMode.IN);//接口的參數
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//設置返回類型
Object result = call.invoke(new String[]{"789"});
//給方法傳遞參數,並且調用方法
System.out.println("result is :"+result);
}
catch (Exception e) {
System.err.println(e.toString());
}
}
}
}
附圖:
提醒一下:代碼中的
(“http://text/”)的就是你訪問路徑後的到的namespace;
(“getValue”)是你要調用的方法名;
(”name“)是方法中的參數名字;
服務器端的參數一定要寫@WebParam(name = “name”)不然的話接不到參數
(”789“)就是你要穿的參數對應的內容啦;
如果有多個參數的話 call.addparameter就可以有多個,參數內容的話就以逗號分隔開就行啦
然後執行這個main方法就能得到Webservice返回的結果啦
上面是通過url來訪問接口的,還有個是在本地項目內的
這個和上面沒上面大的區別 第一步都是一樣的
首先新建一個web項目 然後目錄是這樣的
JwsServiceHello.java這個就是服務端java
package service;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
/**
* Title: ServiceHello
* Description: 基於jdk1.6以上的javax.jws 發佈webservice接口
@WebService - 它是一個註解,用在類上指定將此類發佈成一個ws。
Endpoint – 此類爲端點服務類,它的方法publish用於將一個已經添加了@WebService註解
對象綁定到一個地址的端口上。
* Version:1.0.0
* @author panchengming
*/
@WebService
public class JwsServiceHello {
/** 供客戶端調用方法 該方法是非靜態的,會被髮布
* @param name 傳入參數
* @return String 返回結果
* */
@WebMethod(operationName = "getValue1")
public String getValue1(String name){
return "歡迎你! "+name;
}
/**
* 方法上加@WebMentod(exclude=true)後,此方法不被髮布;
* @param name
* @return
*/
@WebMethod(exclude=true)
public String getHello(String name){
return "你好! "+name;
}
/** 靜態方法不會被髮布
* @param name
* @return
*/
public static String getString(String name){
return "再見!"+name;
}
//通過EndPoint(端點服務)發佈一個WebService
public static void main(String[] args) {
/*參數:1,本地的服務地址;
2,提供服務的類;
*/
Endpoint.publish("http://192.168.1.155:7071/Service/ServiceHello1", new JwsServiceHello());
System.out.println("發佈成功!");
//發佈成功後 在瀏覽器輸入 http://192.168.1.155:7071/Service/ServiceHello?wsdl
}
}
直接運行main方法就行啦
然後右鍵src 找到web service client
下一步
然後點擊next就行啦 隨後會生成這些文件
然後新建一個class 測試用
import ws.JwsServiceHello;
import ws.JwsServiceHelloService;
public class text {
public static void main(String[] args) throws Exception {
//調用webservice
JwsServiceHello hello=new JwsServiceHelloService().getJwsServiceHelloPort();
String name=hello.getValue("panchengming");
System.out.println(name);
}
}
運行就可以啦
所需要的jar;提取碼:wosg
ok 就到這裏啦,上面的是通過路徑來訪問接口,下面的是通過本地生成對應的接口然後去使用,看個人需求然後去使用;
有哪裏寫的不好的還望多多包含