基於maven的XFire構建webservice服務

方式一:j2ee項目中添加,不集成spring的項目
一、構建maven項目,maven項目的構建這裏省略,pom.xml依賴項目xfire-all:java

        <dependency>
            <groupId>org.codehaus.xfire</groupId>
            <artifactId>xfire-all</artifactId>
            <version>1.2.6</version>
        </dependency>


xfire-all項目引入後,由於它依賴其餘項目,因此其餘項目也會跟着下載下來的。固然還包括其餘的依賴,那是j2ee必備的依賴。web

完整的pom.xml:spring

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com</groupId>
	<artifactId>apacheCommon</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>apacheCommon</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<!-- j2ee -->
		<dependency>
			<groupId>javax</groupId>
			<artifactId>javaee-api</artifactId>
			<version>7.0</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		
		<dependency>
			<groupId>org.codehaus.xfire</groupId>
			<artifactId>xfire-all</artifactId>
			<version>1.2.6</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>apacheCommon</finalName>
		<resources>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.properties</include>
					<include>**/*.xml</include>
				</includes>
				<filtering>false</filtering>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>**/*.*</include>
				</includes>
				<filtering>false</filtering>
			</resource>
		</resources>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.apache.tomcat.maven</groupId>
					<artifactId>tomcat7-maven-plugin</artifactId>
					<version>2.2</version>
					<configuration>
						<uriEncoding>utf-8</uriEncoding>
						<port>8080</port>
					</configuration>
				</plugin>
				<plugin>
					<groupId>org.mortbay.jetty</groupId>
					<artifactId>jetty-maven-plugin</artifactId>
					<version>8.1.16.v20140903</version>
					<configuration>
						<stopKey>stop</stopKey>
						<stopPort>9999</stopPort>
						<scanIntervalSeconds>1</scanIntervalSeconds>
						<contextXml>${project.basedir}/src/main/resources/jetty-context.xml</contextXml>
						<webApp>
							<contextPath>/cb</contextPath>
						</webApp>
						<connectors>
							<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
								<port>80</port>
								<maxIdleTime>60000</maxIdleTime>
							</connector>
						</connectors>

						<webAppSourceDirectory>src/main/webapp</webAppSourceDirectory>
					</configuration>
				</plugin>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-war-plugin</artifactId>
					<version>2.6</version>
					<configuration>
						<failOnMissingWebXml>false</failOnMissingWebXml>
					</configuration>
				</plugin>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-compiler-plugin</artifactId>
					<version>2.3.2</version>
					<configuration>
						<source>1.7</source>
						<target>1.7</target>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>
</project>

2.web項目必然少不了web的配置:apache

<?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"
	version="3.0">
	<display-name>Archetype Created Web Application</display-name>
	<servlet>
		<servlet-name>XFireServlet</servlet-name>	
		<servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
		<load-on-startup>0</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>XFireServlet</servlet-name>
		<url-pattern>/services/*</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

當請求到這裏時候org.codehaus.xfire.transport.http.XFireConfigurableServlet,它會找到一個services.xml的配置文件,services.xml要咱們本身寫提供XFireConfigurableServlet調用,services.xml主要做用是說明咱們webservice是哪些類實現的還有咱們webservice服務的一些其餘信息,如命名空間。services.xml的完整路徑是/src/main/java/META-INF/xfire/services.xml。在個人maven項目我放在/src/main/resources/META-INF/xfire/services.xml.由於個人maven項目中我把resouces目錄設爲資源路徑,最終項目打包的時候META-INF/xfire/services.xml會被拷貝的classs目錄下的。serviecs.xml的寫法,接下來會有簡單說明。api

3.構建webservice服務類。這個包括一個接口和一個實現類。如:tomcat

接口:mvc

package com.proxy.http.webservice;
public interface OrgService {
    public int getUserInfo(String userId);
    public int getDepartmentInfo(String dptId);
}

實現類:app

package com.proxy.http.webservice;
public class OrgServiceImpl implements OrgService{
    @Override
    /**
     * 這裏寫咱們的業務
     */
    public int getUserInfo(String userId) {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    /**
     * 這裏寫咱們的業務
     */
    public int getDepartmentInfo(String dptId) {
        // TODO Auto-generated method stub
        return 0;
    }
}

4. services.xml:eclipse

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
	<service>
		<name>OrgService</name>
		<namespace>http://www.apache.com/OrgService</namespace>
		<serviceClass>com.proxy.http.webservice.OrgService</serviceClass>
		<implementationClass>com.proxy.http.webservice.OrgServiceImpl
		</implementationClass>
	</service>
</beans>

比較容易看懂吧。webapp

5.啓動tomcat。個人maven項目集成了jetty和tomcat的,因此不用跟另外引入外部的tomcat。--tomcat7:run。
訪問咱們的服務:http://localhost:8080/apacheCommon/services頁面中能夠看到咱們發佈的那些webservice服務。這裏只有一個,OrgService。點擊進入就是:http://localhost:8080/apacheCommon/services/OrgService?wsdl。到此已經完成服務端的開發好了。


方式2、j2ee項目中添加,集成spring的項目

一、pom.xml依賴項目xfire-all,剔除spring1.2.6的jar包避免衝突,另外加入spring核心項目:

<!-- spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>3.2.7.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>3.2.7.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>3.2.7.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>3.2.7.RELEASE</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.codehaus.xfire/xfire-all -->
		<dependency>
			<groupId>org.codehaus.xfire</groupId>
			<artifactId>xfire-all</artifactId>
			<version>1.2.6</version>
			<exclusions>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring</artifactId>
				</exclusion>
			</exclusions>
		</dependency>


2.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"
    version="3.0">
    <display-name>Archetype Created Web Application</display-name>
    
    <!--xfire方式發佈 webservice -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
            classpath:spring-application.xml
    </param-value>
  </context-param>
    <listener>
        <description>spring監聽器</description>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>XFireServlet</servlet-name>
        <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>XFireServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>
  
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

其中spring-application.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:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/cache  
            http://www.springframework.org/schema/cache/spring-cache-3.2.xsd  
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
    <bean id="baseWebService" class="org.codehaus.xfire.spring.remoting.XFireExporter"
        lazy-init="false" abstract="true">
        <property name="serviceFactory" ref="xfire.serviceFactory" />
        <property name="xfire" ref="xfire" />
    </bean>
    <bean id="OrgServiceImpl" class="com.proxy.http.webservice.OrgServiceImpl" />  <!-- 這是接口實現類 -->
    <bean id="OrgService" parent="baseWebService">
        <property name="serviceBean" ref="OrgServiceImpl" />
        <property name="serviceClass" value="com.proxy.http.webservice.OrgService" /> <!-- 這是接口 -->
        <property name="properties">
            <map>
                <entry key="mtom-enabled" value="true" />
            </map>
        </property>
    </bean>
</beans>


三、訪問webservice服務和第一種同樣。到此,第二種方式也介紹完了。

 

這篇博客能幫到你了嗎,有啥疑問能夠相互交流。

相關文章
相關標籤/搜索