Web Service Learning Vi: CXF resolving data types that cannot be processed

Source: Internet
Author: User
Tags ming

CXF is not able to handle data types such as map complexity and requires a separate conversion process.

General Idea: Create a converter and a corresponding type of data structure that can be processed, converting types that cannot be processed into types that can be processed:

Steps:

first, Create an example of a type that can be handled: to convert map<string,user>

 packagews;Importjava.util.List; public classStringuser { public Static classEntry {PrivateString key; PrivateUser value;  publicString GetKey () {returnkey; }         public voidSetkey (String Key) { this. Key =key; }         publicUser getValue () {returnvalue; }         public voidsetValue (User Value) { this. Value =value; }    }    PrivateList<entry>values;  publicList<entry>getValues () {returnvalues; }     public voidSetvalues (list<entry>values) {         this. Values =values; }        }

second, to create a converter, you need to inherit the abstract class xmladapter<valuetype,boundtype>, where ValueType is a type that can be handled boundtype is a type that cannot be processed

 packagews;Importjava.util.ArrayList;Importjava.util.HashMap;Importjava.util.List;Importjava.util.Map;Importjavax.xml.bind.annotation.adapters.XmlAdapter;ImportWs. stringuser.entry;/*** The converter is responsible for converting stringuser with map<string, user> * where the first parameter stringuser is the type that the CXF can handle * The second parameter map<string, user> is CXF Types that cannot be processed*/ public classFkxmladapterextendsxmladapter<stringuser, map<string, user>> {    /*** Convert Stringuser into map<string, user>*/@Override publicmap<string, user> unmarshal (stringuser v)throwsException {Map<string, user> map =Newhashmap<string, user>();  for(Entry u:v.getvalues ()) {map.put (u.getkey (), u.getvalue ()); }        returnmap; }    /*** Convert map<string, user> into Stringuser*/@Override publicStringuser Marshal (map<string, User> V)throwsException {stringuser u=NewStringuser (); List<Entry> list =NewArraylist<entry>();  for(String key:v.keyset ()) {Entry e=NewEntry ();            E.setkey (key);            E.setvalue (v.get (key));        List.add (e);        } u.setvalues (list); returnu; }}

third, the method defined in the Web service needs to use the annotation @XmlJavaTypeAdapter (value=fkxmladapter.class) to indicate the processing converter corresponding to the return value

@WebMethod (operationname= "getsecusers") @WebResult (name= "returninfo")public@ Xmljavatypeadapter (value=fkxmladapter.class) map<string, user> getsecusers ();

Iv. test the following to view the WSDL document return data types are resolvable types

Service-side Implementation Class:

 packagews.impl;Importjava.util.ArrayList;Importjava.util.Date;Importjava.util.HashMap;Importjava.util.List;Importjava.util.Map;Importjavax.jws.WebService;ImportWs. hellowordi;ImportWs. User;/*** Class Description *@authorWangjunyu * @createDate 2016-7-10 a.m. 10:59:27 *@versionV1.0*/@WebService (endpointinterface= "ws." Hellowordi ", servicename=" Hellowordimpl ") public classHellowordimplImplementsHellowordi { publicstring Sayhi (string Name) {returnname+ "hello, The current time is:" +NewDate (); }     publicList<user>getusers (user user) {System.out.println ("test:" +User.getname ()); List<User> list =NewArraylist<user>(); User U1=NewUser (); U1.setid ("1"); U1.setname ("xiao ming");        List.add (u1); User U2=NewUser (); U2.setid ("2"); U2.setname ("snow plum");        List.add (u2); returnlist; } @Override publicmap<string, user>getsecusers () {Map<String,User> map =NewHashmap<string,user>(); User U1=NewUser (); U1.setid ("1"); U1.setname ("xiao ming"); Map.put (A, u1); User U2=NewUser (); U2.setid ("2"); U2.setname ("little red"); Map.put (Two, u2); returnmap; }    }
 package ws; Import javax.xml.ws.Endpoint; Import ws.impl.HelloWordImpl;  public class servermain {    publicstaticvoid  main (string[] Args)     {                 New  Hellowordimpl ();        Endpoint.publish ("http://192.168.0.104/test", hw);        SYSTEM.OUT.PRINTLN ("WebService exposed successfully! ");    }}

Client Print:

 packagews;Importjava.util.List;Importws.impl.HelloWordImpl; public classClientmain {/**     * @paramargs*/     public Static voidmain (string[] Args) {stringuser u=hw.getsecusers (); System.out.println (u.getvalues (). Get (0). getValue (). getName ()); System.out.println (u.getvalues (). Get (1). getValue (). getName ()); }}

Web Service Learning Vi: CXF resolving data types that cannot be processed

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.