Spring 整合 CXF web service
Posted by Bruce Tsai
專案結構
project
| pom.xml
|
+---src
+---main
+---java
| \---com
| \---fet
| \---crm
| \---web2
| \---service
| | IHelloCXFService.java
| |
| \---impl
| HelloCXFService.java
|
+---resources
| \---spring
| application-base .xml
|
\---webapp
| index.jsp
|
\---WEB-INF
web.xml
依賴元件
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-databinding-aegis</artifactId>
<version>3.1.6</version>
</dependency>
定義 web service 介面
package com.fet.crm.web2.service;
import javax.jws.WebService;
@WebService
public interface IHelloCXFService {
long getServerTime();
}
實作 web service 內容
package com.fet.crm.web2.service.impl;
import javax.jws.WebMethod;
import javax.jws.WebService;
import org.springframework.beans.factory.annotation.Autowired;
import com.fet.crm.sample.common.dao.ICategoryRepository;
import com.fet.crm.web2.service.IHelloCXFService;
@WebService(endpointInterface = "com.fet.crm.web2.service.IHelloCXFService")
public class HelloCXFService implements IHelloCXFService {
@Autowired
ICategoryRepository categoryRepository;
@WebMethod
@Override
public long getServerTime() {
System.out.println(categoryRepository);
return System.currentTimeMillis();
}
}
配置 Spring bean 及 web service 路徑
- application-base.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:context="http://www.springframework.org/schema/context"
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://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-extension-soap.xml" /> -->
<!-- <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> -->
<bean id="hello" class="com.fet.crm.web2.service.impl.HelloCXFService" />
<jaxws:endpoint id="helloWorld" implementor="#hello"
address="/HelloCXFService" />
<!-- <jaxws:endpoint id="helloWorld" -->
<!-- implementor="com.fet.crm.web2.service.impl.HelloCXFService" address="/HelloCXFService"
/> -->
</beans>
配置 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"
version="2.5">
...
<!-- spring 設定檔 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/application-base.xml</param-value>
</context-param>
<!-- cxf web service -->
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/cxf/*</url-pattern>
</servlet-mapping>
...
</web-app>
查看結果
- wsdl 清單: http://{hostaddress}:{port}/{contextpath}/{servletprefix}
- wsdl 位罝: http://{hostaddress}:{port}/{contextpath}/{servletprefix}/{servicepath}?wsdl,本例為 http://localhost:8080/sample/cxf/HelloCXFService?wsdl
- serlvetprefix 為 web.xml 中設定的 CXFServlet url pattern
- servicepath 需與 spring bean 中設定的 endpoint 一致