一引润、環(huán)境搭建
- 這里只是初步整合巩趁,我們直接將
CXF
中的lib
目錄中的所有jar
包全部加入(除去jetty
相關(guān)包、geronimo-servlet_3.0_spec-1.0.jar
)淳附。 - 將之前工程中的包拷貝到本工程(
cxf_spring
)中议慰,
分別是org.fkjava.cxf.ws.domain、org.fkjava.cxf.ws.service奴曙、org.fkjava.cxf.ws.service.impl别凹、org.fkjava.cxf.ws、org.fkjava.cxf.ws.impl洽糟、org.fkjava.cxf.ws.util炉菲、org.fkjava.cxf.ws.server
。 - 相關(guān)的配置文件
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- 使用一個(gè)監(jiān)聽器加載spring容器坤溃,保證web應(yīng)用啟動(dòng)時(shí)加載spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 下面的配置表明所有來自/myService/*的請(qǐng)求都交給CXFServlet處理 -->
<servlet>
<servlet-name>cxf</servlet-name>
<!-- 下面這個(gè)類在API文檔中是查不到的 -->
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/myService/*</url-pattern>
</servlet-mapping>
</web-app>
說明:這里主要配置了spring
的配置文件地址拍霜,spring
監(jiān)聽器、CXF
核心控制器薪介。
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:jaxws="http://cxf.apache.org/jaxws"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!-- web應(yīng)用的類加載路徑有兩類:
1.WEB-INF/classes目錄沉御。2.WEB-INF/lib目錄下,兩者的唯一區(qū)別是前者是散的class文件昭灵,后者是打成jar包的class文件
-->
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<bean id="userService" class="org.fkjava.cxf.impl.service.impl.UserServiceImpl"/>
</beans>
說明:
- 這里我們需要將
CXF
相關(guān)的配置文件加入進(jìn)去吠裆,同時(shí)配置相關(guān)的業(yè)務(wù)類。 - 在
spring
配置文件(applicationContext.xml
)中注入CXF
提供的schema烂完、xml
配置试疙。- 對(duì)于
schema
的配置:
在beans
標(biāo)簽中加入xmlns:jaxws=http://cxf.apache.org/jaxws
然后在xsi:schemaLocation
中加入http://cxf.apache.org/jaxws
(命名空間)http://cxf.apache.org/schemas/jaxws.xsd
(物理路徑)。 - 對(duì)于
xml
的配置:
web應(yīng)用的類加載路徑有兩類:1.WEB-INF/classes
目錄抠蚣。2.WEB-INF/lib
目錄下祝旷,兩者的唯一區(qū)別是前者是散的class
文件,后者是打成jar
包的class
文件。
<import resource="classpath:META-INF/cxf/cxf.xml"/>
其實(shí)導(dǎo)入的cxf-servlet.xml
和cxf.extension-soap.xml
一般不需要怀跛,在之后的版本中可以不需要導(dǎo)入了距贷,一般只是為了兼容性才會(huì)導(dǎo)入。
- 對(duì)于
二吻谋、進(jìn)行開發(fā)
2.1 暴露WebService
在spring
的配置文件中使用jaxws:endpoint
元素來暴露WebService
忠蝗,這里有兩種方式:
<1>直接給定服務(wù)器提供者的類名(不太好用)
<jaxws:endpoint
implementor="org.fkjava.cxf.ws.impl.HelloWorldWs"
address="/fkjava">
</jaxws:endpoint>
然后我們就可以將工程部署到tomcat
中,在瀏覽器中使用
地址http://localhost:8080/cxf_spring/myService
訪問我們的WebService
工程漓拾。
- 新建一個(gè)
java
工程進(jìn)行測(cè)試(工程Call_CXFSpring
)
首先還是使用命令:wsdl2java http://localhost:8080/cxf_spring/myService/fkjava?wsdl
將相關(guān)文件考出來阁最。之后刷新工程,在工程中新建一個(gè)類MyClient.java
訪問WebService
即可骇两。
MyClient.java
package org.fkjava.cxf.ws.client;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.fkjava.cxf.ws.auth.AddHeaderInterceptor;
import org.fkjava.cxf.ws.Entry;
import org.fkjava.cxf.ws.HelloWorld;
import org.fkjava.cxf.ws.StringCat;
import org.fkjava.cxf.ws.impl.HelloWorldWs;
public class MyClient {
public static void main(String[] args) {
HelloWorldWs factory = new HelloWorldWs();
HelloWorld hw = factory.getHelloWorldWsPort();
System.out.println(hw.sayHi("張三"));
StringCat sc = hw.getAllCats();
for(Entry entry : sc.getEntries()){
System.out.println(entry.getKey() + ":" + entry.getValue().getName());
}
}
}
注意:myService
指定我們的服務(wù)名字(在web.xml
中配置)速种,而fkjava
指定服務(wù)提供者的地址(在applicationContext.xml
中配置)。同時(shí)這種集成不好低千,因?yàn)閷⒌刂穼懰懒伺湔螅瑳]有使用spring
的注入方式。
我們看HelloWorldWs.java
這個(gè)服務(wù)類:
package org.fkjava.cxf.ws.impl;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.jws.WebService;
import org.fkjava.cxf.ws.HelloWorld;
import org.fkjava.cxf.ws.domain.Cat;
import org.fkjava.cxf.ws.domain.User;
import org.fkjava.cxf.ws.service.UserService;
import org.fkjava.cxf.ws.service.impl.UserServiceImpl;
@WebService(endpointInterface = "org.fkjava.cxf.ws.HelloWorld", serviceName = "HelloWorldWs")
public class HelloWorldWs implements HelloWorld {
@Override
public String sayHi(String name) {
return name + "您好!" + "現(xiàn)在的時(shí)間是: " + new Date();
}
@Override
public List<Cat> getCatsByUser(User user) {
//這里我們需要手工new示血,如果交給spring管理可以將對(duì)象注入進(jìn)來
UserService service = new UserServiceImpl();
return service.getCatByUser(user);
}
//新增方法
@Override
public Map<String, Cat> getAllCats() {
UserService service = new UserServiceImpl();
return service.getAllCats();
}
}
可以看到其中的實(shí)際業(yè)務(wù)處理類還是使用的手工方法進(jìn)行實(shí)例化闸餐,沒有使用spring
的注入方式。
<2>使用注入方式
在spring
的配置文件中改變暴露WebService
的方式:
<bean id="helloWorldWs" class="org.fkjava.cxf.ws.impl.HelloWorldWs">
<property name="userService" ref="userService"></property>
</bean>
<jaxws:endpoint
implementor="#helloWorldWs"
address="/fkjava">
</jaxws:endpoint>
說明:加#
號(hào)是為了讓服務(wù)器知道這個(gè)名字不是一個(gè)類 矾芙,而只是一個(gè)id
舍沙。
然后改造HelloWorldWs.java
服務(wù)類:
package org.fkjava.cxf.ws.impl;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.jws.WebService;
import org.fkjava.cxf.ws.HelloWorld;
import org.fkjava.cxf.ws.domain.Cat;
import org.fkjava.cxf.ws.domain.User;
import org.fkjava.cxf.ws.service.UserService;
import org.fkjava.cxf.ws.service.impl.UserServiceImpl;
@WebService(endpointInterface = "org.fkjava.cxf.ws.HelloWorld", serviceName = "HelloWorldWs")
public class HelloWorldWs implements HelloWorld {
private UserService userService;
@Override
public String sayHi(String name) {
return name + "您好!" + "現(xiàn)在的時(shí)間是: " + new Date();
}
@Override
public List<Cat> getCatsByUser(User user) {
return userService.getCatByUser(user);
}
//新增方法
@Override
public Map<String, Cat> getAllCats() {
return userService.getAllCats();
}
public void setUserService(UserService userService) {
this.userService = userService;
}
}
然后我們?cè)俅问褂弥暗牡刂愤M(jìn)行訪問,發(fā)現(xiàn)效果還是一樣的剔宪。
2.2 添加攔截器
在上面配置的基礎(chǔ)上進(jìn)行添加拂铡,在applicationContext.xml
中:
<jaxws:endpoint
implementor="#helloWorldWs"
address="/fkjava">
<!-- 如果要添加out攔截器,則使用 jaxws:outInterceptors標(biāo)簽定義 -->
<jaxws:inInterceptors>
<!--這里是臨時(shí)定義的一個(gè)嵌套bean葱绒,這是cxf提供的攔截器-->
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
<!--這里是引用容器中已有的一個(gè)bean-->
<!--<ref bean="anotherInterceptor"/>-->
<!--這里將我們自己的權(quán)限控制器引進(jìn)來-->
<bean class="org.fkjava.cxf.ws.auth.AuthInterceptor"/>
</jaxws:inInterceptors>
</jaxws:endpoint>
此時(shí)感帅,和以前一樣我們需要在客戶端(工程Call_CXFSpring
)那邊加上攔截器,添加上用戶名和密碼的頭地淀,然后對(duì)服務(wù)器進(jìn)行訪問失球。以此來進(jìn)行驗(yàn)證“锘伲客戶端中我們運(yùn)行類:
MyClient.java
package org.fkjava.cxf.ws.client;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.fkjava.cxf.ws.auth.AddHeaderInterceptor;
import org.fkjava.cxf.ws.Entry;
import org.fkjava.cxf.ws.HelloWorld;
import org.fkjava.cxf.ws.StringCat;
import org.fkjava.cxf.ws.impl.HelloWorldWs;
public class MyClient {
public static void main(String[] args) {
HelloWorldWs factory = new HelloWorldWs();
HelloWorld hw = factory.getHelloWorldWsPort();
Client client = ClientProxy.getClient(hw) ;//調(diào)用此方法实苞,以遠(yuǎn)程WebService的代理為參數(shù)
client.getOutInterceptors().add(new AddHeaderInterceptor("大熊", "111"));
System.out.println(hw.sayHi("張三"));
StringCat sc = hw.getAllCats();
for(Entry entry : sc.getEntries()){
System.out.println(entry.getKey() + ":" + entry.getValue().getName());
}
}
}
訪問服務(wù)端。
三烈疚、另一種整合方式
傳統(tǒng)的SSH
項(xiàng)目中黔牵,一般在本地是有相關(guān)的數(shù)據(jù)的,但是有一種場(chǎng)景是我們需要調(diào)用別人的數(shù)據(jù)爷肝,但是別人的數(shù)據(jù)不可能讓我們進(jìn)行修改等操作猾浦,那么別人只會(huì)暴露一個(gè)WebService
陆错,而我們就需要去調(diào)用別人的WebService
服務(wù)器。我們?cè)谠L問別人時(shí)金赦,只能得到WSDL
文檔音瓷。此時(shí)我們的項(xiàng)目中就不需要業(yè)務(wù)邏輯組件了,直接去調(diào)用別人的WebService
的一個(gè)代理即可夹抗。
3.1 環(huán)境
首先我們拷貝上面的工程绳慎,改名為cxf_springClient
,然后需要增加struts2
的相關(guān)jar
包:
asm-3.3.jar(會(huì)和CXF包中的重復(fù)兔朦,刪掉低版本)偷线、
asm-commons-3.3.jar磨确、
asm-tree-3.3.jar沽甥、
commons-lang3-3.2.jar、
commons-fileupload-1.3.1.jar乏奥、
commons-io-2.2.jar摆舟、
commons-lang-2.4.jar(會(huì)和CXF中的重復(fù),刪掉低版本)邓了、
freemarker-2.3.22.jar恨诱、
javassist-3.11.0.GA.jar、
ognl-3.0.6.jar骗炉、
struts2-core-2.3.24.jar照宝、
xworks-core-2.3.24.jar。
strut22-spring-plugin-2.3.24.1.jar(用于和spring集成的插件)
將原有的一些包刪掉句葵,留下權(quán)限包org.fkjava.cxf.ws.auth
厕鹃。然后我們啟動(dòng)工程Auth_Server
提供遠(yuǎn)程服務(wù)。在web
工程中生成相應(yīng)的包及程序乍丈。
3.2 配置
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- struts2 -->
<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>/*</url-pattern>
</filter-mapping>
<!-- 使用一個(gè)監(jiān)聽器加載spring容器剂碴,保證web應(yīng)用啟動(dòng)時(shí)加載spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
說明:這里我們不再需要CXF
的servlet
了,因?yàn)椴恍枰獦I(yè)務(wù)類了轻专,我們直接調(diào)用遠(yuǎn)程業(yè)務(wù)即可忆矛。同時(shí)配置struts2
的核心控制器。
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:jaxws="http://cxf.apache.org/jaxws"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<!-- action依賴的是遠(yuǎn)程WebService的代理请垛,所以這里不能配置本地的業(yè)務(wù)邏輯組件催训,而是需要
配置遠(yuǎn)程WebService代理,為了保證該WebService代理對(duì)象可以自動(dòng)裝配給action宗收,因此應(yīng)該保證該id的值與action中的setter方法名對(duì)應(yīng) -->
<jaxws:client id="hw"
serviceClass="org.fkjava.cxf.ws.HelloWorld"
address="http://localhost:8888/myService">
<jaxws:outInterceptors>
<bean class="org.fkjava.cxf.ws.auth.AddHeaderInterceptor">
<constructor-arg value="大熊"/>
<constructor-arg value="111"/>
</bean>
</jaxws:outInterceptors>
</jaxws:client>
</beans>
說明:這里我們同樣不需要業(yè)務(wù)類了瞳腌,但是需要配置遠(yuǎn)程業(yè)務(wù)代理類(serviceClass
)。同時(shí)注意:不要將端口設(shè)置為8080镜雨,會(huì)沖突嫂侍《酰可以看到我們使用serviceClass
將接口配置進(jìn)去,而這里的id
不能隨便寫挑宠,要和action
中定義的接口名一致菲盾。由于遠(yuǎn)程服務(wù)器設(shè)置了權(quán)限攔截,所以這里使用過濾器將用戶名和密碼設(shè)置到wsdl
文檔的頭中各淀。然后在這個(gè)類中我們需要使用execute
方法去獲取相應(yīng)的數(shù)據(jù)懒鉴。
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="listCats" class="org.fkjava.cxf.ws.action.ListCatsAction">
<result name="success">
/WEB-INF/content/listCats.jsp
</result>
</action>
</package>
</struts>
3.3 Action
ListCatsAction .java
package org.fkjava.cxf.ws.action;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.fkjava.cxf.ws.Entry;
import org.fkjava.cxf.ws.Cat;
import org.fkjava.cxf.ws.HelloWorld;
import org.fkjava.cxf.ws.StringCat;
import com.opensymphony.xwork2.ActionSupport;
public class ListCatsAction extends ActionSupport {
private HelloWorld hw ;
private Map<String, Cat> cats;
public HelloWorld getHw() {
return hw;
}
public void setHw(HelloWorld hw) {
this.hw = hw;
}
public Map<String, Cat> getCats() {
return cats;
}
public void setCats(Map<String, Cat> cats) {
this.cats = cats;
}
//此處的action調(diào)用的是遠(yuǎn)程WebService方法
public String execute(){
StringCat sc = hw.getAllCats();
Map<String, Cat> map = new HashMap<String, Cat>();
for(Entry entry : sc.getEntries()){
map.put(entry.getKey(), entry.getValue());
}
setCats(map);
return SUCCESS;
}
}
說明:此時(shí)我們就可以使用地址http://localhost:8080/cxf_springClient/listCats
進(jìn)行訪問了,當(dāng)然會(huì)報(bào)找不到jsp
的錯(cuò)誤碎浇。
3.4 listCats.jsp
content/listCats.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>所有的貓</title>
</head>
<body>
<ul>
<s:iterator var="entry" value="cats" >
<li>${entry.key }-->${entry.value.name }-->${entry.value.color }</li>
</s:iterator>
</ul>
</body>
</html>
說明:此時(shí)訪問就可以看到
這樣就表示成功临谱。