CXF处理形参,返回值类型:
1:当形参,返回值的类型是String,基本数据类型时,CXF可以处理。
2:当形参,返回值类型是JavaBean式的复合类,List集合,数组等时,CXF可以处理
3:当形参返回值是Map,非JavaBean式的复合类,CXF自身不会按原来的类型返回,会经过转换来处理。
@XmlJavaTypeAdapter来注释到不能处理的类型
例如:
1:
@WebService public interface TestWebservice { public abstract void say(); public String login(String userName, String password); //@XmlJavaTypeAdapter来注释到不能处理的类型 AdapterListMap类为转换处理的类 public @XmlJavaTypeAdapter(AdapterListMap.class) List<Map<String, Object>> selectProjectInfo(String param); }
2:
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.xml.bind.annotation.adapters.XmlAdapter; import dj.log.controller.MapEntry.Entry; //需要继承XmlAdapter类 public class AdapterListMap extends XmlAdapter<ListMap, List<Map<String, Object>>>{ @Override public List<Map<String, Object>> unmarshal(ListMap v) throws Exception { List<Map<String, Object>> list=new ArrayList<Map<String,Object>>(); for(MapEntry mapEntry:v.getList()){ Map<String, Object> map=new HashMap<String, Object>(); for(Entry entry:mapEntry.getEntries()){ map.put(entry.getKey(), entry.getValue()); } list.add(map); } return list; } @Override public ListMap marshal(List<Map<String, Object>> v) throws Exception { ListMap listMap=new ListMap(); for(Map<String, Object> map:v){ MapEntry mapEntry=new MapEntry(); for(String key:map.keySet()){ Entry entry=new Entry(); entry.setKey(key); entry.setValue(map.get("key")); mapEntry.getEntries().add(entry); } } return listMap; } }
3:其他相关的类根据具体的逻辑来处理
package dj.log.controller; import java.util.List; public class MapEntry { private List<Entry> entries; public List<Entry> getEntries() { return entries; } public void setEntries(List<Entry> entries) { this.entries = entries; } public static class Entry { private String key; private Object value; public String getKey() { return key; } public void setKey(String key) { this.key = key; } public Object getValue() { return value; } public void setValue(Object value) { this.value = value; } } }
package dj.log.controller; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import dj.log.controller.MapEntry.Entry; public class ListMap { private List<MapEntry> list; public List<MapEntry> getList() { return list; } public void setList(List<MapEntry> list) { this.list = list; } public List<Map<String, Object>> unmarshal(ListMap v) throws Exception { List<Map<String, Object>> list=new ArrayList<Map<String,Object>>(); for(MapEntry mapEntry:v.getList()){ Map<String, Object> map=new HashMap<String, Object>(); for(Entry entry:mapEntry.getEntries()){ map.put(entry.getKey(), entry.getValue()); } list.add(map); } return list; } public ListMap marshal(List<Map<String, Object>> v) throws Exception { ListMap listMap=new ListMap(); for(Map<String, Object> map:v){ MapEntry mapEntry=new MapEntry(); for(String key:map.keySet()){ Entry entry=new Entry(); entry.setKey(key); entry.setValue(map.get(key)); mapEntry.getEntries().add(entry); } } return listMap; } }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。