CXF開發及與Spring整合開發

oscar999發表於2020-04-06

前言

本篇承接上一篇

Java CXF介紹與例項

開發環境:

Java: 1.8.0_161

Eclipse Version: Oxygen.3 Release (4.7.3)

Maven: 3.5.3

Tomcat:7.0

工程開發環境與框架搭建

1. 使用maven建立web 專案

mvn archetype:generate -DgroupId=com.oscar999 -DartifactId=WebStudy -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

2. 切換到產生的專案路徑,將此專案切換為eclipse的web專案

mvn eclipse:eclipse -Dwtpversion=2.0

注: 這裡直接使用wtpversion=4.0會有問題, 所以先用2.0, 後面再修改。

這個設定和Tomcat的版本是對應的:

dynamic web module 2.4 對應Tomcat 5.5
dynamic web module 2.5 對應Tomcat 6.0
dynamic web module 3.0 對應Tomcat 7.0

3. 開啟eclipse, 將此專案匯入。

4. 修改wtpversion

有兩種方式:

 一種是在eclipse介面操作。工程上右鍵單擊,找到Project Facets, 可以設定Dynamic Web Module和Java的版本

另一種是通過修改eclipse 檔案。專案路徑.settings 的org.eclipse.wst.common.project.facet.core.xml 
  <installed facet="jst.java" version="1.8"/>

  <installed facet="jst.web" version="3.0"/>

5. 新增伺服器到eclipse

windows->Preferences--> Server-->Runtime Environment, 加入Tomcat 7

6. 在pom.xml加入cxf依賴項

<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf</artifactId>
			<version>3.2.4</version>
			<type>pom</type>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>3.2.4</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http</artifactId>
			<version>3.2.4</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.woodstox</groupId>
			<artifactId>stax2-api</artifactId>
			<version>4.1</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.woodstox</groupId>
			<artifactId>woodstox-core-asl</artifactId>
			<version>4.4.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-simple</artifactId>
			<version>3.2.4</version>
		</dependency>

7. 需要Servlet API

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>4.0.1</version>
			<scope>provided</scope>
		</dependency>

不整合Spring 的方式

1. 介面與實現類

MyWebServiceI 和MyWebServiceImpl沿用上一篇的就可以了。

2.  定義Servlet

/**
 * @Title: CxfPublishServlet.java
 * @Package com.oscar999
 * @Description: TODO
 * @author oscar999
 * @date May 23, 2018 4:22:51 PM
 * @version V1.0
 */

package com.oscar999;

import javax.servlet.ServletConfig;

import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.apache.cxf.transport.servlet.CXFNonSpringServlet;
import  org.apache.cxf.frontend.*;

/**
 * @ClassName: CxfPublishServlet
 * @Description: TODO
 * @author oscar999
 */

public class CxfPublishServlet extends CXFNonSpringServlet {

	@Override
	public void loadBus(ServletConfig sc) {
		super.loadBus(sc);
		Bus bus = getBus();
		BusFactory.setDefaultBus(bus);
		// Endpoint.publish("/sayHi",new MyWebServiceImpl());
		MyWebServiceImpl hi = new MyWebServiceImpl();
		ServerFactoryBean sfb = new ServerFactoryBean(); // server
		sfb.setServiceClass(MyWebServiceI.class);//
		sfb.setAddress("/myweb"); //
		sfb.setServiceBean(hi);
		sfb.create();
	}
}

3. 在web.xml 新增Servlet的配置

	<servlet>
		<description>publsh web serivces</description>
		<display-name>CxfPublishServlet</display-name>
		<servlet-name>CxfPublishServlet</servlet-name>
		<servlet-class>com.oscar999.CxfPublishServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>CxfPublishServlet</servlet-name>
		<url-pattern>/webservice/*</url-pattern>
	</servlet-mapping>

4. Run as -> Run on Server , 訪問如下地址:

http://localhost:8080/WebStudy/webservice/myweb?wsdl

整合Spring的方式

這裡新進一個新的web Project: webPrj

1. 新建Project

mvn archetype:generate -DgroupId=com.oscar999 -DartifactId=webPrj -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

2. eclipse設定和依賴項匯入和上面類似。

3.匯入Spring 依賴項

<!--Spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.3.17.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>4.3.17.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>4.3.17.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.3.17.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>4.3.17.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.3.17.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.3.17.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>4.3.17.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>4.3.17.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>4.3.17.RELEASE</version>
		</dependency>

接下來,開始程式碼和配置部分

1. 介面

/**
 * @Title: MyWebServiceI.java
 * @Package com.oscar999
 * @Description: TODO
 * @author oscar999
 * @date May 23, 2018 11:34:39 AM
 * @version V1.0
 */

package com.oscar999;

import javax.jws.WebParam;
import javax.jws.WebService;

/**
 * @ClassName: MyWebServiceI
 * @Description: TODO
 * @author oscar999
 */

@WebService
public interface MyWebServiceI {

	String sayHi(@WebParam(name = "text") String text);  
}

2.實現類

/**
 * @Title: MyWebServiceImpl.java
 * @Package com.oscar999
 * @Description: TODO
 * @author oscar999
 * @date May 23, 2018 11:37:02 AM
 * @version V1.0
 */

package com.oscar999;

import javax.jws.WebParam;
import javax.jws.WebService;

/**
 * @ClassName: MyWebServiceImpl
 * @Description: TODO
 * @author oscar999
 */

public class MyWebServiceImpl implements MyWebServiceI {

	public String sayHi(@WebParam(name = "text") String text) {
		return "Hello," + text;
	}
}

3. 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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

	<bean id="myWebService" class="com.oscar999.MyWebServiceImpl"></bean>
	<jaxws:server address="/myService"
		serviceClass="com.oscar999.MyWebServiceI">
		<jaxws:serviceBean>
			<ref bean="myWebService" />
		</jaxws:serviceBean>
	</jaxws:server>
</beans>

4. 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"
	xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	version="3.0">

	<display-name>Archetype Created Web Application</display-name>

	<servlet>
		<servlet-name>cxfServlet</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
		<load-on-startup>0</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>cxfServlet</servlet-name>
		<url-pattern>/ws/*</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:applicationContext.xml</param-value>
	</context-param>
</web-app>

5. 訪問WSDl

http://localhost:8080/webPrj/ws/myService?wsdl

問題及解決


1. jar 包沒有copy 到eclipse 
進入到tomcat的部署路徑.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\下檢查了一下,發現工程部署後在WEB-INF資料夾下沒有生成lib目錄,正常情況下,會生成lib目錄並把工程的所有依賴jar包都部署到該目錄下。
解決方法:
右鍵點選專案--選擇Properties
選擇Deployment Assembly,在右邊點選Add按鈕,在彈出的視窗中選擇Java Build Path Entries

相關文章