CXF學習(Spring整合)
阿新 • • 發佈:2018-12-26
一、相關jar包依賴
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId >
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version> 4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId >org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.2</version>
</dependency>
</dependencies>
資料庫連線池、資料庫驅動包、日誌等可以去http://mvnrepository.com/載入
二、相關配置檔案
(1)web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>myapp</display-name>
<servlet>
<description>WebService服務釋出</description>
<display-name>cxf</display-name>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/Services/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/myapp-context.xml</param-value>
</context-param>
</web-app>
(2)myapp-context.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:aop="http://www.springframework.org/schema/aop"
xmlns:beans="http://cxf.apache.org/configuration/beans"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:core="http://cxf.apache.org/core"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://cxf.apache.org/configuration/beans http://cxf.apache.org/schemas/configuration/cxf-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<import resource="myapp-resources.xml"/>
<import resource="cxf-servlet.xml" />
</beans>
(3)cxf-servlet.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:addressing="http://schemas.xmlsoap.org/ws/2004/08/addressing"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:beans="http://cxf.apache.org/configuration/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://schemas.xmlsoap.org/wsdl/http/ http://schemas.xmlsoap.org/wsdl/http/
http://schemas.xmlsoap.org/wsdl/ http://schemas.xmlsoap.org/wsdl/2003-02-11.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://schemas.xmlsoap.org/ws/2004/08/addressing http://schemas.xmlsoap.org/ws/2004/08/addressing
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://cxf.apache.org/configuration/beans http://cxf.apache.org/schemas/configuration/cxf-beans.xsd">
<bean id="myservice" class="com.soft.platform.webservice.MyServiceImpl"></bean>
<jaxws:endpoint id="appservice" implementor="#myservice" address="/MyService" />
</beans>
相關環境:jdk1.7 和Tomcat7
三、建立服務相關步驟
(1)Create a Service Endpoint Interface (SEI) that defines the methods you wish to expose as a service.The SEI is a standard Java interface. It defines a set of methods that a class will implement. It can also define a number of member fields and constants to which the implementing class has access.
(2)Add the required annotations to your code.
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
/**
* soap介面
*/
@WebService
public interface MyService {
@WebMethod(operationName = "authorization")
String authorization(@WebParam(name = "userId") String userId,
@WebParam(name = "password") String password);
}
import javax.jws.WebService;
@WebService(endpointInterface = "com.soft.platform.webservice.MyService",
serviceName = "MyService")
public class MyServiceImpl implements MyService {
@Override
public String authorization(String userId, String password) {
if ("admin".equals(userId) && "123456".equals(password)) {
return "success";
}
return "false";
}
}
(3)Publish the service.(cxf-servlet.xml)
<bean id="myservice" class="com.soft.platform.webservice.MyServiceImpl"></bean>
<jaxws:endpoint id="appservice" implementor="#myservice" address="/MyService" />
或者
<jaxws:server id="jaxwsService" serviceClass="com.soft.platform.webservice.MyService"
address="/MyService">
<jaxws:serviceBean>
<bean class="com.soft.platform.webservice.MyServiceImpl" />
</jaxws:serviceBean>
</jaxws:server>
(4)部署啟動Tomcat訪問http://localhost:8080/myapp/Services/MyService?wsdl