xfire+spring配置webservice实例讲解

web.xml::

  
    <servlet>  
         
       <servlet-name>xfireServletservlet-name>  
       <servlet-class>org.codehaus.xfire.spring.XFireSpringServletservlet-class>  
    servlet>  
    <servlet-mapping>  
       <servlet-name>xfireServletservlet-name>  
         
       <url-pattern>/service/*url-pattern>  
    servlet-mapping>   
  

xfire-servlet.xml:




<beans>

    
    <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />

      
    <bean id="userService" class="com.test.xfire.UserServiceImpl" />


     
    
    <bean id="webAnnotations" class="org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations" /> 

    
    <bean id="jsr181HandlerMapping" class="org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping"> 
        <property name="xfire" ref="xfire" /> 
        <property name="webAnnotations" ref="webAnnotations" /> 
    bean>

beans>
package com.test.xfire;

public class User
{
    private String username;
    private int age;
    private String hobby;

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    public int getAge()
    {
        return age;
    }

    public void setAge(int age)
    {
        this.age = age;
    }

    public String getHobby()
    {
        return hobby;
    }

    public void setHobby(String hobby)
    {
        this.hobby = hobby;
    }

    public String toString()
    {
        return "用户:" + username + ",年龄" + age + "时,爱好:" + hobby;
    }
}
package com.test.xfire;

import javax.jws.WebService;

@WebService
public interface IUserService
{
    public User findUserHobby(@WebParam(name="user") User user) throws Exception;
}
package com.test.xfire;

import java.util.HashMap;
import java.util.Map;

import javax.jws.WebService;

/**
 * serviceName是这这个服务的名称,默认为接口实现类的名称,endpointInterface是该类实现的接口的类全名
* 访问:http://localhost:8080/ssh/service/userServiceImpl?wsdl * * @author Administrator */
@WebService(serviceName = "userServiceImpl", endpointInterface = "com.test.xfire.IUserService") public class UserServiceImpl implements IUserService { private static final Map mapUser = new HashMap(); static { mapUser.put("jg.sun", "篮球"); mapUser.put("lcrystal", "足球"); mapUser.put("s0meb0dy", "游泳"); mapUser.put("猫来猫去", "睡觉"); mapUser.put("小刚", "唱歌"); } public User findUserHobby(User user) throws Exception { if (user == null) { return null; } String hobby = mapUser.get(user.getUsername()); if (hobby == null) { user.setHobby("无"); } else { user.setHobby(hobby); } return user; } }

客户端调用2种方式:
1、通过WSDL文件生成客户端调用程序,先在目录存放WSDL文件

package com.test.xfire.client;

import org.codehaus.xfire.client.Client;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.test.xfire.IUserService;
import com.test.xfire.User;

/**
 * 通过WSDL文件生成客户端调用程序
 * 
 * @author Administrator
 */
public class WebServiceClientTestByWsdl
{
    IUserService iUserService = null;

    public static void main(String[] args) throws Exception
    {
        WebServiceClientTest test = new WebServiceClientTest();
        test.testClient();
    }

    public void testClient() throws Exception
    {
        String wsdl = "userServiceImpl.wsdl"; // 对应的WSDL文件
        Resource resource = new ClassPathResource(wsdl);
        Client client = new Client(resource.getInputStream(), null); // 根据WSDL创建客户实例

        User user = new User();
        user.setUsername("猫来猫去");
        Object[] objArray = new Object[1];
        objArray[0] = user;
        // 调用特定的Web Service方法
        Object[] results = client.invoke("findUserHobby", objArray);
        System.out.println("result: " + results[0]);
    }
}

2、根据服务地址创建客户端调用程序

webserviceClient.xml



<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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

    <bean  id ="testWebService"  class ="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean">
        <property name ="serviceClass">          
            <value>com.test.xfire.IUserServicevalue>       
        property>      
        <property name ="wsdlDocumentUrl">         
            <value>http://localhost:8080/ssh/service/userServiceImpl?wsdlvalue>       
        property>      
    bean>


beans>
package com.test.xfire.client;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.test.xfire.IUserService;
import com.test.xfire.User;

/**
 * 根据服务地址创建客户端调用程序
 * 
 * @author Administrator
 */
public class WebServiceClientTest
{
    IUserService iUserService = null;

    public static void main(String[] args)
    {
        WebServiceClientTest test = new WebServiceClientTest();
        test.testClient();
    }

    public void testClient()
    {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("webserviceClient.xml");
        iUserService = (IUserService) ctx.getBean("testWebService");
        try
        {
            User user = new User();
            user.setUsername("猫来猫去");
            System.out.println(iUserService.findUserHobby(user));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

转自:http://coach.iteye.com/blog/894159

你可能感兴趣的:(xfire+spring配置webservice实例讲解)