Apache CXF Combat eight map type binding

Source: Internet
Author: User
Tags assert static class

This article link: http://blog.csdn.net/kongxx/article/details/7544640

Apache CXF One of the actual combat Hello World Web Service

Apache CXF Combat II Integrated sping and Web container

Apache CXF Three-combat Transfer Java objects

Apache CXF Real-combat four build restful Web Service

Apache CXF Combat Five compressed Web service data

Apache CXF Combat Six Create a secure Web Service

Apache CXF Combat Seven use the Web service to transfer files

In CXF, if the Web service return type is map, for example, the method signature is as follows

    @WebMethod
    @WebResult map<string, user> getusermap ();
At this point, if you run the program, you get an exception like the following
...
caused By:com.sun.xml.bind.v2.runtime.illegalannotationsexception:2 counts of illegalannotationexceptions
    Java.util.Map is a interface, and JAXB can ' t handle interfaces. This problem was related to the following Location:at Java.util.Map at private java.util.Map com.googlecod E.garbagecan.cxfstudy.type.sample2.jaxws_asm. Getusermapresponse._return at Com.googlecode.garbagecan.cxfstudy.type.sample2.jaxws_asm.
    Getusermapresponse Java.util.Map does not have a No-arg default constructor. This problem was related to the following Location:at Java.util.Map at private java.util.Map com.googlecod E.garbagecan.cxfstudy.type.sample2.jaxws_asm. Getusermapresponse._return at Com.googlecode.garbagecan.cxfstudy.type.sample2.jaxws_asm. Getusermapresponse at Com.sun.xml.bind.v2.runtime.illegalannotationsexception$builder.check ( illegalannotationsexception.java:102) at Com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet (Jaxbcontextimpl.java:472) at com.sun.xml.bind.v2.runtime.jaxbcontextimpl.<init> (jaxbcontextimpl.java:302) at Com.sun.xml . Bind.v2.runtime.jaxbcontextimpl$jaxbcontextbuilder.build (jaxbcontextimpl.java:1136) at Com.sun.xml.bind.v2.ContextFactory.createContext (contextfactory.java:154) at Com.sun.xml.bind.v2.ContextFactory.createContext (contextfactory.java:121) at Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native method) at Sun.reflect.NativeMethodAccessorImpl.invoke ( Unknown source) at Sun.reflect.DelegatingMethodAccessorImpl.invoke (Unknown source) at JAVA.LANG.REFLECT.METHOD.INV Oke (Unknown source) at Javax.xml.bind.ContextFinder.newInstance (Unknown source) at Javax.xml.bind.ContextFinder.ne Winstance (Unknown source) at Javax.xml.bind.ContextFinder.find (Unknown source) at Javax.xml.bind.JAXBContext.newIn Stance (Unknown Source) at Org.apache.cxf.jaxb.JAXBDataBinding.createContext (jaxbdatabinding.java:560) at Org.apach E.cxf.jaxb.jaxbdatabindinG.createjaxbcontextandschemas (jaxbdatabinding.java:500) at Org.apache.cxf.jaxb.JAXBDataBinding.initialize ( jaxbdatabinding.java:320) ... More ...
If you change the method signature to the following

    @WebMethod
    @WebResult hashmap<string, user> getusermap ();
Running the program will find that the returned result is always an empty hashmap.


For the above question, you need to do some data binding or conversion for the map type, see the detailed example below

1. First is an entity class

Package com.googlecode.garbagecan.cxfstudy.type.sample2;

public class User {
    private String ID;
    private String name;
    private String password;
    Public String GetId () {return
        ID;
    }
    public void SetId (String id) {
        this.id = ID;
    }
    Public String GetName () {return
        name;
    }
    public void SetName (String name) {
        this.name = name;
    }
    Public String GetPassword () {return
        password;
    }
    public void SetPassword (String password) {
        this.password = password;
    }
}
2. Class and adapter classes that convert to map types

Package com.googlecode.garbagecan.cxfstudy.type.sample2;
Import Java.util.HashMap;

Import Java.util.Map;

