Learn WebService's CXF (5): Cxf dealing with complex types such as map

Source: Internet
Author: User

Tagged with: str hal for adaptation role size client DMI the


caused By:com.sun.xml.internal.bind.v2.runtime.illegalannotationsexception:1 counts of IllegalAnnotationExceptionsjava.util.List is an interface, and JAXB cannot handle the interface. This problem is related to the following location: at java.util.List private Java.util.Map Com.rg2.webservice.jaxws_asm. Getrolesresponse._return at Com.rg2.webservice.jaxws_asm. Getrolesresponse

It is necessary to annotate the CXF when processing the map type, otherwise the above error will be reported.

@WebService  Public Interface HelloWorld {    public  string Say (string str);          Public List<role> getrolebyuser (user user);         /**      * Get the     role of all users @return     *    /@XmlJavaTypeAdapter ( Mapadapter. class )    public map<string, list<role>> getroles ();} 

Where Mapadapter is written separately

 PackageCom.rg2.adapter;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;ImportJavax.xml.bind.annotation.adapters.XmlAdapter;ImportCom.rg2.entity.MyRole;ImportCom.rg2.entity.Role; Public classMapadapterextendsXmladapter<myrole[], map<string, list<role>>>{    /*** Adapter Conversion myrole[]-map<string, list<role>>*/@Override PublicMap<string, list<role>> unmarshal (myrole[] v)throwsException {Map<string, list<role>> map =NewHashmap<string,list<role>>();  for(inti = 0; i < v.length; i++) {myrole role=V[i];        Map.put (Role.getkey (), Role.getvalue ()); }        returnmap; }    /*** Adapter Conversion map<string, list<role>>-to-fit conversion myrole[]*/@Override PublicMyrole[] Marshal (map<string, list<role>> v)throwsException {myrole[] roles=Newmyrole[v.size ()]; inti = 0;  for(String key:v.keyset ()) {Roles[i]=NewMyrole ();            Roles[i].setkey (key);            Roles[i].setvalue (V.get (key)); I++; }        returnroles; }}
 Packagecom.rg2.entity;Importjava.util.List;/*** Custom Entity CXF can accept *@authorAdministrator **/ Public classMyrole {PrivateString Key; PrivateList<role>value;  PublicString GetKey () {returnkey; }     Public voidSetkey (String key) { This. Key =key; }     PublicList<role>GetValue () {returnvalue; }     Public voidSetValue (list<role>value) {         This. Value =value; }}

Start the server method

Open the browser without error

Automatically generate code from clients using WSDL2, as previously written

The server-side interface is then invoked through the client class

 PackageCom.rg2.webservice;Importjava.util.List; Public classClient { Public Static voidMain (string[] args) {HelloWorldService service=NewHelloWorldService (); HelloWorld Helloworldport=Service.gethelloworldport (); Myrolearray GetRoles=Helloworldport.getroles (); List<MyRole> rolelist =Getroles.item;  for(inti = 0; I < rolelist.size (); i++) {Myrole my=Rolelist.get (i); System.out.print (My.key+ ":");  for(Role role:my.value) {System.out.print (Role.getid ()+","+role.getrolename ()); } System.out.println ("==============="); }    }}

The result of the operation is:

Learn WebService's CXF (5): Cxf dealing with complex types such as map

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.