在寫一個webservice的時候,方法的返回值是一個復雜類型,處理方法是寫一個結果類(Javabean)作為返回值。想着webservice方法返回值為Map的沒寫過,然后就試着寫了一個簡單的Demo。出錯了...那我就來勁了,總有辦法解決吧....
通過百度(你有Google癖好就用Google吧)。。找到方法,通過前輩們的經驗找到解決方法。
業內人士都懂!注重版權,奉上原文鏈接:
http://blog.csdn.net/jspamd/article/details/8914427
貼上自個Demo代碼之前,補充個知識點,webservice中發布方法的參數以及返回值可以很好的處理基本類型,POJO類,數組,以及list集合等復雜類型,但是在處理Map,非JavaBean式的類,我們需要自定義一個轉換器,負責將webservice中不能處理的類型轉換為可以處理的類型
(1)需要使用注解@XmlJavaTypeAdapter修飾返回類型
1 @WebService 2 public interface HelloService { 3 public String sayHello(String name); 4 public String sayGoodBy(String name); 5 public String sayHello2(String name); 6 7 public @XmlJavaTypeAdapter((XmlMapAdapter.class)) Map<String, String> getSpace(String name); 8 }
(2)自定義一個可以替代不可處理參數類型的類(可以理解為模擬Map接口的類,這個類是POJO)
1 public class MyStringMap { 2 private List<Entry> entries; 3 4 /** 5 * @return entries 6 */ 7 public List<Entry> getEntries() { 8 return entries; 9 } 10 11 /** 12 * @param entries the entries to set 13 */ 14 public void setEntries(List<Entry> entries) { 15 this.entries = entries; 16 } 17 18 public static class Entry { 19 private String key; 20 private String value; 21 /** 22 * @return key 23 */ 24 public String getKey() { 25 return key; 26 } 27 /** 28 * @param key the key to set 29 */ 30 public void setKey(String key) { 31 this.key = key; 32 } 33 /** 34 * @return value 35 */ 36 public String getValue() { 37 return value; 38 } 39 /** 40 * @param value the value to set 41 */ 42 public void setValue(String value) { 43 this.value = value; 44 } 45 46 } 47 }
(3)自定義一個轉換器(作為@XmlJavaTypeAdapter注解中value)
1 public class XmlMapAdapter extends XmlAdapter<MyStringMap, Map<String, String>>{ 2 @Override 3 //Map(不可處理)轉換為可處理的類(自定義的POJO類,就是模擬Map的一個類) 4 public MyStringMap marshal(Map<String, String> v) throws Exception { 5 MyStringMap result = new MyStringMap(); 6 List<pojo.MyStringMap.Entry> entries = new ArrayList<MyStringMap.Entry>(); 7 for(Entry<String, String> e : v.entrySet()){ 8 pojo.MyStringMap.Entry entry = new pojo.MyStringMap.Entry(); 9 entry.setKey(e.getKey()); 10 entry.setValue(e.getValue()); 11 entries.add(entry); 12 } 13 result.setEntries(entries); 14 return result; 15 16 } 17 //自定義可處理類轉換為Map(不可處理的類型) 18 @Override 19 public Map<String, String> unmarshal(MyStringMap v) throws Exception { 20 Map<String, String> result = new HashMap<String, String>(); 21 for(pojo.MyStringMap.Entry e : v.getEntries()){ 22 result.put(e.getKey(), e.getValue()); 23 } 24 return result; 25 } 26 }
(4)接口的實現類
1 @WebService(endpointInterface="com.webservice.HelloService",serviceName="MyService",targetNamespace="http://www.baidu.com") 2 public class HelloServiceImpl implements HelloService { 3 4 @WebMethod(operationName="AliassayHello") 5 @WebResult(name="myReturn") 6 @Override 7 public String sayHello(@WebParam(name="name")String name) { 8 System.out.println("Hello,"+name); 9 return "Hello,"+name; 10 } 11 12 @Override 13 public String sayGoodBy(@WebParam(name="name")String name) { 14 System.out.println("GoodBy,"+name); 15 return "GoodBy,"+name; 16 } 17 18 @WebMethod(exclude=true)//不會被發布出去 19 @Override 20 public String sayHello2(String name) { 21 System.out.println("hello2"+ name); 22 return "Hello2,"+name; 23 } 24 25 @Override 26 public Map<String, String> getSpace(String name) { 27 HashMap<String, String> resultMap = new HashMap<String,String>(); 28 29 resultMap.put("age", "12"); 30 resultMap.put("name", name); 31 resultMap.put("orid", "123123"); 32 resultMap.put("address", "北京"); 33 34 System.out.println(resultMap); 35 return resultMap; 36 } 37 38 public static void main(String[] args) { 39 Endpoint.publish("http://127.0.0.1:8099/hello", new HelloServiceImpl()); 40 System.out.println("服務發布成功!"); 41 } 42 43 }
剩下的就是測試工作,這里就沒寫客戶端程序去測試了,使用SoapUI測試下
soap請求消息:
soap響應
個人理解webservice中的轉換器的思想機制和struts2 中的類型轉換機制是一個樣的。
朋友,看我這么帥,點個贊唄