CXF
CXF簡介
CXF是一個開源的WebService框架。Apache CXF = Celtix + XFire,開始叫 Apache CeltiXfire,後來更名爲 Apache CXF 了,以下簡稱爲 CXF。CXF 繼承了 Celtix 和 XFire 兩大開源項目的精華,提供了對 JAX-WS 全面的支持,並且提供了多種 Binding 、DataBinding、Transport 以及各種 Format 的支持,並且可以根據實際項目的需要,採用代碼優先(Code First)或者 WSDL 優先(WSDL First)來輕鬆地實現 Web Services 的發佈和使用。
支持多種標準
- 支持 JAX-WS、 JAX-WSA、JSR-181 和 SAAJ;
- 支持 SOAP 1.1、1.2、WS-I BasicProfile、WS-Security、WS-Addressing、WS-RM 和 WS-Policy;
- 支持 WSDL 1.1 、2.0;
- 支持 MTOM;
它支持多種協議,比如:SOAP1.1,1,2、XML/HTTP、RESTful HTTP 或者 CORBA。CORBA(Common Object Request Broker Architecture公共對象請求代理體系結構,早期語言使用的WS。C,c++,C#)
Cxf是基於SOA總線結構,依靠spring完成模塊的集成,實現SOA方式。
靈活的部署:可以運行有Tomcat,Jboss,Jetty(內置),weblogic上面。
CXF入門案例
我們還以昨天的天氣服務爲案例來看一下CXF的開發過程。
服務端的實現
1.創建一個空的java項目,創建一個lib目錄,將所有jar包放入lib目錄
然後爲工程引入jar包,選擇build path,然後Add JARS,只用選擇cxf-manifest.jar即可。
2.創建一個SEI接口,需要在接口上添加@WebService註解
@WebService
public interface WeatherInterface {
public String queryWeather(String cityName);
}
3.創建SEI接口實現類
public class WeatherInterfaceImpl implements WeatherInterface {
public String queryWeather(String cityName) {
if("河南".equals(cityName)) {
return "熱爆炸";
}else {
return "冰雹";
}
}
}
4.發佈服務
public class WeatherServer {
public static void main(String[] args) {
//創建服務工廠Bean
JaxWsServerFactoryBean jaxWsServerFactoryBean=new JaxWsServerFactoryBean();
//設置服務接口
jaxWsServerFactoryBean.setServiceClass(WeatherInterface.class);
//設置服務實現類
jaxWsServerFactoryBean.setServiceBean(new WeatherInterfaceImpl());
//設置服務地址
jaxWsServerFactoryBean.setAddress("http://127.0.0.1:12345/weather");
//創建服務
jaxWsServerFactoryBean.create();
}
}
5.訪問服務的wsdl文件地址,看服務是否發佈成功
http://127.0.0.1:12345/weather?wsdl
發佈SOAP1.2的服務端
SOAP分爲1.1版本和1.2版本。JDK1.6並不支持1.2,我們可以通過CXF來發布SOAP1.2的服務端。
只需要在接口上添加註解 @BindingType(SOAPBinding.SOAP12HTTP_BINDING)。然後重新發布服務即可
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;
@WebService
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public interface WeatherInterface {
public String queryWeather(String cityName);
}
客戶端的實現
Wsdl2java命令是CXF提供的生成客戶端的工具,他和wsimport類似,可以根據WSDL生成客戶端代碼
Wsdl2java常用參數:
-d,指定輸出目錄
-p,指定包名,如果不指定該參數,默認包名是WSDL的命名空間的倒序
Wsdl2java支持SOAP1.1和SOAP1.2
1.我們先創建一個客戶端項目,然後引入jar包,和上面一樣,使用Add JARS選擇cxf-manifest.jar即可
然後使用工具生成客戶端
wsdl2java -p com.cad.cxf -d . http://127.0.0.1:12345/weather?wsdl
2.創建客戶端
public class WeatherClient {
public static void main(String[] args) {
JaxWsProxyFactoryBean jaxWsProxyFactoryBean=new JaxWsProxyFactoryBean();
//設置服務接口
jaxWsProxyFactoryBean.setServiceClass(WeatherInterface.class);
//設置服務地址
jaxWsProxyFactoryBean.setAddress("http://127.0.0.1:12345/weather");
//獲取服務接口實例
WeatherInterface weatherInterface=(WeatherInterface) jaxWsProxyFactoryBean.create();
//調用方法
String message=weatherInterface.queryWeather("河南");
System.out.println(message);
}
}
CXF+Spring整合發佈SOAP模式的服務
服務端的實現
1.創建WEB項目,導入jar包
2.創建SEI接口
@WebService
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public interface WeatherInterface {
public String queryWeather(String cityName);
}
3.創建SEI實現類
public class WeatherInterfaceImpl implements WeatherInterface {
public String queryWeather(String cityName) {
if("河南".equals(cityName)) {
return "熱爆炸";
}else {
return "冰雹";
}
}
}
4.配置spring配置文件,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:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<!--jaxws:server發佈SOAP協議的服務 ,對JaxWsServerFactoryBean類封裝-->
<!--serviceClass屬性是服務接口,address代表地址,因爲我們是web服務,不需要輸入ip。serviceBean是服務實現類-->
<jaxws:server serviceClass="com.cad.cxf.WeatherInterface" address="/weather">
<jaxws:serviceBean>
<ref bean="weatherInterfaceImpl"/>
</jaxws:serviceBean>
</jaxws:server>
<bean name="weatherInterfaceImpl" class="com.cad.cxf.WeatherInterfaceImpl"></bean>
</beans>
5.配置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>CXFSpringDemo</display-name>
//配置Tomcat啓動時加載Spring配置文件
<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>
//配置CXF提供的Servlet
<servlet>
<servlet-name>CXF</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXF</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>
6.部署到Tomcat下,發佈服務,並訪問
http://localhost:8080/CXFSpringDemo/ws/weather?wsdl
客戶端的實現
1.創建項目,導入jar包,生成客戶端
wsdl2java -p com.cad.cxf -d . http://localhost:8080/CXFSpringDemo/ws/weather?wsdl
2.配置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:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<!-- <jaxws:client實現客戶端 ,對JaxWsProxyFactoryBean類封裝-->
<!-- address是服務地址,servicelass是服務接口,返回服務實現類-->
<jaxws:client id="weatherClient" address="http://localhost:8080/CXFSpringDemo/ws/weather" serviceClass="com.cad.cxf.WeatherInterface"/>
</beans>
3.通過Spring容器獲取服務實現類,調用方法
public class WeatherClient {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
WeatherInterface weatherInterface = (WeatherInterface) context.getBean("weatherClient");
String message=weatherInterface.queryWeather("河南");
System.out.println(message);
}
}
CXF發佈REST模式的服務
REST即表述性狀態傳遞(英文:Representational State Transfer,簡稱REST),是一種軟件架構風格。
因爲REST模式的Web服務與複雜的SOAP和XML-RPC對比來講明顯的更加簡潔,越來越多的web服務開始採用REST風格設計和實現rest服務採用HTTP 做傳輸協議,REST 對於HTTP 的利用實現精確的資源定位。
例如:
非rest方式:http://ip:port/queryUser.action?userType=student&id=001
Rest方式:http://ip:port/user/student/query/001
1.創建一個項目,導入CXF jar包
2.創建一個實體類 Student
@XmlRootElement(name="student")可以實現XML和對象之間的轉換,name屬性指定根元素
@XmlRootElement(name="student")
public class Student {
private int id;
private String name;
private Date birthday;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
3.創建SEI接口
@WebService
//@Path("/student")就是指定訪問該接口的路徑
@Path("/Student")
public interface StudentInterface {
//指定請求方式,如果服務端發佈的時候指定的是GET(POST),那麼客戶端訪問時必須使用GET(POST
@GET
//指定服務數據類型,可以是XML,json等數據類型
@Produces(MediaType.APPLICATION_XML)
//@Path("/query/{id}")指定該方法的路徑,“{id}”指參數,多個參數,以“/”隔開,放到“{}”中
@Path("/query/{id}")
public Student queryStudent(@PathParam("id")int id);
@GET
@Produces(MediaType.APPLICATION_XML)
@Path("/queryList/{name}")
public List<Student> queryList(@PathParam("name")String name);
}
4.創建SEI實現類
public class StudentInterfaceImpl implements StudentInterface {
@Override
public Student queryStudent(int id) {
Student s=new Student();
s.setId(666);
s.setName("張三");
s.setBirthday(new Date());
return s;
}
@Override
public List<Student> queryList(String name) {
Student s=new Student();
s.setId(666);
s.setName("張三");
s.setBirthday(new Date());
Student s2=new Student();
s2.setId(888);
s2.setName("李四");
s2.setBirthday(new Date());
List<Student> l=new ArrayList<Student>();
l.add(s);
l.add(s2);
return l;
}
}
5.發佈服務
public class StudentServer {
public static void main(String[] args) {
JAXRSServerFactoryBean jaxrsServerFactoryBean=new JAXRSServerFactoryBean();
//設置服務實現類
jaxrsServerFactoryBean.setServiceBean(new StudentInterfaceImpl());
//設置資源類,如果有多個資源類,可以以“,”隔開,例如Student.class StudentInterface.class都是資源類,但是StudentInterfaceImpl裏面已經包含了Student.class StudentInterface.class,所以不用重複指定
jaxrsServerFactoryBean.setResourceClasses(StudentInterfaceImpl.class);
//設置服務地址
jaxrsServerFactoryBean.setAddress("http://127.0.0.1:12345/Class");
//發佈服務
jaxrsServerFactoryBean.create();
}
}
6.測試服務
訪問query方法
http://127.0.0.1:12345/Class/Student/query/001
訪問queryList方法
http://127.0.0.1:12345/Class/Student/queryList/xxx
如果服務端發佈時指定請求方式是GET(POST),客戶端必須使用GET(POST)訪問服務端,否則會報異常。
如果在同一方法上同時指定XML和JSON媒體類型,在GET請求下,默認返回XML,在POST請求下,默認返回JSON
CXF+Spring整合發佈REST模式的服務
1.創建web項目,引入jar包
2.創建一個實體類 Student
@XmlRootElement(name="student")可以實現XML和對象之間的轉換,name屬性指定根元素
@XmlRootElement(name="student")
public class Student {
private int id;
private String name;
private Date birthday;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
3.創建SEI接口
@WebService
//@Path("/student")就是指定訪問該接口的路徑
@Path("/Student")
public interface StudentInterface {
//指定請求方式,如果服務端發佈的時候指定的是GET(POST),那麼客戶端訪問時必須使用GET(POST
@GET
//指定服務數據類型,可以是XML,json等數據類型
@Produces(MediaType.APPLICATION_XML)
//@Path("/query/{id}")指定該方法的路徑,“{id}”指參數,多個參數,以“/”隔開,放到“{}”中
@Path("/query/{id}")
public Student queryStudent(@PathParam("id")int id);
@GET
@Produces(MediaType.APPLICATION_XML)
@Path("/queryList/{name}")
public List<Student> queryList(@PathParam("name")String name);
}
4.創建SEI實現類
public class StudentInterfaceImpl implements StudentInterface {
@Override
public Student queryStudent(int id) {
Student s=new Student();
s.setId(666);
s.setName("張三");
s.setBirthday(new Date());
return s;
}
@Override
public List<Student> queryList(String name) {
Student s=new Student();
s.setId(666);
s.setName("張三");
s.setBirthday(new Date());
Student s2=new Student();
s2.setId(888);
s2.setName("李四");
s2.setBirthday(new Date());
List<Student> l=new ArrayList<Student>();
l.add(s);
l.add(s2);
return l;
}
}
第五步:配置Spring配置文件,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:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<!-- <jaxrs:server發佈REST的服務 ,對JAXRSServerFactoryBean類封裝-->
<jaxrs:server address="/user">
<jaxrs:serviceBeans>
<ref bean="studentInterface"/>
</jaxrs:serviceBeans>
</jaxrs:server>
<!-- 配置服務實現類 -->
<bean name="studentInterface" class="com.cad.rest.StudentInterfaceImpl"/>
</beans>
6.配置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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ws_2_cxf_spring_server</display-name>
<!-- 設置spring的環境 -->
<context-param>
<!--contextConfigLocation是不能修改的 -->
<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>
<!-- 配置CXF的Servlet -->
<servlet>
<servlet-name>CXF</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXF</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>
7.部署到Tomcat中,發佈服務,測試一下
http://127.0.0.1:8080/CXFRestDemo/ws/user/Student/query/100
綜合案例:手機歸屬地查詢
1.創建web項目,導入 CXF jar包
2.生成公網提供的手機歸屬地查詢的客戶端
wsdl2java -p com.cad.mobile -d . http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
3.編寫我們自己的SEI接口
@WebService
public interface MobileInterface {
public String queryMobile(String phoneNum);
}
4.編寫我們的SEI實現類 ,裏面調用公網客戶端的查詢方法,我們在Spring配置客戶端,然後注入即可
public class MobileInterfaceImpl implements MobileInterface {
//公網客戶端,提供set方法 以便注入
private MobileCodeWSSoap mobileClient;
//調用公網的查詢方法
public String queryMobile(String phoneNum) {
return mobileClient.getMobileCodeInfo(phoneNum, "");
}
public MobileCodeWSSoap getMobileClient() {
return mobileClient;
}
public void setMobileClient(MobileCodeWSSoap mobileClient) {
this.mobileClient = mobileClient;
}
}
5.配置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:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<!-- 配置公網客戶端 -->
<jaxws:client id="mobileClient" address="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx"
serviceClass="com.cad.mobile.MobileCodeWSSoap"/>
<!-- <jaxws:server發佈我們的服務-->
<jaxws:server address="/mobile" serviceClass="com.cad.server.MobileInterface">
<jaxws:serviceBean>
<ref bean="mobileServer"/>
</jaxws:serviceBean>
</jaxws:server>
<!-- 配置我們的服務實現類 -->
<bean name="mobileServer" class="com.cad.server.MobileInterfaceImpl">
<property name="mobileClient" ref="mobileClient"/>
</bean>
</beans>
6.創建查詢頁面
<body>
<form action="MobileServlet" method="post">
手機號歸屬地查詢:<input type="text" name="phoneNum"><input type="submit" value="查詢"><br>
查詢結果:${result}
</form>
</body>
7.創建處理的Servlet
@WebServlet("/MobileServlet")
public class MobileServlet extends HttpServlet {
private MobileInterface mobileServer;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//獲取頁面的電話號
String phoneNum = request.getParameter("phoneNum");
if(null != phoneNum && !"".equals(phoneNum)){
//獲取Spring容器
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
//獲取我們的服務實現類
mobileServer = (MobileInterface) context.getBean("mobileServer");
//調用查詢方法
String result = mobileServer.queryMobile(phoneNum);
request.setAttribute("result", result);
}
//請求轉發
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
8.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>MobileDemo</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>
<!--contextConfigLocation是不能修改的 -->
<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>
<!-- 配置CXF的Servlet -->
<servlet>
<servlet-name>CXF</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXF</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
</web-app>
9.部署Tomcat,訪問測試