CXF和Spring整合实现WebService服务端、客户端

来源:互联网 发布:知乎 博客园 csdn 编辑:程序博客网 时间:2024/05/20 04:31

一 、CXF和Spring整合实现WebService服务端、客户端(已测代码可以成功调用服务)

首先导入CXF3.1.13jar包 下载地址:http://download.csdn.net/download/porsche_gt3rs/10024998

1.服务端代码:

SEI接口:

@WebServicepublic interface OrderWS {@WebMethodpublic Order getOrderById(int id);}
SEI接口实现类:

@WebServicepublic class OrderWSImpl implements OrderWS {public OrderWSImpl() {System.out.println("===========OrderWSImpl()=========");}@Overridepublic Order getOrderById(int id) {System.out.println("Server getOrderById() 被调用了!" + id);return new Order(id, "跑车订单", 12000000);}}

服务端添加一个入拦截器验证客户端请求

public class serverInInterceptor extends AbstractPhaseInterceptor<SoapMessage>{public serverInInterceptor() {super(Phase.PRE_PROTOCOL);  //  准备协议化时拦截// TODO Auto-generated constructor stub}/* 请求头的消息: * <Envelope> * <head> * <ebring> * <name>xuhao</name> * <password>123456</password> * </ebring> * <ebring> * <name>xuhao</name> * <password>123456</password> * </ebring> * </head> * <body> * <sayHello> * <arg0>Tom</arg0> * </sayHello> * </body> * </Envelope> * */ @Overridepublic void handleMessage(SoapMessage msg) throws Fault {Header header = msg.getHeader(new QName("ebring"));if(header != null){  //  获取到请求偷头中的用户名和密码Element root = (Element) header.getObject();String name = root.getElementsByTagName("name").item(0).getTextContent();String password = root.getElementsByTagName("password").item(0).getTextContent();if("xuhao".equals(name) && "123456".equals(password)){System.out.println("server端通过拦截器...");return;}}//  没有通过System.out.println("server没有通过!");throw new Fault(new RuntimeException("请求需要一个正确的用户名和密码!"));}}

进行spring配置了,在eclipse的src文件夹下新建beans.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:p="http://www.springframework.org/schema/p"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:jaxws="http://cxf.apache.org/jaxws"    xsi:schemaLocation="        http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd         http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop.xsd         http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx.xsd        http://cxf.apache.org/jaxws        http://cxf.apache.org/schemas/jaxws.xsd">       <import resource="classpath:META-INF/cxf/cxf.xml" />              <jaxws:endpoint id="OrderWS"            implementor="impl.OrderWSImpl"            address="/orderWS">            <!--  添加服务器端的入拦截器 -->            <jaxws:inInterceptors>            <bean class="Interceptor.serverInInterceptor">            </bean>            </jaxws:inInterceptors>                   </jaxws:endpoint></beans>
Web.xml配置拦截所有请求让CXF框架处理

<?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_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>CXF_Spring_Server1</display-name>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>    <display-name>webservice_cxf_spring_server</display-name><!-- 加载 beans.xml --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:beans.xml</param-value></context-param><!-- 应用启动的一个监听 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 所有请求都会先经过 cxf 框架 --><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>/*</url-pattern></servlet-mapping></web-app>

2.客户端代码:

使用命令:wsimport -keep http://localhost:8080/CXF_Spring_Server1/orderWS 生成客户端代码

客户端添加出拦截器:

/** * 客户端出拦截器 * @author xuhao * */public class ClientOutInterceptor extends AbstractPhaseInterceptor<SoapMessage> {private String name;private String password;public ClientOutInterceptor(String name, String password) {super(Phase.PRE_PROTOCOL);  //  准备协议化时拦截this.name = name;this.password = password;System.out.println("客户端出拦截器被调用ClientOutInterceptor()...");}/* * <Envelope> * <head> * <ebring> * <name>xuhao</name> * <password>123456</password> * </ebring> * <ebring> * <name>xuhao</name> * <password>123456</password> * </ebring> * </head> * <body> * <sayHello> * <arg0>Tom</arg0> * </sayHello> * </body> * </Envelope> * */ @Overridepublic void handleMessage(SoapMessage msg) throws Fault{List<Header> headers = msg.getHeaders();/* * <head> <ebring>  <name>xuhao</name>  <password>123456</password>  </ebring> </head> * */DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = null;try {builder = factory.newDocumentBuilder();} catch (ParserConfigurationException e) {// TODO Auto-generated catch blocke.printStackTrace();}// 创建DocumentDocument doc = builder.newDocument();Element root = doc.createElement("ebring");Element ele_name = doc.createElement("name");Element ele2_password = doc.createElement("password");ele_name.setTextContent(name);ele2_password.setTextContent(password);root.appendChild(ele_name);root.appendChild(ele2_password);headers.add(new Header(new QName("ebring"), root));System.out.println("Client====>handleMessage()...");}}
配置客户端的client-beans.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"  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">          <jaxws:client id="orderClient"      serviceClass="ws.OrderWS"    address="http://localhost:8080/CXF_Spring_Server1/orderWS" ><!-- 请求WebService服务端的地址 -->        <!-- 添加出拦截配置 -->    <jaxws:outInterceptors>    <bean class="Interceptor.ClientOutInterceptor"><constructor-arg name="name" value="xuhao"/><constructor-arg name="password" value="123456"/>    </bean>    </jaxws:outInterceptors>    </jaxws:client>  </beans>  
测试代码

/** * 测试客户端 * @author xuhao * */public class TestClient {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("client-beans.xml");OrderWS orderWS = (OrderWS)context.getBean("orderClient");Order order = orderWS.getOrderById(1204);System.out.println("客户端接收到的返回值:" + order.toString());}}





阅读全文
0 0
原创粉丝点击