1、创建web项目后先编辑web.xml配置文件加入以下内容
(底部附下载链接)

<servlet>
    <servlet-name>XFireServlet</servlet-name>
    <servlet-class>
org.codehaus.xfire.transport.http.XFireConfigurableServlet
    </servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>XFireServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>

2、在src目录下创建文件夹META-INF/xfire,然后在改文件夹下创建services.xml配置文件,内容如下这里写图片描述

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
    <service>
        <name>UserService</name>
        <!-- <namespace>http://XFireWebService.com/UserService</namespace> -->
        <serviceClass>com.ht.service.UserService</serviceClass>
        <implementationClass>com.ht.service.impl.UserServiceImpl</implementationClass>
    </service>
</beans>

3、创建一个javabean(内容自定义)

public class User {
    private Integer id;
    private String name;
    private String pwd;
    private int age;
    private String sex;
    //以下内容省略,都是get set方法
    ·
    ·
    ·
}

4、写一个接口类

public interface UserService {
    public boolean login(String username,String password);
    public List<User> getAllUser();
    public User findUserById(int id);
}

5、实现这个接口

public class UserServiceImpl implements UserService{

    @Override
    public boolean login(String username, String password) {
        if(username != null && !"".equals(username) && password != null && !"".equals(password)){
            return true;
        }
        return false;
    }

    @Override
    public List<User> getAllUser() {
        List<User> users = new ArrayList<>();
        User u = new User();
        u.setId(112);
        u.setName("张三");
        u.setPwd("123456");
        u.setAge(20);
        u.setSex("男");

        User u2 = new User();
        u2.setId(113);
        u2.setName("李四");
        u2.setPwd("123456");
        u.setAge(19);
        u.setSex("女");

        users.add(u);
        users.add(u2);
        return users;
    }

    @Override
    public User findUserById(int id) {
        User u = new User();
        u.setId(id);
        u.setName("张三");
        u.setPwd("123456");
        return u;
    }

到这里基本上已经完成了,接下来就是测试了,写一个测试类

public class TestServices {
    public static void main(String[] args) {
        Service srvcModel = new
        ObjectServiceFactory().create(UserService.class);
        XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
        String quoteURL = "http://127.0.0.1/WebService/services/UserService";
        try {
            UserService service = (UserService)factory.create(srvcModel, quoteURL);
            //调用登录方法
            boolean bln = service.login("username", "123456");
            System.out.println("bln = "+bln);

            //用户列表
            List<User> list=service.getAllUser();
            for(User u:list){
                System.out.println("*:"+u);
            }

            //id查询
            User user=service.findUserById(113);
            System.out.println("user:"+user);

        } catch (MalformedURLException e) {
           e.printStackTrace();
        }
    }

先启动服务器,再运行这个类,OK就可以看到效果了
下载:http://download.csdn.net/detail/askycat/9692167

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