webservice返回值为Map类型的处理方法
在写一个webservice的时候,方法的返回值是一个复杂类型,处理方法是写一个结果类(Javabean)作为返回值。想着webservice方法返回值为Map的没写过,然后就试着写了一个简单的Demo。出错了...那我就来劲了,总有办法解决吧....

通过百度(你有Google癖好就用Google吧)。。找到方法,通过前辈们的经验找到解决方法。
业内人士都懂!注重版权,奉上原文链接:
http://blog.csdn.net/jspamd/article/details/8914427
贴上自个Demo代码之前,补充个知识点,webservice中发布方法的参数以及返回值可以很好的处理基本类型,POJO类,数组,以及list集合等复杂类型,但是在处理Map,非JavaBean式的类,我们需要自定义一个转换器,负责将webservice中不能处理的类型转换为可以处理的类型
(1)需要使用注解@XmlJavaTypeAdapter修饰返回类型
@WebService
public interface HelloService {
public String sayHello(String name);
public String sayGoodBy(String name);
public String sayHello2(String name); public @XmlJavaTypeAdapter((XmlMapAdapter.class)) Map<String, String> getSpace(String name);
}
(2)自定义一个可以替代不可处理参数类型的类(可以理解为模拟Map接口的类,这个类是POJO)
public class MyStringMap {
private List<Entry> entries;
/**
* @return entries
*/
public List<Entry> getEntries() {
return entries;
}
/**
* @param entries the entries to set
*/
public void setEntries(List<Entry> entries) {
this.entries = entries;
}
public static class Entry {
private String key;
private String value;
/**
* @return key
*/
public String getKey() {
return key;
}
/**
* @param key the key to set
*/
public void setKey(String key) {
this.key = key;
}
/**
* @return value
*/
public String getValue() {
return value;
}
/**
* @param value the value to set
*/
public void setValue(String value) {
this.value = value;
}
}
}
(3)自定义一个转换器(作为@XmlJavaTypeAdapter注解中value)
public class XmlMapAdapter extends XmlAdapter<MyStringMap, Map<String, String>>{
@Override
//Map(不可处理)转换为可处理的类(自定义的POJO类,就是模拟Map的一个类)
public MyStringMap marshal(Map<String, String> v) throws Exception {
MyStringMap result = new MyStringMap();
List<pojo.MyStringMap.Entry> entries = new ArrayList<MyStringMap.Entry>();
for(Entry<String, String> e : v.entrySet()){
pojo.MyStringMap.Entry entry = new pojo.MyStringMap.Entry();
entry.setKey(e.getKey());
entry.setValue(e.getValue());
entries.add(entry);
}
result.setEntries(entries);
return result;
}
//自定义可处理类转换为Map(不可处理的类型)
@Override
public Map<String, String> unmarshal(MyStringMap v) throws Exception {
Map<String, String> result = new HashMap<String, String>();
for(pojo.MyStringMap.Entry e : v.getEntries()){
result.put(e.getKey(), e.getValue());
}
return result;
}
}
(4)接口的实现类
@WebService(endpointInterface="com.webservice.HelloService",serviceName="MyService",targetNamespace="http://www.baidu.com")
public class HelloServiceImpl implements HelloService { @WebMethod(operationName="AliassayHello")
@WebResult(name="myReturn")
@Override
public String sayHello(@WebParam(name="name")String name) {
System.out.println("Hello,"+name);
return "Hello,"+name;
} @Override
public String sayGoodBy(@WebParam(name="name")String name) {
System.out.println("GoodBy,"+name);
return "GoodBy,"+name;
} @WebMethod(exclude=true)//不会被发布出去
@Override
public String sayHello2(String name) {
System.out.println("hello2"+ name);
return "Hello2,"+name;
} @Override
public Map<String, String> getSpace(String name) {
HashMap<String, String> resultMap = new HashMap<String,String>(); resultMap.put("age", "12");
resultMap.put("name", name);
resultMap.put("orid", "123123");
resultMap.put("address", "北京"); System.out.println(resultMap);
return resultMap;
} public static void main(String[] args) {
Endpoint.publish("http://127.0.0.1:8099/hello", new HelloServiceImpl());
System.out.println("服务发布成功!");
} }
剩下的就是测试工作,这里就没写客户端程序去测试了,使用SoapUI测试下
soap请求消息:

soap响应

