spring,xfire开发soap接口

xfire开发webservice服务接口简洁博客

 

一、新建一个maven工程导入依赖

<dependencies>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.codehaus.xfire</groupId>
			<artifactId>xfire-all</artifactId>
			<version>1.2.6</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.0.0.RELEASE</version>
		</dependency>
	</dependencies>

 二、配置web.xml

spring配置和xfire配置

<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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ws</display-name>
<!--   spring配置 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring*.xml</param-value>
  </context-param>
<!--   xfire配置 -->
  <servlet>
    <servlet-name>xfire</servlet-name>
    <servlet-class>  
     org.codehaus.xfire.spring.XFireSpringServlet  
    </servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>xfire</servlet-name>
    <url-pattern>/ws/service/*</url-pattern>
  </servlet-mapping>
</web-app>

 

三、helloword 接口和实现

public interface HelloWorldService {

	public String say(String word);
}

 

public class HelloWordServiceImpl implements HelloWorldService{

	public String say(String word){
		
		return "hello:"+word;
	}
}

 四、配置spring配置文件为外提供接口服务

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<!-- 引入JAR中XFire默认配置信息 -->
	<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
	<!-- 使用XFire接口导出器 -->
	<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="helloSI" class="org.jxx.ws.server.HelloWordServiceImpl">
	</bean>
	<!--第二步,实现websvice,暴露接口-->
	<bean id="helloWorldService" parent="baseWebService">
		<!-- 业务服务bean -->  
		<property name="serviceBean" ref="helloSI" />
		<!-- 爆率业务服务接口类-->
		<property name="serviceClass" value="org.jxx.ws.server.HelloWorldService" />
	</bean>
</beans>
五、测试类
public class ClientTest {
	public static void main(String[] args) throws MalformedURLException, Exception {
		//第一种方式
		String serviceUrl1 ="http://localhost:803/ws/ws/service/HelloWorldService?wsdl";
		Client client = new Client(new URL(serviceUrl1));
		Object[] results = client.invoke("say", new Object[]{"hello11111111"});
		
		System.out.println(results[0]);
		
		//第二种方式 
		//客户端可以自行建立一个同名interface.参考 WSDL 建立interface方法和参数
		String serviceurl2 ="http://localhost:803/ws/ws/service/HelloWorldService";
		Service service = new ObjectServiceFactory().create(HelloWorldService.class) ;  
		XFireProxyFactory factory = new XFireProxyFactory() ;  
		HelloWorldService helloService = (HelloWorldService) factory.create(service,serviceurl2) ;  

		System.out.println(helloService.say("hello22222222"));
	}
			
}
 

 

你可能感兴趣的:(java,spring,webservice,xfire,SOAP)