CXF 发布 Web Service

来源:互联网 发布:百度网盘无限试用 mac 编辑:程序博客网 时间:2024/06/15 01:29

使用CXF框架开发

①.CXF : xfire–>xfire + celtrix
做web service开发的开源框架

②.开发Server端:
加入cxf的Jar包即可,其它不需要动

测试CXF支持的数据类型
1.基本类型
–int,float,boolean等
2.引用类型
–String
–集合:数组,List, Set, Map
–自定义类型 Student




@WebService
public class DataTypesImpl implements DataTypeWS {  //SEI接口实现类

    public boolean addStudent(Student s) {
        System.out.println("server addStudent()" +s);
        return true;
    }

    public Student getStudentById(int id) {
        System.out.println("server  getStudentById()" +id);
        return new Student(id,"CAT",1000);
    }

    public List<Student> getStudentByPrice(float price) {
        System.out.println("server  getStudentByPrice()" +price);
        List<Student> list=new ArrayList<Student>();
        list.add(new Student(1,"tg1",price+1));
        list.add(new Student(2,"tg2",price+2));
        list.add(new Student(3,"tg3",price+3));
        return list;
        
        
    }

    public Map<Integer, Student> getAllStudentMap() {
        System.out.println("server getAllStudentMap()" );
        Map<Integer,Student> map=new HashMap<Integer, Student>();
        map.put(1,new Student(1,"TG1",123));
        map.put(2,new Student(2,"TG2",143));
        map.put(3,new Student(3,"TG3",153));
        
        return map;
    }


public class serviceTest2 {  //发布服务

    /**
     * @param args
     */
    public static void main(String[] args) {
        //客户端发送web service请求的url
        String address="http://127.0.0.1:8888/tg_ws_cxf/datatypews";
        //处理请求的SEI对象
        DataTypeWS dataTypesImpl=new DataTypesImpl();
        //发布web service
        //Endpoint.publish("http://127.0.0.1/person_ws/HelloWS", hellows);
    Endpoint.publish(address, dataTypesImpl);
    
        
        
        System.out.println("web service 发布成功");

    }

--------------------------------------------------------------------------

(中间必须先生成客户端代码   打开cmd .. 见我的上一篇博客http://blog.csdn.net/tanggao1314/article/details/48393205    图2


public class ClientTest {   //客户端测试

    /**
     * @param args
     */
    public static void main(String[] args) {
        DataTypesImplService factory=new DataTypesImplService();
        DataTypeWS dataTypeWS=factory.getDataTypesImplPort();
        
        boolean s=dataTypeWS.addStudent(new Student());
        System.out.println(s);
        
        List<Student> list=dataTypeWS.getStudentByPrice(12);
        System.out.println(list);
        
        Return r=dataTypeWS.getAllStudentMap();
        
        List<com.tg.web.service.GetAllStudentMapResponse.Return.Entry> entrys=r.getEntry();
        
        for(com.tg.web.service.GetAllStudentMapResponse.Return.Entry entry:entrys){
            Integer id=entry.getKey();
            Student student=entry.getValue();
            System.out.println(id+"-"+student);
        }
        
        

    }


1 0