作者微信 bishe2022

代码功能演示视频在页面下方,请先观看;如需定制开发,联系页面右侧客服
java程序调用xfire发布的webService服务(二)

Custom Tab

在上一篇的调用xfire发布的webService服务中,我只是从服务端返回了一个字符串给客户端,却没有测试从客户端传递数据给服务端。而实际应用中一般是不太可能只出现这样的应用场景的,因此我便更进一步测试了客户端传递数据给服务端。

因为相关的jar包在上一篇已经说过,因此便不再重复说明,这次的测试步骤如下:

一、测试向服务端传递字符串(重点在第二个):

    为了进一步理解服务搭建,我重新写了一个服务端服务类:

接口:

[java] 
package xfireTest;  
  
public interface XFireTestService {  
    public String test(String cont);  
}

实现类:

[java] 
package xfireTest.xfireTestImp;  
import xfireTest.XFireTestService;  
  
public class XFireTestImp implements XFireTestService {  
  
    @Override  
    public String test(String cont) {  
        cont = "webService收到消息:" + cont;  
        return cont;  
    }  
}

在之前的services.xml中加入了以下代码:

[html] 
<service xmlns="http://xfire.codehaus.org/config/1.0">  
        <!-- webService服务的名称 -->  
        <name>XFireTest</name>  
        <namespace>http://xfireTest/XFireTestService</namespace>  
        <!-- 自己所写的接口路径 -->  
        <serviceClass>  
            xfireTest.XFireTestService  
        </serviceClass>  
        <!-- 实现类路径 -->  
        <implementationClass>  
            xfireTest.xfireTestImp.XFireTestImp  
        </implementationClass>      
    </service>

然后是另外一个项目中模拟客户端调用代码,同样是需要先创建和服务端一样的服务接口:

[java] 
package test;  
  
public interface XFireTestService {  
    public String test(String cont);  
}

然后是调用:

[java] 
package test;  
  
import org.codehaus.xfire.XFireFactory;  
import org.codehaus.xfire.client.XFireProxyFactory;  
import org.codehaus.xfire.service.Service;  
import org.codehaus.xfire.service.binding.ObjectServiceFactory;  
  
public class CallWebServiceTest1 {  
    public static void main(String[] args) {  
        Service srModel = new ObjectServiceFactory()  
                .create(XFireTestService.class);  
        XFireProxyFactory factory = new XFireProxyFactory(XFireFactory  
                .newInstance().getXFire());// 创建工厂实例  
  
        String helloURL = "http://localhost:8082/xfireTest/services/XFireTest";  
        try {  
            XFireTestService service = (XFireTestService) factory.create(  
                    srModel, helloURL);  
            System.out.println(service.test("测试"));  
        } catch (Exception e) {  
            throw new RuntimeException(e);  
        }  
    }  
}

这里的调用是比较简单的,也没有什么问题,字符串的传递完全可以。

二、调用时传递对象(重点在结尾处)


这个测试,我写了一个简单的UserModel类,用来当做参数使用,根据网上的一些资料,我给实体类做了序列化,如下:

[java] 
package xfireTest;  
import java.io.Serializable;  
public class UserModel implements Serializable {  
  
    /** 
     *  
     */  
    private static final long serialVersionUID = -8344776127885486411L;  
  
    public UserModel() {  
        super();  
    }  
  
    public UserModel(String userName, int age) {  
        super();  
        this.userName = userName;  
        this.age = age;  
    }  
  
    /** 
     * 用户名 
     */  
    private String userName;  
    /** 
     * 用户年龄 
     */  
    private int age;  
  
    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;  
    }  
  
    @Override  
    public String toString() {  
        return "UserModel [userName=" + userName + ", age=" + age + "]";  
    }  
}

然后是对应的接口:

[java] 
package xfireTest;  
import javax.jws.WebParam;  
import javax.jws.WebService;  
  
