Cxf - Spring整合呼叫WebServices
一、新建Web工程
1、匯入Struts Core、Struts Spring、Spring Core、Spring Web和Cxf的相關jar包;通過Cxf的wsdl2java工具生成WS相關的程式碼
2、本例使用 Cxf-Spring整合暴露WebServices 中暴露的WS為呼叫目標
3、完成之後,介面如下:
二、新建Action和呼叫需要的Interceptor
public class UserBookAction extends ActionSupport {
private static final long serialVersionUID = 3826430837838869209L;
// 遠端Web Services物件的代理 (介面)
private UserInfoWs userInfoWs;
private List<Book> books;
@Override
public String execute() throws Exception {
User u = new User();
u.setName("Admin");
books = userInfoWs.getBookByUser(u);
return SUCCESS;
}
public UserInfoWs getUserInfoWs() {
return userInfoWs;
}
public void setUserInfoWs(UserInfoWs userInfoWs) {
this.userInfoWs = userInfoWs;
}
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
}
public class ClientAuthOutInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
private String name;
private String pwd;
public ClientAuthOutInterceptor(String name, String pwd) {
// 在訊息傳送之前呼叫
super(Phase.PREPARE_SEND);
this.name = name;
this.pwd = pwd;
}
public void handleMessage(SoapMessage message) throws Fault {
List<Header> headers = message.getHeaders();
Document doc = DOMUtils.createDocument();
Element element = doc.createElement("userInfo");
Element nameEle = doc.createElement("userName");
nameEle.setTextContent(name);
Element passEle = doc.createElement("userPass");
passEle.setTextContent(pwd);
element.appendChild(nameEle);
element.appendChild(passEle);
Header header = new Header(new QName("xilen"), element);
headers.add(header);
}
}
三、配置相關xml檔案
1、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<context-param>
<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>
</web-app>
2、struts.xml<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="userbook" class="com.xilen.view.UserBookAction">
<result>/userbook.jsp</result>
</action>
</package>
</struts>
3、applicationContext.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:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<!-- Action依賴的是遠端WebService的物件代理,所以serviceClass指向遠端物件代理介面;address即遠端Endpoint地址;id即Action中屬性名稱 -->
<jaxws:client id="userInfoWs" serviceClass="com.xilen.cxf.ws.UserInfoWs" address="http://127.0.0.1:8080/CxfSpringServer/cxf/userinfo">
<!-- 攔截器 -->
<jaxws:outInterceptors>
<bean class="com.xilen.inter.ClientAuthOutInterceptor" >
<constructor-arg value="CxfName"/>
<constructor-arg value="CxfPass"/>
</bean>
</jaxws:outInterceptors>
</jaxws:client>
</beans>
四、新建struts中配置的jsp檔案
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><title>User Book</title></head>
<body>
<table border="1">
<c:forEach items="${books}" var="book">
<tr><td width="200px;" align="center"><c:out value="${book.name}"></c:out></td></tr>
</c:forEach>
</table>
</body>
</html>
五、載入到Tomcat並啟動
呼叫成功!
六、資源下載
http://download.csdn.net/detail/u013379717/7220085
相關文章
- Cxf - Spring整合Cxf暴露WebServicesSpringWeb
- CXF--整合SpringSpring
- WebService之Spring+CXF整合示例WebSpring
- 基於Maven的Spring整合CXFMavenSpring
- Spring3.2 + cxf1.7.3整合Spring
- c#呼叫webservicesC#Web
- cxf ServerFactoryBean 生成基於soap1.2的WebServicesServerBeanWeb
- 使用 CXF 整合 Spring 開發 Web ServiceSpringWeb
- Apache CXF 與 Spring 整合簡單例子ApacheSpring單例
- CXF開發及與Spring整合開發Spring
- 使用CXF與Spring整合實現RESTFul WebServiceSpringRESTWeb
- Spring整合CXF,釋出RSETful 風格WebServiceSpringWeb
- Spring和cxf3的整合,以maven的方式SpringMaven
- spring整合mongo及呼叫SpringGo
- Java呼叫取得天氣預報WebServicesJavaWeb
- SpringBoot整合Apache-CXF實踐Spring BootApache
- CXF呼叫方式——使用wsdl2java(Windwos下)Java
- CXF入門教程(5) -- webService非同步呼叫模式Web非同步模式
- spring mina整合(客戶端進行呼叫)Spring客戶端
- dubbo升級spring4與cxfSpring
- 實戰CXF呼叫Webxml天氣預報服務WebXML
- axis WebServices 完美呼叫天氣預報,查詢、顯示 程式碼!Web
- SOAP及WebServicesWeb
- maven+CXF+Spring+tomcat 開發webserviceMavenSpringTomcatWeb
- 使用瀏覽器位址列呼叫CXF Webservice的寫法瀏覽器Web
- 開發:隨筆記錄之 CXF 動態呼叫Webservice筆記Web
- CXF 呼叫方式——動態建立客戶端(呼叫穩定版本號為2.7.18)客戶端
- Spring Boot 整合 Fisco Bcos(部署、呼叫區塊鏈合約)Spring Boot區塊鏈
- java 上傳圖片 cxf,servlet,spring 標準方式JavaServletSpring
- CXF+Spring+JAXB+Json構建Restful服務SpringJSONREST
- 用cxf編寫基於spring的webservice之下篇SpringWeb
- WebServices in Oracle 11gWebOracle
- Spring RMI呼叫Spring
- Spring Boot整合Spring BatchSpring BootBAT
- Spring Boot整合Spring SecuritySpring Boot
- Spring Boot整合Spring AopSpring Boot
- Spring 整合 MyBatisSpringMyBatis
- mybatis整合springMyBatisSpring