Soa(面向服务的架构)
Service1,Service2,Service3…——所有组件都是“即插即用”的。
IBM提倡的soa架构:希望以“组装电脑”的方式来开发软件。
1. 各种提供服务的组件。(web service)
2. 企业服务总线(enterprise service bus,ESB)
CXF可称得上是SOA架构
Cxf内置了一个jetty web服务器
使用cxf开发web service服务器端:
Cxf环境变量设置:
Classpath:F:\apache-cxf-2.1.2\lib
CXF_HOME = "CXF安装路径". 例如:F:\apache-cxf-2.1.2
在PATH中添加%CXF_HOME%/bin
(1) 开发一个web service业务接口
该接口要用@webservice注解
package org.weir.cxf.ws; import javax.jws.WebService; @WebService publicinterface Helloworld { String sayHi(String name); } |
(2) 开发一个web service实现类
也需要@webservice注解
package org.weir.cxf.ws.impl; import javax.jws.WebService; import org.weir.cxf.ws.Helloworld; @WebService(endpointInterface="org.weir.cxf.ws.Helloworld",serviceName="HelloworldWs") publicclass HelloworldWs implements Helloworld { @Override public String sayHi(String name) { return name+"HI"; } } |
(3)发布webservice
package lee; import javax.xml.ws.Endpoint; import org.weir.cxf.ws.Helloworld; import org.weir.cxf.ws.impl.HelloworldWs; publicclass ServerMain { publicstaticvoid main(String[] args) { Helloworld hw = new HelloworldWs(); //发布web service Endpoint.publish("http://172.168.1.172/weir", hw); System.out.println("Ko"); } }
|
运行main方法不要结束
http://172.168.1.172/weir?wsdl
会出现:
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.ws.cxf.weir.org/"xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://ws.cxf.weir.org/" name="HelloworldWs"targetNamespace="http://impl.ws.cxf.weir.org/">
<wsdl:import location="http://172.168.1.172/weir?wsdl=Helloworld.wsdl" namespace="http://ws.cxf.weir.org/"></wsdl:import>
<wsdl:binding name="HelloworldWsSoapBinding" type="ns1:Helloworld">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHi">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="sayHi">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sayHiResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloworldWs">
<wsdl:port binding="tns:HelloworldWsSoapBinding" name="HelloworldWsPort">
<soap:address location="http://172.168.1.172/weir"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
客户端
(1) 调用cxf提供的wsdl2java工具,根据wsdl文档生成相应的java代码。
(任何语言实现了web service,都需要提供、并暴露wsdl文档)
WSDL——web service definition language
用cmd进入客户端的src目录:
运行 wsdl2java http://172.168.1.172/weir?wsdl
wsdl2java –encoding utf-8 http://172.168.1.172/weir?wsdl
(2) 找到wsdl2java所生成类中,一个继承了service的类。
该类的实例可当成工厂来使用
(3)调用service子类的实例的getxxxPort方法,返回远程web service的代理。
package lee; import org.weir.cxf.ws.Helloworld; import org.weir.cxf.ws.impl.HelloworldWs; publicclass ClientMain { publicstaticvoid main(String[] args) { HelloworldWs factory = new HelloworldWs(); Helloworld hw = factory.getHelloworldWsPort(); System.out.println(hw.sayHi("weir")); } }
|
形参、返回值
1. 当形参、返回值的类型是string、基本数据类型时,cxf可以处理
2. 当形参、返回值的类型是javabean式的符合类、list集合、数组等时,cxf可以处理
3. 还有一些像map、非javabean式的复合类,cxf是处理不了的
Web service的三个技术基础:
1. wsdl web service definition language ——web service定义语言
2. soap
3. uddl
一次web service的调用——其实并不是方法调用,而是发送soap消息(即xml文档片段)
阅读技巧从下往上阅读
调用一次web service的本质:
1. 客户端把调用方法参数,转换xml文档片段——该文档片段必须符合wsdl定义的格式
2. 通过网络,把xml文档片段从传给服务器
3. 服务器接受到xml文档片段
4. 服务器解析xml文档片段,提取其中的数据。并把数据转换调用webservice所需要的参数值
5. 服务器执行方法
6. 把执行方法得到的返回值,再次转换生成为xml文档片段(soap消息)——该文档片段必须符合wsdl定义的格式
7. 通过网络、把xml文档片段传给客户端
8. 客户端接收到xml文档片段
9. 客户端解析xml文档片段,提取其中的数据。并把数据转换调用webservice所需要的返回值
从上面调用本质来看,要一个语言支持web service
唯一的要求是:该预言支持xml文档解析、生成、支持网络传输。
在cxf开发中,如果遇到cxf无法处理的类型,就需要程序员自行处理
(1) 使用@XmlJavaTypeAdapter注解修饰
使用annotation时,通过value属性指定一个转换器
(2)实现自己的转换器。
Web service 如何进行权限控制?
解决思路:服务器端要求input消息总是携带有用户名密码信息
如果没有直接拒绝调用
Cxf引入拦截器
服务器端添加拦截器
wsdl2java –encoding utf-8 http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
注释注解:@WSDLDocumentation(value="注释")
Spring 与cxf 整合
1.spring发布SOAP方式web服务(jax-ws)
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>ch03_1</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans-server.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</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>
beans-server.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:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">
<!-- 导入 CXF 扩充XML标记库,用于在Spring启用WebService标记 -->
<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" />
<!-- CXF 提供的内置拦截器 -->
<bean id="inLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" />
<bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
<!-- 配置方案一 使用JAX-WS标准配置jaxws:endpoint,发布服务 -->
<!-- id:spring bean标识,implementor:服务实现类,address:服务发布路径 -->
<jaxws:endpoint id="merchant_1"
implementor="com.cxfdemo.server.service.impl.IMerchantServiceImpl" address="/m_1">
<!-- 可选配置入口拦截器 -->
<jaxws:inInterceptors>
<ref bean="inLoggingInterceptor" />
</jaxws:inInterceptors>
<!-- 可选配置出口拦截器 -->
<jaxws:outInterceptors>
<ref bean="outLoggingInterceptor" />
</jaxws:outInterceptors>
</jaxws:endpoint>
<!-- 配置方案二 使用JAX-WS标准配置jaxws:endpoint,发布服务 -->
<!-- id:spring bean标识,serviceClass:服务实现接口,address:服务发布路径 -->
<jaxws:server id="merchant_2" serviceClass="com.cxfdemo.server.service.IMerchantService" address="/m_2">
<!-- 注入:服务实现类 -->
<jaxws:serviceBean>
<ref bean="merchantService"/>
</jaxws:serviceBean>
<!-- 可选配置入口拦截器 -->
<jaxws:inInterceptors>
<ref bean="inLoggingInterceptor" />
</jaxws:inInterceptors>
<!-- 可选配置出口拦截器 -->
<jaxws:outInterceptors>
<ref bean="outLoggingInterceptor" />
</jaxws:outInterceptors>
</jaxws:server>
<!-- 服务实现类 -->
<bean id="merchantService" class="com.cxfdemo.server.service.impl.IMerchantServiceImpl" />
</beans>
<?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:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">
<!-- 导入 CXF 扩充XML标记库,用于在Spring启用WebService标记 -->
<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" />
<!-- spring管理ws client -->
<!-- address:ws服务URL serverClass:客户端服务接口类(非实现类)交由Spring生成客户端代理对象 -->
<jaxws:client id="merchantServiceClient" address="http://localhost:8088/ch03_1/ws/m_2"
serviceClass="com.cxfdemo.server.client.IMerchantService"/>
</beans>
2.Rest
<?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:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">
<!-- 导入 CXF 扩充XML标记库,用于在Spring启用WebService标记 -->
<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" />
<!-- CXF 提供的内置拦截器 -->
<bean id="inLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" />
<bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
<jaxrs:server id="merchantrs" address="/">
<jaxrs:serviceBeans>
<ref bean="merchantService"/>
</jaxrs:serviceBeans>
<jaxrs:extensionMappings>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</jaxrs:extensionMappings>
<!-- 可选配置入口拦截器 -->
<jaxrs:inInterceptors>
<ref bean="inLoggingInterceptor" />
</jaxrs:inInterceptors>
<!-- 可选配置出口拦截器 -->
<jaxrs:outInterceptors>
<ref bean="outLoggingInterceptor" />
</jaxrs:outInterceptors>
</jaxrs:server>
<!-- 服务实现类 -->
<bean id="merchantService" class="com.cxfdemo.server.service.impl.IMerchantServiceImpl" />
</beans>
软件技术开发分类:
1.本地调用(LPC)local procedure call
2.远程调用(RPC)remote procedure call
2.1 远程方法调用(RMI)remote method invocation
2.2 通用对象请求代理体系结构(CORBA)common object request broker architecture
IDL:接口描述语言 interface descripton language
2.3 互联网内部对象请求代理协议(RMI-IIOP)internet inner-object protocat
2.4 组件对象模型(COM、COM+)component object model
2.5 web service
协议:
http(超文本传输协议)
SOAP(简单对象访问协议) simple object access protocol
Soa service oriented architecture
ESB(企业服务总线) entertainment service bus
Sca service component architechture
相关推荐
webservice cxf示例工程集成spring,包含所有jar包和测试类。使用的是myeclipse8.5
cxf+spring 实现websevice的maven项目,此例子简单,易懂,对刚接触cxf webservice的非常有帮助!
webService CXF不依赖Spring集成,包含客户端、服务端代码。使用3.1.7版的CXF包。demo可以直接运行
CXF WebService整合Spring示例工程代码demo可以直接导入eclipse。参照网页http://www.cnblogs.com/hoojo/archive/2011/03/30/1999563.html 完成的webService服务提供。 大致步骤: 1.引入cxf和其他需要的jar包,(本...
NULL 博文链接:https://chilongxph.iteye.com/blog/510707
CXF+Spring+Hibernate实现WebService,实现的方法比较简单,只有一个增加的方法,没有用到Struts 有服务端代码和客户端测试代码
使用spring集成cxf,在两个web project里发布及调用webservice server端使用spring+springmvc+mybatis+cxf,client端使用struts2+spring+hibernate+cxf 两个工程均为myeclipse project,包含所有除myeclipse自带以外...
SpringBoot WebService cxf接口发布以及logbok日志集成, 希望对学习的小伙伴们有帮助
cxf集成Spring的restful WebServ ice接口,Demo的完整代码!
webService实现 cxf2.7.5 与 spring3.0集成 完整流程,内涵全部jar ,下载集合运行
使用最新的CXF2.7和Spring3的 Java WebService 集成用例 应用Spring Annotation
Cxf集成Spring的WebService接口的配置文档
readme下为开发文档。 1.集成 spring 2.集成 WSS4J 3.mtom 功能示例 4.基本数据类型service接口 示例 5.复杂数据类型service接口 示例
cxf与spring整合webservice所需jar
这个是cxf 集成spring 的webservice的例子 供大家参考
CXF集成Spring发布WebService代码实例
基于spring和CXF发布webservice,包含java本地发布和调用及web发布和调用。
cxf集成Spring的restful WebService接口以Json形式表现的完整代码,适合新入门在学WebService的小伙伴们参考,希望对你们有帮助!
Spring集成Cxf调用WebServices
CXF简化了构造,集成,面向服务架构(SOA)业务组件与技术的灵活复用。在CXF中,Service使用WSDL标准定义并能够使用各种不同的消息格式(或binding)和网络协议(transports)包括SOAP、XML(通过HTTP或JMS)进行访问。CXF...