Import Javax.xml.bind.annotation.adapters.XmlAdapter; public class Mapadapter extends Xmladapter<mapconvertor, map<string, object>> {@Override public MAPC
        Onvertor Marshal (map<string, object> Map) throws Exception {Mapconvertor convertor = new Mapconvertor (); For (map.entry<string, object> entry:map.entrySet ()) {Mapconvertor.mapentry e = new Mapconvert Or.
            Mapentry (entry);
        Convertor.addentry (e);
    return convertor; @Override public map<string, object> unmarshal (Mapconvertor Map) throws Exception {map<string
        , object> result = new hashmap<string, object> ();
        For (Mapconvertor.mapentry e:map.getentries ()) {Result.put (E.getkey (), E.getvalue ());
    return result; }} Package Com.googlecode.garbagecan.cxfstudy.typE.sample2;
Import java.util.ArrayList;
Import java.util.List;

Import Java.util.Map;
Import Javax.xml.bind.annotation.XmlAccessType;
Import Javax.xml.bind.annotation.XmlAccessorType;

Import Javax.xml.bind.annotation.XmlType; @XmlType (name = "Mapconvertor") @XmlAccessorType (Xmlaccesstype.field) public class Mapconvertor {private List<mape

    ntry> entries = new arraylist<mapentry> ();
    public void AddEntry (Mapentry entry) {Entries.Add (entry);
    Public list<mapentry> GetEntries () {return entries;

        public static class Mapentry {private String key;
        
        private Object value;
        Public Mapentry () {super ();
            Public Mapentry (map.entry<string, object> Entry) {super ();
            This.key = Entry.getkey ();
        This.value = Entry.getvalue ();
            Public Mapentry (String key, Object value) {super (); This.key = Key
        This.value = value;
        Public String Getkey () {return key;
        public void Setkey (String key) {this.key = key;
        Public Object GetValue () {return value;
        The public void SetValue (Object value) {this.value = value; }
    }
}
3. The following is the WebService interface class, note the @xmljavatypeadapter annotation section

Package com.googlecode.garbagecan.cxfstudy.type.sample2;

Import java.util.List;
Import Java.util.Map;

Import Javax.jws.WebMethod;
Import Javax.jws.WebResult;
Import Javax.jws.WebService;
Import Javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@WebService Public
interface UserService {

    @WebMethod
    @WebResult list<user> getuserlist ();

    @WebMethod
    @XmlJavaTypeAdapter (mapadapter.class)
    @WebResult map<string, user> getusermap ();
}
4. WebService interface Implementation Class

Package com.googlecode.garbagecan.cxfstudy.type.sample2;
Import java.util.ArrayList;
Import Java.util.LinkedHashMap;
Import java.util.List;

Import Java.util.Map; public class Userserviceimpl implements UserService {public list<user> getuserlist () {List<user&gt ;
        userlist = new arraylist<user> ();
            for (int i = 0; i < i++) {User user = new user ();
            User.setid ("" + i);
            User.setname ("User_" + i);
            User.setpassword ("Password_" + i);
        Userlist.add (user);
    return userlist; Public map<string, User> Getusermap () {map<string, user> userMap = new linkedhashmap<string
        , user> ();
            for (int i = 0; i < i++) {User user = new user ();
            User.setid ("" + i);
            User.setname ("User_" + i);
            User.setpassword ("Password_" + i);
        Usermap.put (User.getid (), user); Return to useRMap;
 }
}
5. Finally, a unit test class

Package com.googlecode.garbagecan.cxfstudy.type.sample2;
Import java.util.List;

Import Java.util.Map;

Import Javax.xml.ws.Endpoint;
Import Org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
Import Org.junit.After;
Import Org.junit.Assert;
Import Org.junit.Before;
Import Org.junit.BeforeClass;

Import Org.junit.Test; public class Userservicetest {private static final String address = "Http://localhost:9000/ws/type/sample2/userServi
    
    Ce ";
    
    Private UserService UserService; @BeforeClass public static void Setupbeforeclass () throws Exception {endpoint.publish (address, new Userservic
    Eimpl ()); @Before public void SetUp () throws Exception {Jaxwsproxyfactorybean Factorybean = new Jaxwsproxyfa
        Ctorybean ();
        Factorybean.setaddress (address);
        Factorybean.setserviceclass (Userservice.class);
        Object obj = Factorybean.create ();
    UserService = (userservice) obj; @After public void teardown () throws Exception {userservice = null;
        @Test public void Testgetuserlist () {assert.assertnotnull (userservice);
        list<user> users = Userservice.getuserlist ();
        Assert.assertnotnull (users);
    Assert.assertequals (Users.size ());
        @Test public void Testgetusermap () {assert.assertnotnull (userservice);
        map<string, user> users = Userservice.getusermap ();
        Assert.assertnotnull (users);
    Assert.assertequals (Users.size ());
 }

}
6. Run the Unit test class to verify the implementation above.





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.