什么是webservice就不在多少了,百度很多解释,这里主要是告诉大家怎么使用webservice。
服务端:
第一步:创建SEI(Service Endpoint Interface)接口,本质上就是Java接口
第二步:创建SEI实现类,在实现类上加入@WebService
第三步:发布服务,Endpoint发布服务,publish方法,两个参数:1.服务地址;2.服务实现类
第四步:测试服务是否发布成功,通过阅读使用说明书,确定客户端调用的接口、方法、参数和返回值存在,证明服务发布成功。
WSDL地址:服务地址+”?wsdl”
WSDL阅读方式:从下往上
客户端 :
l 第一步:wsimport命令生成客户端代码
2 第二步:根据使用说明书,使用客户端代码调用服务端
2.1 第一步:创建服务视图,视图是从service标签的name属性获取
2.2 第二步:获取服务实现类,实现类从portType的name属性获取
2.3 第三步:获取查询方法,从portType的operation标签获取
WSDL:
WSDL及web服务描述语言,他是webservice服务端使用说明书,说明服务端接口、方法、参数和返回值,WSDL是随服务发布成功,自动生成,无需编写
l <service> 服务视图,webservice的服务结点,它包括了服务端点
<binding> 为每个服务端点定义消息格式和协议细节
<portType> 服务端点,描述 web service可被执行的操作方法,以及相关的消息,通过binding指向portType
<message> 定义一个操作(方法)的数据参数(可有多个参数)
<types> 定义 web service使用的全部数据类型
阅读方式:从下往上
* SOAP
l SOAP即简单对象访问协议,他是使用http发送的XML格式的数据,它可以跨平台,跨防火墙,SOAP不是webservice的专有协议。
2 SOAP=http+xml
DEMO:
公网服务地址:
http://www.webxml.com.cn/zh_cn/index.aspx
service编程调用方式:
public class ServiceClient {
public static void main(String[]args) throws IOException {
//创建WSDL的URL,注意不是服务地址
URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl");
//创建服务名称
//1.namespaceURI - 命名空间地址
//2.localPart - 服务视图名
QName qname = new QName("http://WebXml.com.cn/","MobileCodeWS");
//创建服务视图
//参数解释:
//1.wsdlDocumentLocation - wsdl地址
//2.serviceName - 服务名称
Service service = Service.create(url,qname);
//获取服务实现类
MobileCodeWSSoap mobileCodeWSSoap = service.getPort(MobileCodeWSSoap.class);
//调用查询方法
String result =mobileCodeWSSoap.getMobileCodeInfo("1866666666","");
System.out.println(result);
}
}
HttpURLConnection调用方式:
第一步:创建服务地址
第二步:打开一个通向服务地址的连接
第三步:设置参数
第四步:组织SOAP数据,发送请求
第五步:接收服务端响应,打印
public class HttpClient {
public static void main(String[]args) throws IOException {
//第一步:创建服务地址,不是WSDL地址
URL url = new URL("http://webservice.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("15226466316");
OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes());
//第五步:接收服务端响应,打印
int responseCode =connection.getResponseCode();
if(200 == responseCode){//表示服务端响应成功
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String temp = null;
while(null != (temp =br.readLine())){
sb.append(temp);
}
System.out.println(sb.toString());
is.close();
isr.close();
br.close();
}
os.close();
}
/**
* <?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>string</mobileCode>
<userID>string</userID>
</getMobileCodeInfo>
</soap:Body>
</soap:Envelope>
* @param phoneNum
* @return
*/
public static String getXML(StringphoneNum){
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;
}
}
*用注解修改WSDL内容
@WebService-定义服务,在public class上边
targetNamespace:指定命名空间
name:portType的名称
portName:port的名称
serviceName:服务名称
endpointInterface:SEI接口地址,如果一个服务类实现了多个接口,只需要发布一个接口的方法,可通过此注解指定要发布服务的接口。
@WebMethod-定义方法,在公开方法上边
operationName:方法名
exclude:设置为true表示此方法不是webservice方法,反之则表示webservice方法,默认是false
@WebResult-定义返回值,在方法返回值前边
name:返回结果值的名称
@WebParam-定义参数,在方法参数前边
name:指定参数的名称
作用:
通过注解,可以更加形像的描述Web服务。对自动生成的wsdl文档进行修改,为使用者提供一个更加清晰的wsdl文档。
当修改了WebService注解之后,会影响客户端生成的代码。调用的方法名和参数名也发生了变化
---------------------------------------------------------------------------------------------------C----X------F---------------------------------------------------------------------------------------------------------CXF+Spring整合发布SOAP协议的服务
服务端:
开发步骤:
第一步:创建web项目(引入jar包)
第二步:创建SEI接口
第三步:创建SEI实现类
第四步:配置spring配置文件,applicationContext.xml,用<jaxws:server标签发布服务,设置1.服务地址;2.设置服务接口;3设置服务实现类
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs"xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> <!-- <jaxws:server发布SOAP协议的服务,对JaxWsServerFactoryBean类封装--> <jaxws:serveraddress="/weather"serviceClass="cn.itcast.ws.cxf.server.WeatherInterface"> <jaxws:serviceBean> <refbean="weatherInterface"/> </jaxws:serviceBean> </jaxws:server> <!-- 配置服务实现类 --> <beanname="weatherInterface"class="cn.itcast.ws.cxf.server.WeatherInterfaceImpl"/> </beans> |
第五步:配置web.xml,配置spring配置文件地址和加载的listener,配置CXF的servlet
<?xmlversion="1.0"encoding="UTF-8"?> <web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID"version="3.0"> <display-name>ws_2_cxf_spring_server</display-name>
<!-- 设置spring的环境 --> <context-param> <!--contextConfigLocation是不能修改的 --> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
<!-- 配置CXF的Servlet --> <servlet> <servlet-name>CXF</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXF</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping>
<welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app> |
第六步:部署到tomcat下,启动tomcat
第七步:测试服务,阅读使用说明书
WSDL地址规则:http://ip:端口号/项目名称/servlet拦截路径/服务名称?wsdl
客户端:
开发步骤:
第一步:引入jar包
第二步:生成客户端代码
第三步:配置spring配置文件,applicationContent.xml
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs"xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> <!-- <jaxws:client实现客户端,对JaxWsProxyFactoryBean类封装--> <jaxws:clientid="weatherClient"address="http://127.0.0.1:8080/ws_2_cxf_spring_server/ws/weather" serviceClass="cn.itcast.cxf.weather.WeatherInterface"/> </beans> |
第四步:从spring上下文件获取服务实现类
第五步:调用查询方法,打印
publicclass WeatherClient {
publicstaticvoid main(String[] args) {
//初始化spring的上下文
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
WeatherInterfaceweatherInterface = (WeatherInterface) context.getBean("weatherClient");
String weather = weatherInterface.queryWeather("保定");
System.out.println(weather);
}
}