WebService CXF Learning (Advanced Chapter 2): Complex object Transfer (LIST,MAP) _CXF

Source: Internet
Author: User

The passing of the JavaBean object is described earlier in this section, which we explain the delivery of the CXF framework complex object (LIST,MAP). &NBSP
First step: Create a class that stores complex objects (because WebServices's complex objects are passed, it must be implemented with Third-party objects (that is, custom objects) [java]  View Plain copy package  com.ws.model;      import java.util.arraylist;   Import java.util.hashmap ;      public class users {       private  arraylist<userinfo> userlist;  //  Do not use list type         private hashmap<integer, userinfo> usermap; //  do not use map type         private UserInfo[] userArray;       // geter/seter   Method   }  

Step Two: Create WebServices server-side interface and implementation class [java]  view plain copy package com.ws.services;      import javax.jws.webservice;   import com.ws.model.userinfo;   import  com.ws.model.users;      @WebService    public interface iuserservices  {          public users getallusers ();               public users getusersmap ();               public users getusersarray ();     }   [java]  View plain copy package com.ws.services.impl;      import java.util.arraylist;   import java.util.hashmap;   import  javax.jws.webservice;   import com.ws.model.userinfo;   import com.ws.model.Users;    Import com.ws.services.iuserservices;      @WebService    public class  UserServicesImpl implements IUserServices {           public users getallusers ()  {            Arraylist<userinfo> list = new arraylist<userinfo> ();            list.add (New userinfo ("Vicky"));            list.add (New userinfo ("Ivy");            list.add (New userinfo ("Simon"));            list.add (New userinfo ("Carol"));            users users = new users ();            users.setuserlist (lIST);           return users;        }          public users getusersmap ()  {            hashmap<integer, userinfo> map =  new HashMap<Integer, UserInfo> ();            map.put (23, new userinfo ("Vicky"));            map.put (22, new userinfo ("Caty");            map.put (24, new userinfo ("Leah");            map.put (25, new userinfo ("Kelly");            map.put (27, new userinfo ("Ivy"));            map.put (26,&NBSp;new userinfo ("Simon"));           map.put (29,  new userinfo ("Carol");                      users users = new users ();           users.setusermap (map);            return users;       }           public users getusersarray ()  {            UserInfo[] userInfo = new UserInfo[5];            for (int i=0;i<5;i++) {                userinfo info = new userinfo ();                info.setuserage (23+i);                info.setusername ("Array" + (i+1));                userInfo[i] = info;            }           users users =  new users ();           users.setuserarray (userInfo );           return users;        }     }  

Step three: Create the webservices server [java]  view plain copy package com.test;      import  javax.xml.ws.endpoint;   import org.apache.cxf.jaxws.jaxwsserverfactorybean;   Import  com.ws.services.impl.UserServicesImpl;      public class servertest {        public servertest () {            //  Publishing User Service Interface            endpoint.publish (" Http://localhost:8090/userInfoServices ",  new userservicesimpl ());        }       public static void main (String[] args)  {            //  Startup Services             new servertest ();            System.out.println ("Server ready ...");              try  {               thread.sleep (1000* 300)//Hibernate five minutes, easy to test             } catch  ( Interruptedexception e)  {                e.printstacktrace ();           }               system.out.println ("Server exit ...");               system.exit (0);       }  }  

Step Fourth: Create the WebServices client and test
1. Copy the class and interface of the complex object created by the server to the customer project, and the path should be consistent with the service end;
    2, new test class for testing [java]  view plain copy package com.ws.client;      import java.util.list;   import org.apache.cxf.jaxws.jaxwsproxyfactorybean;   import com.ws.model.userinfo;   import com.ws.model.users;   import  com.ws.server.iuserservices;      public class usertest {        public static void main (String[] args)  {      &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//Create WebService Client Agent factory                jaxwsproxyfactorybean factory = new jaxwsproxyfactorybean ();               //Registration WebService Interface                factory.setserviceclass (Iuserservices.class) ;               //Set WebService address                factory.setaddress ("Http://localhost:8090/userInfoServices");                     iuserservices userservices =  (iuserservices) factory.create ();              system.out.println ("Invoke userinfo webservice ...");            //  Test map  //       testmap (userservices);           //  Test list  //      testlist (userservices);            //  Test array  //      testarray ( Userservices);          system.exit (0);           }               public static  void testarray (iuserservices userservices) {            users users = userservices.getusersarray ();            if (users!=null) {                userinfo[] array = users.getuserarray ();                for (Userinfo info:array) {                    system.out.println ("UserName:   "+info.getusername ());                   &nbsP System.out.println ("userage : " +info.getuserage ());                }           }       }              public  Static void testlist (iuserservices userservices) {            users users = userservices.getallusers ();            if (users!=null) {                list<userinfo> list = users.getuserlist ();                for (userinfo info:list) {                    system.out.println (" username:  "+info.getusername ());                    system.out.println ("userage : " +info.getuserage ());                }            }       }               public static void testmap (iuserservices userservices) {            users users = userservices.getusersmap ();           if (users!=null) {                userinfo info = users.getusermap ();                system.out.println ("UserName:   "+info.getusernamE ());               system.out.println (" userage :  "+info.getuserage ());           }        }  }  

Step Fifth: Run the service side to verify that the WebServices service is published successfully
Step Sixth: Run the client to verify that the WebServices service is successfully invoked

Note: The type of the return value does not use the interface type when making WebServices complex object delivery. For example (List should be replaced with ArrayList, map should be replaced with HashMap)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.