目录
1.服务端
1.1新建webserviceServer服务
1.2 接口实现类
1.3 发布服务
2.客户端实现的几种方式
2.1Wsimport实现方式
2.1.1获取wsdl文件
测试服务是否发布成功,通过阅读wsdl,确定客户端调用的接口、方法、参数和返回值存在,证明服务发布成功
我们在浏览器输入 http://127.0.0.1:9090/Mobile?wsdl
来获取wsdl文件进行阅读wsdl,是以XML文件形式来描述WebService的”说明书”,有了说明书,我们才可以知道如何使用或是调用这个服务。
现在我们还不知道怎么去阅读,后面我们会详解,只要能获取到,就能确定WebService服务发布成功
2.1.2客户端代码
testdemo包内代码为执行2.1.1操作自动生成
执行结果:
2.2 service方式
2.3HttpURLConnection调用方式
3.相关代码
3.1MobileserviceInf
package com.wshy.testdemo;
import javax.management.Query;
/**
* @author wshy
* @data 2020/7/22
**/
public interface MobileServiceInf {
String Query(String phoneNum);
}
3.2MobileServiceImpl
package com.wshy.testdemo;
import javax.jws.WebMethod;
import javax.jws.WebService;
/**
* @author wshy
* @data 2020/7/22
**/
@WebService(
targetNamespace="http://testdemo.wshy.com/", //namespace
portName="MobileSOAPPort",
serviceName="MobileServiceImplService", //服务名称
name="MobileServiceImpl" //portType名称
)
public class MobileServiceImpl implements MobileServiceInf{
@WebMethod(
operationName="Query", //方法名
exclude=false //默认为false-使用webservice,true-不使用webservice
)
@Override
public String Query (String phoneNum) {
System.out.println("获取手机号"+phoneNum);
return "手机号:" + phoneNum;
}
}
3.3webserviceServer
package com.wshy.testdemo;
import javax.xml.ws.Endpoint;
/**
* @author wshy
* @data 2020/7/22
**/
public class MobileServer {
public static void main (String[] args) {
Endpoint.publish("http://127.0.0.1:9090/Mobile", new MobileServiceImpl ());
System.out.println ("发布成功!");
}
}
3.4客户端调用
3.4.1Wsimport实现方式
package com.wshy;
import com.wshy.testdemo.MobileServiceImpl;
import com.wshy.testdemo.MobileServiceImplService;
/**
* @author wshy
* @data 2020/7/22
**/
public class webserviceClient {
public static void main (String[] args) {
//创建服务视图,视图是从wsdl文件的service标签的name属性获取<wsdl:service name="WeatherServiceImplService">
MobileServiceImplService mobileServiceImplService=new MobileServiceImplService();
//获取服务实现类,实现类从wsdl文件的portType的name属性获取<wsdl:portType name="WeatherServiceImpl">
MobileServiceImpl mobileService= mobileServiceImplService.getPort(MobileServiceImpl.class);
//获取查询方法,从portType的operation标签获取
String mobile =mobileService.query("18361393086");
System.out.println(mobile);
}
}
3.4.2service方式
package com.wshy;
import com.wshy.testdemo.MobileServiceImpl;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import java.net.MalformedURLException;
import java.net.URL;
/**
* @author wshy
* @data 2020/7/22
**/
public class client2 {
public static void main (String[] args) throws MalformedURLException {
//创建WSDL文件的URL
URL wsdlDocumentLocation=new URL("http://127.0.0.1:9090/Mobile?wsdl");
//创建服务名称
//1.namespaceURI - 命名空间地址
//2.localPart - 服务视图名
QName serviceName=new QName("http://testdemo.wshy.com/","MobileServiceImplService");
Service service=Service.create(wsdlDocumentLocation, serviceName);
//获取服务实现类
MobileServiceImpl mobileService = service.getPort(MobileServiceImpl.class);
//调用方法
String message=mobileService.query ("1234567890");
System.out.println(message);
}
}
3.4.3 HttpURLConnection调用方式
package com.wshy;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
/**
* @author wshy
* @data 2020/7/22
**/
public class client {
public static void main (String[] args) throws IOException {
//第一步:创建服务地址,不是WSDL地址
URL url = new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx");
//第二步:打开一个通向服务地址的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//第三步:设置参数
//3.1发送方式设置:POST必须大写
connection.setRequestMethod("POST");
//3.2设置数据格式:content-type
connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
//3.3设置输入输出,因为默认新创建的connection没有读写权限,
connection.setDoInput(true);
connection.setDoOutput(true);
//第四步:组织SOAP数据,发送请求
String soapXML = getXML("15202137761");
OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes());
//第五步:接收服务端响应,打印(xml格式数据)
int responseCode = connection.getResponseCode();
if(200 == responseCode){//表示服务端响应成功
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
StringBuilder sb = null;
try {
is = connection.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
sb = new StringBuilder();
String temp = null;
while(null != (temp = br.readLine())){
sb.append(temp);
}
System.out.println(sb.toString());
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
br.close();
isr.close();
is.close();
}
}
os.close();
}
public static String getXML(String phonenum){
String soapXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+"<soap:Body>"
+"<getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">"
+"<mobileCode>"+phonenum+"</mobileCode>"
+"<userID></userID>"
+"</getMobileCodeInfo>"
+"</soap:Body>"
+"</soap:Envelope>";
return soapXML;
}
}