@WebService(serviceName = "UserService", targetNamespace = "http://xfireTest/UserService")  
public interface UserService {  
    public UserModel addUser(@WebParam(name = "user") UserModel user);  
}

实现类:

[java] 
package xfireTest.xfireTestImp;  
import xfireTest.UserModel;  
import xfireTest.UserService;  
public class UserServiceImp implements UserService {  
  
    @Override  
    public UserModel addUser(UserModel user) {  
        // System.out.println(user);  
        System.out.println(user.getUserName() + ":" + user.getAge());  
        return user;  
    }  
}

services.xml中加入如下代码:

[html]
<service xmlns="http://xfire.codehaus.org/config/1.0">  
       <!-- webService服务的名称 -->  
       <name>UserService</name>  
       <namespace>http://xfireTest/UserService</namespace>  
       <!-- 自己所写的接口路径 -->  
       <serviceClass>  
           xfireTest.UserService  
       </serviceClass>  
       <!-- 实现类路径 -->  
       <implementationClass>  
           xfireTest.xfireTestImp.UserServiceImp  
       </implementationClass>      
   </service>

然后是客户端调用的代码:

[java]
package test;  
import org.codehaus.xfire.XFireFactory;  
import org.codehaus.xfire.client.XFireProxyFactory;  
import org.codehaus.xfire.service.Service;  
import org.codehaus.xfire.service.binding.ObjectServiceFactory;  
  
public class CallWebServiceTest2 {  
  
    public static void main(String[] args) {  
        Service srModel = new ObjectServiceFactory().create(UserService.class);  
        XFireProxyFactory factory = new XFireProxyFactory(XFireFactory  
                .newInstance().getXFire());// 创建工厂实例  
  
        String helloURL = "http://localhost:8082/xfireTest/services/UserService";  
        try {  
            UserService service = (UserService) factory.create(srModel,  
                    helloURL);  
            UserModel user = new UserModel();  
            user.setAge(22);  
            user.setUserName("test");  
            System.out.println(service.addUser(user));  
        } catch (Exception e) {  
            throw new RuntimeException(e);  
        }  
  
    }  
}

因为和服务端是不同的项目,因此需要自己新建和服务端一样的服务接口以及UserModel类,我都写在和上一个类的同一个包中,即test中:

接口:

[java] 
package test;  
import javax.jws.WebParam;  
import javax.jws.WebService;  
  
@WebService(serviceName = "UserService", targetNamespace = "http://xfireTest/UserService")  
public interface UserService {  
    public UserModel addUser(@WebParam(name = "user") UserModel user);  
}

UserModel类:

[java] 
package test;  
import java.io.Serializable;  
public class UserModel implements Serializable {  
  
    /** 
     *  
     */  
    private static final long serialVersionUID = 9024481738536854407L;  
  
    public UserModel() {  
        super();  
    }  
  
    public UserModel(String userName, int age) {  
        super();  
        this.userName = userName;  
        this.age = age;  
    }  
  
    /** 
     * 用户名 
     */  
    private String userName;  
    /** 
     * 用户年龄 
     */  
    private int age;  
  
    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;  
    }  
  
    @Override  
    public String toString() {  
        return "UserModel [userName=" + userName + ", age=" + age + "]";  
    }  
}

上边这些代码和之前的比较,除开加了一些注解外,基本上没有多大区别,然后启动服务端,并在模拟的客户端中调用,结果控制台的打印信息却不是预想中的,都成了默认值,如图:


zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz.png

于是又查了许多资料,并在项目老大的帮助下找到了原因。竟然是因为我客户端的UserModel类的包名和服务端的不一样,于是新建了一个和服务端一样的包,并把这个UserModel挪过去,然后再启动模拟客户端的main方法,控制台如愿以偿的输出结果,至此整个测试完毕

ggggggggggggg.png



         转载自:http://blog.csdn.net/tuzongxun/article/details/51612367

Home