随笔-16  评论-84  文章-1  trackbacks-0

在项目中使用Apache开源的Services Framework CXF来发布WebService,CXF能够很简洁与Spring Framework 集成在一起,在发布WebService的过程中,发布的接口的入参有些类型支持不是很好,比如Timestamp和Map。这个时候我们就需要编写一些适配来实行类型转换。

Timestamp:

 1/**
 2 * <java.sql.Timestamp类型转换>
 3 * <功能详细描述>
 4 * 
 5 * @author  Owen
 6 * @version  [版本号, 2010-11-26]
 7 * @see  [相关类/方法]
 8 * @since  [产品/模块版本]
 9 */

10public class TimestampAdapter extends XmlAdapter<String, Timestamp>
11{
12    /** <一句话功能简述>
13     * <功能详细描述>
14     * @param time
15     * @return
16     * @throws Exception
17     * @see [类、类#方法、类#成员]
18     */

19    public String marshal(Timestamp time)
20        throws Exception
21    {
22        return DateUtil.timestamp2Str(time);
23    }

24    
25    /** <一句话功能简述>
26    * <功能详细描述>
27    * @param v
28    * @throws Exception
29    * @see [类、类#方法、类#成员]
30    */

31    public Timestamp unmarshal(String str)
32        throws Exception
33    {
34        return DateUtil.str2Timestamp(str);
35    }

36    
37}


DateUtil

  1/*
  2 * 文 件 名:  DateUtil.java
  3 * 版    权:  4 Dream Co., Ltd. Copyright YYYY-YYYY,  All rights reserved
  4 * 描    述:  <描述>
  5 * 修 改 人:  Owen 
  6 * 修改时间:  2010-11-5
  7 * 跟踪单号:  <跟踪单号>
  8 * 修改单号:  <修改单号>
  9 * 修改内容:  <修改内容>
 10 */

 11package com.osoft.portal.common.util.commons;
 12
 13import java.sql.Timestamp;
 14import java.text.ParseException;
 15import java.text.SimpleDateFormat;
 16import java.util.Date;
 17
 18import org.apache.log4j.Logger;
 19
 20/**
 21 * <一句话功能简述>
 22 * <功能详细描述>
 23 * 
 24 * @author  Owen
 25 * @version  [版本号, 2010-11-5]
 26 * @see  [相关类/方法]
 27 * @since  [产品/模块版本]
 28 */

 29public class DateUtil
 30{
 31    /**
 32     * 注释内容
 33     */

 34    private static final Logger log = Logger.getLogger(DateUtil.class);
 35    
 36    /**
 37     * 默认日期格式
 38     */

 39    private static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
 40    
 41    /** <默认构造函数>
 42     */

 43    private DateUtil()
 44    {
 45    }

 46    
 47    /** <字符串转换成日期>
 48     * <如果转换格式为空,则利用默认格式进行转换操作>
 49     * @param str 字符串
 50     * @param format 日期格式
 51     * @return 日期
 52     * @see [类、类#方法、类#成员]
 53     */

 54    public static Date str2Date(String str, String format)
 55    {
 56        if (null == str || "".equals(str))
 57        {
 58            return null;
 59        }

 60        //如果没有指定字符串转换的格式,则用默认格式进行转换
 61        if (null == format || "".equals(format))
 62        {
 63            format = DEFAULT_FORMAT;
 64        }

 65        SimpleDateFormat sdf = new SimpleDateFormat(format);
 66        Date date = null;
 67        try
 68        {
 69            date = sdf.parse(str);
 70            return date;
 71        }

 72        catch (ParseException e)
 73        {
 74            log.error("Parse string to date error!String : " + str);
 75        }

 76        
 77        return null;
 78    }

 79    
 80    /** <一句话功能简述>
 81     * <功能详细描述>
 82     * @param date 日期
 83     * @param format 日期格式
 84     * @return 字符串
 85     * @see [类、类#方法、类#成员]
 86     */

 87    public static String date2Str(Date date, String format)
 88    {
 89        if (null == date)
 90        {
 91            return null;
 92        }

 93        SimpleDateFormat sdf = new SimpleDateFormat(format);
 94        return sdf.format(date);
 95    }

 96    
 97    /** <时间戳转换为字符串>
 98     * <功能详细描述>
 99     * @param time
100     * @return
101     * @see [类、类#方法、类#成员]
102     */

103    public static String timestamp2Str(Timestamp time)
104    {
105        Date date = new Date(time.getTime());
106        return date2Str(date, DEFAULT_FORMAT);
107    }

108    
109    /** <一句话功能简述>
110     * <功能详细描述>
111     * @param str
112     * @return
113     * @see [类、类#方法、类#成员]
114     */

115    public static Timestamp str2Timestamp(String str)
116    {
117        Date date = str2Date(str, DEFAULT_FORMAT);
118        return new Timestamp(date.getTime());
119    }

120}

121



