JAX-WS: How to make a SOAP Response return a HashMap object

前端 未结 3 1095
天涯浪人
天涯浪人 2020-12-10 17:34

So I have a simple web service:

    @WebMethod(operationName=\"getBookList\")
    public HashMap getBookList()
    {
        HashMap

        
相关标签:
3条回答
  • 2020-12-10 18:00

    In order to help JAXB, you can 'wrap' your HashMap in a class and use the @XmlJavaTypeAdapter to make your custom serialization of the map to XML.

    public class Response {
    
        @XmlJavaTypeAdapter(MapAdapter.class)    
        HashMap<Integer, Book> books;
    
        public HashMap<Integer, Book> getBooks() {
            return mapProperty;
        }
    
        public void setBooks(HashMap<Integer, Book> map) {
            this.mapProperty = map;
        }
    
    }
    

    Then use this class as a return value of your WebMethod

    @WebMethod(operationName="getBookList")
        public Response getBookList()
        {
             HashMap<Integer, Book> books = new HashMap<Integer,Book>();
             Book b1 = new Book(1,"title1");
             Book b2 = new Book(2, "title2");
             books.put(1, b1);
             books.put(2, b2);
             Response resp = new Response();
             resp.setBooks(books);
             return resp;
        }
    

    After all, you need to implement your adapter MapAdapter. There is several ways to do this, so I recommend you to check this

    0 讨论(0)
  • 2020-12-10 18:19

    JAX-WS How to make SOAP Response return Hashmap object

    You should not expose any Java specific constructs like HashMap via a Web Service.
    Web Services is about interoperability and following paths like yours is the wrong way.
    Just return the information required so that the web service client can build the hash table regardless of the programming language it is written

    0 讨论(0)
  • 2020-12-10 18:26

    On JBoss Forum I found solution, which works for me on Glassfish. Original solution is on JBoss Forum, topic from Allesio Soldano. It consists from one auxiliary class, which has a HashMap as nested type i.e. HashMap<String, String>. Than in web service Class this auxiliary class is used as returning value. The annotation @XmlAccessorType(XmlAccessType.FIELD) ensures, that structure will be properly treated by SOAP in SOAP Response.

    @XmlAccessorType(XmlAccessType.FIELD)
    public class MyHash {
      protected HashMap<String,String> realMap;
    
      // constructor
      public MyHash() {
        realMap = new HashMap<String,String>();
      }
    
      /**
       * @return HashMap<String,String>
       */
      public HashMap<String,String> getRealMap() {  
        if (realMap==null) {  
          realMap = new HashMap<String,String>();  
        }
        return realMap;  
      }
    
      /**
       * @param key
       * @param value
       */
      public void put(String key, String value) {
        realMap.put(key, value);
      }
    }
    

    In Webservice use this class directly as a return object without any additional settings. Of course, the object must be first created and map should be filled similarly as in another POJO.

    If HashMap consists from another non primitive types (objects), I proofed, that it is possible to recursively use the same manner on the nested complex objects. The rule is, that class is not inherited i.e. it must be nested as attribute and the last class has all attributes primitive.

    0 讨论(0)
提交回复
热议问题