基于maven的XFire构建webservice服务

方式一:j2ee项目中添加,不集成spring的项目
1、构建maven项目,maven项目的构建这里省略,pom.xml依赖项目xfire-all:

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


xfire-all项目引入后,因为它依赖其他项目,所以其他项目也会跟着下载下来的。当然还包括其他的依赖,那是j2ee必备的依赖。

完整的pom.xml:

<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的配置:

<?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的写法,接下来会有简单说明。

3.构建webservice服务类。这个包括一个接口和一个实现类。如:

接口:

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

实现类:

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:

<?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>  

比较容易看懂吧。

5.启动tomcat。我的maven项目集成了jetty和tomcat的,所以不用跟另外引入外部的tomcat。--tomcat7:run。
访问我们的服务:http://localhost:8080/apacheCommon/services页面中可以看到我们发布的那些webservice服务。这里只有一个,OrgService。点击进入就是:http://localhost:8080/apacheCommon/services/OrgService?wsdl。到此已经完成服务端的开发好了。


方式二、j2ee项目中添加,集成spring的项目

1、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>


3、访问webservice服务和第一种一样。到此,第二种方式也介绍完了。

 

这篇博客能帮到你了吗,有啥疑问可以相互交流。

转载于:https://my.oschina.net/u/920528/blog/743152

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值