WebService的CXF框架與Spring的整合釋出服務入門
阿新 • • 發佈:2019-01-07
1、建立WEB專案,將CXF與SPRING的包引入專案(在下載的CXF的lib包裡,裡面已經整合好了,直接引入進去就行了)
2、建立介面
package com.ckinghan.cxf.server.service;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;
/**
* 使用CXF框架,在介面上要使用@WebService註解
* @author Ckinghan
*/
@WebService
//這裡指定建立的是SOAP1.2的,如果不指定,建立的是SOAP1.1的,現在大部分使用的都是1.2的
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public interface WeatherService {
public String getWeather(String cityName);
}
3、建立介面的實現類
package com.ckinghan.cxf.server.service.Impl;
import com.ckinghan.cxf.server.service.WeatherService;
public class WeatherServiceImpl implements WeatherService {
@Override
public String getWeather(String cityName) {
System.out.println("收到來自客戶端面的請求:"+cityName);
return "這是一個好天氣";
}
}
4、在專案中建立config (Source Folder)資料夾,並在此檔案中建立applicationContent.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">
<!-- spring對Endpoint也做了封裝,可以使用jaxws:endpoint進行服務釋出 -->
<jaxws:endpoint address="/hello" implementor="com.ckinghan.cxf.server.service.test.EndpointTest"/>
<!-- 配置WebService的釋出服務,設定訪問地址(預設的已經有地址,只需要將訪問的最後路徑新增上即可),設定訪問介面 -->
<jaxws:server address="/weather" serviceClass="com.ckinghan.cxf.server.service.WeatherService">
<!-- 引入介面的實現類 -->
<jaxws:serviceBean>
<ref bean="serviceBean" />
</jaxws:serviceBean>
<!-- 配置攔截器 -->
<jaxws:inInterceptors >
<ref bean="inInterceptor"/>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<ref bean="outInterceptor"/>
</jaxws:outInterceptors>
</jaxws:server>
<!-- 設定介面的實現類 -->
<bean name="serviceBean" class="com.ckinghan.cxf.server.service.Impl.WeatherServiceImpl"/>
<!-- 配置攔截器 -->
<bean name="inInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
<bean name="outInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
</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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>cxfServer1</display-name>
<!-- 設定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>/webService/*</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、建立測試Sping 封裝Endpoint釋出服務的檔案
package com.ckinghan.cxf.server.service.test;
import javax.jws.WebService;
@WebService
public class EndpointTest {
public static void main(String[] args) {
System.out.println("HELLO");
}
}
7、啟動專案
10、建立客戶端的JAVA專案,並使用 wsdl2java -p com.ckinghan.cxf.client -d 專案目錄 http://localhost:8080/cxfServer1/webService/weather?wsdl生成客戶端程式碼
11、引入JAR包
12、建立config Source Folder,並建立applicationContext.xml可以從服務層COPY過來修改
<?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">
<!-- 配置WebService客戶端,設定訪問地址(這裡需要填寫全路徑),設定訪問介面 ,定義ID用以獲取-->
<jaxws:client id="weatherClient" serviceClass="com.ckinghan.cxf.server.service.WeatherService" address="http://localhost:8080/cxfServer1/webService/weather"/>
</beans>
13、建立測試 類
package com.ckinghan.cxf.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ckinghan.cxf.server.service.WeatherService;
public class CXFClientTest {
public static void main(String[] args) {
//獲取Spring上下文
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//獲取bean物件
WeatherService weatherService = (WeatherService) context.getBean("weatherClient");
//呼叫介面法,獲取資訊
String weather = weatherService.getWeather("上海");
//輸出獲取的資訊
System.out.println(weather);
}
}
14、執行。輸出結果如下: