1.使用jdk發佈webservice
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
/**
*
* @author lili
*/
@WebService
public class TestWebService {
/** 供客戶端調用方法 該方法是非靜態的,會被髮布
* @param name 傳入參數
* @return 返回結果
* */
public String getValue(String name){
System.out.println("okokokoko: "+name);
return "hello "+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.4:8081/Service/ServiceHello", new TestWebService());
System.out.println("發佈成功!");
//發佈成功後 在瀏覽器輸入 http://192.168.1.4:8081/Service/ServiceHello?wsdl
}
}
2. java遠程訪問 webservice
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
public static void main(String[] args) {
try {
String endpoint = URL;
// 直接引用遠程的wsdl文件
// 以下都是套路
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(endpoint);
call.setOperationName(new QName("http://testwebservice.mycompany.com/","Hello"));// WSDL裏面描述的接口名稱
call.addParameter("arg0", XMLType.XSD_DATE,ParameterMode.IN);// 接口的參數 “使用arg0” 服務端才能接收到數據
call.setReturnType(XMLType.XSD_STRING);// 設置返回類型
String temp = "ddd"; //訪問數據
String result = (String) call.invoke(new Object[]{temp});
// 給方法傳遞參數,並且調用方法
System.out.println("result is " + result);
} catch (Exception e) {
System.err.println(e.toString());
}
return "";
}