个人理解webservice中的转换器的思想机制和struts2 中的类型转换机制是一个样的。
朋友,看我这么帅,点个赞呗
webservice返回值为Map类型的处理方法的更多相关文章
- 使用MyBatis时接收值和返回值选择Map类型或者实体类型
MyBatis作为现近JavaEE企业级项目开发中常用的持久层框架之一,以其简洁高效的ORM映射和高度的SQL的自由性被广大开发人员认可.Mybatis在接收系统传来的参数和返回的参数时主要可以有Ma ...
- SpringMVC Controller 返回值几种类型
SpringMVC Controller 返回值几种类型 2016年06月21日 19:31:14 为who而生 阅读数:4189 标签: Controller 返回值类型spring mvc 更多 ...
- mybatis Mapper 中resultType使用方法及返回值为Map的写法
mybatis学习(七)——resultType解析 resultType是sql映射文件中定义返回值类型,返回值有基本类型,对象类型,List类型,Map类型等.现总结一下再解释 总结: resul ...
- Spring MVC--------处理方法返回值的可选类型
对于Spring MVC处理方法支持支持一系列的返回方式: (1)ModelAndView (2)Model (3)ModelMap (4)Map (5)View (6)String (7)Void ...
- Swift2.0语言教程之函数的返回值与函数类型
Swift2.0语言教程之函数的返回值与函数类型 Swift2.0中函数的返回值 根据是否具有返回值,函数可以分为无返回值函数和有返回值函数.以下将会对这两种函数类型进行讲解. Swift2.0中具有 ...
- C语言函数返回值和变量类型
前言 最近在刷题,在写矩阵的快速幂的题时,对于返回值是数组的程序,写的十分冗杂.借此机会,重新梳理下C语言中函数的返回值与变量类型的关系. 按照变量的寿命,可以分为三种类型 1.静态变量 寿命从程序开 ...
- Mybaits查询返回值是List类型的
查询返回值是list类型的 1 首先在接口中写方法 public interface EmployeeMapper { public List<Employee> getEmpsByLas ...
- Mybatis select返回值为map时,选取表字段的两列作为key,value
项目需要从ibatis升级到MyBatis,dao中有一个方法返回Map类型,具体是查询语句查询两个字段,将结果列表字段A的值作为key字段B的值作为value存入Map中作为结果返回: ibatis ...
- SpringMVC Controller 返回值的可选类型
spring mvc 支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void. ModelAndView @RequestMap ...
随机推荐
- SqlServer查询表中各列名称、表中列数
查询表名为tb_menu的所有列名 select name from syscolumns where id=object_id('tb_menu') 查询表名为tb_menu的所有列名个数 ...
- ListView总结
ListView类作为在Android开发中经常会使用到的组件,作为新手,还是感到这一块变化形式还是很多的,需要慢慢学习.现在这里大概总结一下. 基于数组的ListView:使用android:ent ...
- mac 关于使用protobuf出现ld: symbol(s) not found for architecture x86_64的问题
主要是编译时没有添加protobuf库文件 g++ -o Writer.o lm.helloworld.pb.cc Writer.cpp -L/usr/local/lib -lprotobuf
- WP8如何添加Newtonsoft.Json包
WP8开发的时候如何使用Newtonsoft.Json包呢?我在网上包括官网下的DLL文件,添加引用时都给出了这样的提示: 而后在网上找到的解决办法是:使用NuGet程序包来添加. 首先点击工具--& ...
- 使用C#在CEF中拦截并响应请求
一.前言 忙里偷闲,研究了一下如何在CEF中拦截请求,并作出响应.这个功能对某些需要修改服务器响应的需求来说必不可少,可以直接读取本地文件作为响应内容. C#的CEF封装项目有很多,我使用的是Chro ...
- 关于使用iframe嵌套页面的跳转方式
一.背景A,B,C,D都是jsp,D是C的iframe,C是B的iframe,B是A的iframe,在D中跳转页面的写法区别如下. 二.JS跳转window.location.href.locatio ...
- jQuery中设置form表单中action的值的方法
下面介绍在jQuery中设置form表单中action的值的方法. $("#myFormId").attr("action", "userinfo.s ...
- Django 中related_name,"%(app_label)s_%(class)s_related"
先看个model from django.db import models # Create your models here. class Parent(models.Model): name = ...
- fsockopen读取、发送cookie及注意事项 -代码示例
function httpPost($url, $data,$cookieStr='') { $url_array = parse_url($url); $host = $url_array['hos ...
- 移动前端不得不了解的html5 head 头标签
本文主要内容来自一丝的常用的 HTML 头部标签和百度FEX的HTML head 头标签. 移动端的工作已经越来越成为前端工作的重要内容,除了平常的项目开发,HTML 头部标签功能,特别是meta标签 ...