CXF開發及與Spring整合開發
前言
本篇承接上一篇
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
相關文章
- 基於Maven的Spring整合CXFMavenSpring
- WebService之Spring+CXF整合示例WebSpring
- SSM整合開發SSM
- Flutter與Native混合開發-FlutterBoost整合應用和開發實踐(iOS)FlutteriOS
- WePy 整合雲開發
- SpringBoot整合Redis開發Spring BootRedis
- SSM框架整合開發SSM框架
- 基於spring-boot&spring-data-jpa的web開發環境整合SpringbootWeb開發環境
- iOS開發 iOS整合FFmpeg及視訊格式轉碼iOS
- PhpStorm 9,PHP整合開發PHPORM
- SpringBoot整合Apache-CXF實踐Spring BootApache
- spring註解開發Spring
- Spring 註解開發Spring
- PhpStorm 2021 for Mac(PHP整合開發)PHPORMMac
- PHP整合開發工具:PhpStorm 2022PHPORM
- PhpStorm 2022 for Mac(php整合開發)PHPORMMac
- PhpStorm 2021 Mac(PHP整合開發)PHPORMMac
- laravel微服務開發,整合rabbitmqLaravel微服務MQ
- Spring Boot 的 Web 開發Spring BootWeb
- Spring的註解開發Spring
- spring 程式開發步驟Spring
- Spring Boot 開發整合 WebSocket,實現私有即時通訊系統Spring BootWeb
- spring-boot 整合mybatis-plus 組成後臺開發基本框架SpringbootMyBatis框架
- 聚焦遊戲開發速度與成本,穩定引擎基礎助力開發者快速整合應用遊戲開發
- 基於spring boot 及mybatis的web開發環境搭建Spring BootMyBatisWeb開發環境
- 合約量化系統開發(開發策略及詳細)丨量化合約系統開發(開發原始碼及說明)原始碼
- API開發平臺,提高API開發及管理效率API
- 小程式開發總結一:mpvue框架及與小程式原生的混搭開發Vue框架
- Spring註解開發_Spring容器建立概述Spring
- Spring開發:動態代理的藝術與實踐Spring
- 淺嘗Spring註解開發_Servlet3.0與SpringMVCServletSpringMVC
- DSW:面向AI研發的整合開發平臺AI
- 【Spring註解驅動開發】聊聊Spring註解驅動開發那些事兒!Spring
- 玩轉iOS開發:Touch ID整合iOS
- PhpStorm 2023 for Mac,PHP整合開發工具PHPORMMac
- 整合開發環境(IDE)的用途?開發環境IDE
- 混合開發:flutter整合進iOS工程FlutteriOS
- iOS整合 Flutter 混合工程開發一iOSFlutter