在具体的Java Bean 中,通过@XmlJavaTypeAdapter注解来通知CXF进行类型转换,具体请看User中的属性registerDate的getter 和setter

  1package com.osoft.portal.system.model;
  2
  3import java.io.Serializable;
  4import java.sql.Timestamp;
  5
  6import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
  7
  8import com.osoft.portal.common.util.commons.DateUtil;
  9import com.osoft.portal.remote.convert.TimestampAdapter;
 10
 11/**
 12 * <一句话功能简述>
 13 * <功能详细描述>
 14 * 
 15 * @author  Owen
 16 * @version  [版本号, 2010-11-1]
 17 * @see  [相关类/方法]
 18 * @since  [产品/模块版本]
 19 */

 20public class User implements Serializable
 21{
 22    private static final long serialVersionUID = -6449817937177158567L;
 23    
 24    /**
 25     * 用户标示
 26     */

 27    private long id;
 28    
 29    /**
 30     * 登录账号
 31     */

 32    private String username;
 33    
 34    /**
 35     * 登录密码
 36     */

 37    private String password;
 38    
 39    /**
 40     * 真实姓名
 41     */

 42    private String realName;
 43    
 44    /**
 45     * 性别
 46     */

 47    private String sex;
 48    
 49    /**
 50     * 注册日期
 51     */

 52    private Timestamp registerDate;
 53    
 54    /**
 55     * 用户状态
 56     */

 57    private String state;
 58    
 59    /** <默认构造函数>
 60     */

 61    public User()
 62    {
 63    }

 64    
 65    public long getId()
 66    {
 67        return id;
 68    }

 69    
 70    public void setId(long id)
 71    {
 72        this.id = id;
 73    }

 74    
 75    public String getUsername()
 76    {
 77        return username;
 78    }

 79    
 80    public void setUsername(String username)
 81    {
 82        this.username = username;
 83    }

 84    
 85    public String getPassword()
 86    {
 87        return password;
 88    }

 89    
 90    public void setPassword(String password)
 91    {
 92        this.password = password;
 93    }

 94    
 95    public String getRealName()
 96    {
 97        return realName;
 98    }

 99    
100    public void setRealName(String realName)
101    {
102        this.realName = realName;
103    }

104    
105    public String getSex()
106    {
107        return sex;
108    }

109    
110    public void setSex(String sex)
111    {
112        this.sex = sex;
113    }

114    
115    @XmlJavaTypeAdapter(TimestampAdapter.class)
116    public Timestamp getRegisterDate()
117    {
118        return registerDate;
119    }

120    
121    public void setRegisterDate(@XmlJavaTypeAdapter(TimestampAdapter.class) Timestamp registerDate)
122    {
123        this.registerDate = registerDate;
124    }

125    
126    public String getState()
127    {
128        return state;
129    }

130    
131    public void setState(String state)
132    {
133        this.state = state;
134    }

135    
136    /** <一句话功能简述>
137     * <功能详细描述>
138     * @return
139     * @see [类、类#方法、类#成员]
140     */

141    public String toString()
142    {
143        StringBuilder build = new StringBuilder();
144        build.append("Real Name: " + this.getRealName() + ",Register Date: "
145            + DateUtil.timestamp2Str(this.getRegisterDate()));
146        return build.toString();
147    }

148    
149}

150


OK,这个时候CXF解析Java Bean User的时候,解析到@XmlJavaTypeAdapter注解时候就会以TimestampAdapter这个适配器来进行Timestamp与String之间的转换。


Map:

这里贴下上一个项目中java.util.Map写的转换器,参考javaeye上一个网友提示的,直接用xstream将Map转换成String

 1/**
 2 * <数据模型转换>
 3 * <Map<String,Object> 与 String之间的转换>
 4 * 
 5 * @author  Owen
 6 * @version  [版本号, Apr 28, 2010]
 7 * @see  [相关类/方法]
 8 * @since  [产品/模块版本]
 9 */

10@XmlType(name = "MapAdapter")
11@XmlAccessorType(XmlAccessType.FIELD)
12public class MapAdapter extends XmlAdapter<String, Map<String, Object>>
13{
14    
15    /**
16     * Convert a bound type to a value type.
17     * 转换JAXB不支持的对象类型为JAXB支持的对象类型
18     *
19     * @param map map
20     *      The value to be convereted. Can be null.
21     * @return String   
22     * @throws Exception
23     *      if there's an error during the conversion. The caller is responsible for
24     *      reporting the error to the user through {@link javax.xml.bind.ValidationEventHandler}.
25     */

26    public String marshal(Map<String, Object> map)
27        throws Exception
28    {
29        XStream xs = new XStream(new DomDriver());
30        return xs.toXML(map);
31    }

32    
33    /**
34     * Convert a value type to a bound type.
35     * 转换JAXB支持的对象类型为JAXB不支持的的类型
36     *
37     * @param model
38     *      The value to be converted. Can be null.
39     * @return Map<String,Object>
40     * @throws Exception
41     *      if there's an error during the conversion. The caller is responsible for
42     *      reporting the error to the user through {@link javax.xml.bind.ValidationEventHandler}.
43     */

44    @SuppressWarnings("unchecked")
45    public Map<String, Object> unmarshal(String model)
46        throws Exception
47    {
48        XStream xs = new XStream(new DomDriver());
49        return (HashMap)xs.fromXML(model);
50    }

51    
52}
posted on 2010-11-27 14:28 absolute 阅读(3392) 评论(1)  编辑  收藏

评论:
# re: 让Apache CXF 支持传递java.sql.Timestamp和java.util.HashMap类型 2014-08-19 10:44 | David房
谢谢,解决了我的问题,但是@XmlJavaTypeAdapter(TimestampAdapter.class)这段加在get/set前面会报有多个属性的错,加在private Timestamp registerDate;前面就ok了。  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: