1、关于Endpoint
@webservice —注解类,用于指定该类变为一个webservice服务类。此时类中的所有非静态方法都会暴露出来,(供外部调用的一个方法),不支持静态方法,final常量方法。
注意:
1、如果不让方法对外公布,此时可以将方法变为static静态方法,或者final常量方法。也可以在方法上添加@webMethod(exclude=true)
2、如果一个类变为了webservice服务类,即使用@webservice注解该类,此时该类中必须要有一个公开的方法,否则会启动webservice服务失败。
2、其他调用webservice的方式
2.1、 推荐使用http://blog.youkuaiyun.com/u012151597/article/details/53737369中的方式调用webservice
2.2 ajax方式调用
<html>
<head>
<title>通过ajax调用WebService服务</title>
<script>
var xhr = new ActiveXObject("Microsoft.XMLHTTP");
function sendMsg(){
var name = document.getElementById('name').value;
//服务的地址
var wsUrl = 'http://192.168.1.100:6789/hello';
//请求体
var soap = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://ws.itcast.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' +
' <soapenv:Body> <q0:sayHello><arg0>'+name+'</arg0> </q0:sayHello> </soapenv:Body> </soapenv:Envelope>';
//打开连接
xhr.open('POST',wsUrl,true);
//重新设置请求头
xhr.setRequestHeader("Content-Type","text/xml;charset=UTF-8");
//设置回调函数
xhr.onreadystatechange = _back;
//发送请求
xhr.send(soap);
}
function _back(){
if(xhr.readyState == 4){
if(xhr.status == 200){
//alert('调用Webservice成功了');
var ret = xhr.responseXML;
var msg = ret.getElementsByTagName('return')[0];
document.getElementById('showInfo').innerHTML = msg.text;
//alert(msg.text);
}
}
}
</script>
</head>
<body>
<input type="button" value="发送SOAP请求" onclick="sendMsg();">
<input type="text" id="name">
<div id="showInfo">
</div>
</body>
</html>
2.3通过UrlConnection调用
其中使用到的SAXReader等类,需要用到dom4jfull.jar包,下载地址http://download.youkuaiyun.com/detail/u012151597/9715328
import java.io.*;
import java.net.*;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class URLTest {
/**
* @description : webservice 接口 urlconnnection 调用方式
* @date 2016-12-19
* @author liucong
* @param args
* @throws Exception
*
*
*/
public static void main(String[] args){
URL url = null;
//打开连接
URLConnection conn = null;
try {
url = new URL("http://localhost:9001/webService/firstWebService");
conn = url.openConnection();
//转换成HttpURL
HttpURLConnection httpConn = (HttpURLConnection) conn;
//打开输入输出的开关
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
//设置请求方式
httpConn.setRequestMethod("POST");
//设置请求的头信息
httpConn.setRequestProperty("Content-type", "text/xml;charset=UTF-8");
//拼接请求消息
//注意:拼接请求头时,需要修改的地方是1.xmlns:q0 需要修改为具体的哪个类,
//2、q0:theFirstWebMethod 修改为具体的服务类需要调用的那个方法。
String data = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:q0=\"http://function.lc.com/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
"<soapenv:Body> <q0:theFirstWebMethod> </q0:theFirstWebMethod> </soapenv:Body> </soapenv:Envelope>";
//获得输出流
OutputStream out = httpConn.getOutputStream();
//发送数据
out.write(data.getBytes());
//判断请求成功
if(httpConn.getResponseCode() == 200){
//获得输入流
InputStream in = httpConn.getInputStream();
//使用输入流的缓冲区
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuffer sb = new StringBuffer();
String line = null;
//读取输入流
while((line = reader.readLine()) != null){
sb.append(line);
}
//创建sax的读取器
SAXReader saxReader = new SAXReader();
//创建文档对象
Document doc = saxReader.read(new StringReader(sb.toString()));
//获得请求响应return元素
List<Element> eles = doc.selectNodes("//return");
for(Element ele : eles){
System.out.println(ele.getText());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}