WebService学习入门(SOAP协议,ApacheCXF入门,基于CXF技术开发CRM服务器)
来源:互联网 发布:网络语青铜是什么意思 编辑:程序博客网 时间:2024/05/22 10:32
webService
什么是Service
WebService 就是两个系统之间远程调用的技术,WebService之间的调用可以实现跨语言调用,因为调用的时Http协议,传输的数据格式为xml格式
这个链接提供了一些服务,包含天气,手机归属地查询等
下边这张图更加形象描述什么是WebService
WebService特点
WebService通过HTTP POST请求接受客户的请求
WebService与客户端之家一般使用SOAP协议传输xml数据
它本身就是为了跨平台或者跨语言而设计的。
什么是SOAP
SOAP是基于HTTP的,它时一个基于xml语言的协议用语在网上的传输数据,
SOAP=在HTTP的基础上+xml数据
SOAP的组成如下
Envelope一部分必须要写的,以xml的根元素出现
Headers也是可选的
Body必须的,在Body部分,包含要执行的服务器的方法,和发送到服务器的的数据、
SOAP请求的代码标准样例
POST /WebServices/MobileCodeWS.asmx HTTP/1.1Host: ws.webxml.com.cnContent-Type: text/xml; charset=utf-8Content-Length: lengthSOAPAction: "http://WebXml.com.cn/getDatabaseInfo"<?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> <getDatabaseInfo xmlns="http://WebXml.com.cn/" /> </soap:Body></soap:Envelope>
和普通的http协议在于content-Type:是text/xml 而不是text/html
SOAP响应代码样例
HTTP/1.1 200 OKContent-Type: text/xml; charset=utf-8Content-Length: length<?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> <getDatabaseInfoResponse xmlns="http://WebXml.com.cn/"> <getDatabaseInfoResult> <string>string</string> <string>string</string> </getDatabaseInfoResult> </getDatabaseInfoResponse> </soap:Body></soap:Envelope>
SOAP与HTTP协议对比
WSDL Web服务描述语言
WSDL,Web Service Description Language web服务描述语言
就是一个xml文档,用于描述当前服务的一些信息,(服务名称,服务发布地址,服务提供的方法,方法的参数类型,方法的返回值类型)
基于JDK1.7发布一个WebService服务
步骤如下
- 创建一个java项目(利用开发工具eclipse或者其他)
- 创建一个类,加入WebService注解
- 提供一个方法sayHello(参数任意)
- 在main方法中调用jdk提供的发布服务器方法
- 访问服务的wsdl文档
- 服务地址就是,服务发布地址加上“?wsdl”
Eg:: http://192.168.25.1:8080/hello?wsdl
代码样例如下:package com.web.service;import javax.jws.WebService;import javax.xml.ws.Endpoint;@WebServicepublic class HelloService { public String sayHello(String name,int i) { System.out.println("服务端的sayHelo"); return "hello" + name+i; } public static void main(String[] args) {//服务器的发布地址 String address = "http://192.168.25.1:8080/hello"; Object implementor = new HelloService();// 提供服务的实例利用静态方法public进行发布 Endpoint.publish(address, implementor); }}
客户端的请求上边的服务
Jdk中wsimport命令的使用
Wsimport是jdk自带的,可以根据wsdl文档生成wsdl文档生成客户端调试代码的工具,当然无论服务器的WeService都是用什么语言写的,都将在客户端生成java代码,服务器端用什么写的并不重要,
Wsimport.exe位于JAVA_HOME\bin目录下,
常用参数为
-d<目录>-将生成的.class文件,默认参数
-s<目录>-将生成的.java文件
-p<生成的新包名>-将生成的泪,放于制定的包下,
(wsdlurl)-http://server:port/one?wsdl,必要的参数
示例:
C:/>wsimport –s. http://192.168.0.100/one?wsdl
注意:-s不能分开,-后边有个小点,用于制定源代码生成的目录,点即当前目录。
如果使用了-s参数则会在目录下生成两分代码,一份为.class 一份为.java代码。
.class代码可以经过打包后使用,.java代码可以直接copy到我们要是用的项目中
操作原理:
1. 使用wsimport命令解析wsdl文件生成的本地diamagnetic
2. 通过本地代码创建一个代理对象
3. 通过代理对象实现远程调用
4. 代码如下
2. public class AppTest {3. public static void main(String[] args) {4. HelloServiceService service = new HelloServiceService();5. HelloService proxy = service.getHelloServicePort();6. String string = proxy.sayHello("小明", 12);7. System.out.println(string);8. 9. }10. }
以上就是利用jdk发布的一个webService服务
Apache CXF入门
CXF是apache公司提供的用于支持WebService的一个开源框架
我们要使用该框架,就必须下载开发包
官网:cxf.apache.org
特点:Apache CXF=Celtix_xFIre
支持多种协议:
SOAP
XML/HTTP
COREA(Common Object Request,Broker Architecture)公共对象请求代理体系,早期语言使用的WS
并且可以与Spring进行快速的无缝整合,
灵活的额部署,可以运行在Tomcat :可以永兴在Tomact.Jboss jettt(内置) IBMWS BeaWL上边
CXF入门案例
服务端
- 创建动态web项目
- 导入相关的jar包(包含spring28个)
- 在web.xml文件中配置
<!-- 配置CXF的Servlet --> <servlet> <servlet-name>cxfservlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <init-param> <param-name>config-location</param-name> <param-value>classpath:cxf.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>cxfservlet</servlet-name> <url-pattern>/service/*</url-pattern> </servlet-mapping>
- 在类路径下提供cxf.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"xmlns:soap="http://cxf.apache.org/bindings/soap"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- 引入CXF Bean定义如下,早期的版本中使用 --> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /></beans>
- 开发一个接口和实现类:
import javax.jws.WebService;@WebServicepublic interface HelloService { public String sayHello(String name);}实现类package com.cxf.inter;public class HelloServiceImpl implements HelloService { @Override public String sayHello(String name) { System.out.println("基于cxf开发的service sayHello方法"); return "hello" + name; }}注意注解一定要放在接口上
- 在cxf.Xml文件中注册服务
<!-- 配置service实现类 --> <bean id="helloService" class="com.cxf.inter.HelloServiceImpl"></bean> <!-- 注册服务 --> <jaxws:server id="myService" address="/cxfService"> <jaxws:serviceBean> <ref bean="helloService" /> </jaxws:serviceBean> </jaxws:server>
CXF入门案例(客户端)
方式一:
使用jdk提供的wsimport命令生成本地代码完成调用
就是通过wsimport -s . http://192.168.25.1:8080/projectName/service/myService?wsdl
生成代码,我们可以通过利用java工程调用生成的代码来实现本地代码的调用。从而可以完成客户端服务器的链接调用。这就是方式一
方式二:
使用CXF提供的方法方式
第一步骤:将新建的java项目并且倒入cxf相关jar包
第二步骤:使用wsimport或者cxf提供的wsdl2java生成的所有接口文件,采用如下命令来获取 所有的接口文件(由于该命令没有在环境中配置,因此women可以找到cxf开发报找到bin目录下打开命令窗口然后输入该命令即可)
第三步:将生成接口文件复制到客户端项目中
第四步:提供spring配置文件,注册客户端代理对象
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:soap="http://cxf.apache.org/bindings/soap" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- 注册CXF客户端代理对象,通过spring狂剑创建这个代理对象,可以使用代理对象实现远程调用 --> <jaxws:client id="myClient" address="http://192.168.25.1:8080/cxf_service/service/cxfService" serviceClass="cn.itcast.client.HelloService"></jaxws:client></beans>
第五步:读取spring配置文件,创建spring工厂,创建代理对象,从而实现远程调用
package com.itheima.service;public class AppTest { public static void main(String[] args) { // TODO Auto-generated method stub HelloServiceImplService helloServiceImplService = new HelloServiceImplService(); // 创建代理对象 HelloService proxy = helloServiceImplService.getHelloServiceImplPort(); // 利用代理对象访问 String string = proxy.sayHello("张三"); System.out.println(string); }}
基于CXF开发crm服务
数据库环境的搭建
第一步创建数据库
第二步创建数据表customer
WEB环境的搭建
第一步:创建动态web项目
第二步:导入到CXF相关jar包
第三步:配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns: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_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>crm_wdl</display-name> <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> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:cxf.xml</param-value> </context-param> <!-- 配置监听器,项目启动的时候就配置Servlet,如果不配置就是首次访问时候才配置 --> <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> <!-- 通过初始化参数指定CXF框架的配置文件位置 --> <init-param> <param-name>config-location</param-name> <param-value>classpath:cxf.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/service/*</url-pattern> </servlet-mapping></web-app>
第四步:在类路径下提供cxf.xml文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:soap="http://cxf.apache.org/bindings/soap" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"></beans>
第五步:针对t_customer表创建一个Customer客户实体类
```public Class Customer{ private int id; private String name; prviate String station; private String telephone; private String address; private String decidezone_id; setter getter方法 toString() 有参无参构造器。。。 }
第六步:开发接口和实现类
接口
@WebServicepublic interface ICustomerService{ public List<Customer> findall();}
第七步:创建一个java工程,通过wsimport 命令生成代码将所有java代码引入到客户端java工程
建立一个类书写main方法
package com.itheima.crm.service;import java.util.List;public class AppTest { public static void main(String[] args) { // TODO Auto-generated method stub CustomerServiceImplService ss = new CustomerServiceImplService(); //创建代理对象 ICustomerService proxy = ss.getCustomerServiceImplPort(); //通过代理对象来调用服务的方法,从而输出数据库中的内容 List<Customer> result = proxy.findAll(); for(Customer c:result){ System.out.println(c.toString()); } }}
- WebService学习入门(SOAP协议,ApacheCXF入门,基于CXF技术开发CRM服务器)
- WebService CXF学习(入门篇3):WSDL、SOAP描述
- 基于Spring+CXF技术开发WebService
- WebService CXF学习(入门篇5):CXF+Spring+Hibernate
- Spring+CXF开发基于SOAP协议的WebService
- cxf基于soap的webservice
- WebService CXF学习(入门篇2):HelloWorld
- WebService CXF学习(入门篇2):HelloWorld示例
- WebService CXF学习(入门篇2):HelloWorld
- WebService CXF学习(入门篇2):HelloWorld
- WebService CXF学习(入门篇3):对象传递
- WebService CXF学习(入门篇4):整合Spring框架
- WebService CXF学习(入门篇2):HelloWorld示例
- WebService CXF学习(入门篇4):整合Spring框架
- WebService CXF学习(入门篇2):HelloWorld
- CXF发布webservice入门
- webservice简介&CXF入门
- webservice cxf -- 基于soap方式开发(一)
- Ajax Jquery 跨域问题 事件冒泡 浏览器默认操作
- Nginx反向代理、负载均衡、SSL支持功能
- boostrap需要的样式的地址
- 双波纹分离字体进度条
- 005_Java解析XML文件
- WebService学习入门(SOAP协议,ApacheCXF入门,基于CXF技术开发CRM服务器)
- Vue路由History mode模式中页面无法渲染的原因及解决
- phpcms v9 站群开发问题汇总
- JNI编程 入门级
- 矩阵A+B(矩阵相加)
- datatable excelHtml5 自定义列格式(百分比保留两位小数)
- 线性链表的查找插入删除算法
- Java InputStream从文件读取示例
- 北京新闻爬虫-final