`
zhlfz
  • 浏览: 27997 次
  • 性别: Icon_minigender_1
  • 来自: 河南
社区版块
存档分类
最新评论

CXF 不支持Timestamp

cxf 
阅读更多

CXF集成Spring报下面错误:

Error creating bean with name 'cxf': Singleton bean creation not 
allowed while the singletons of this factory are in destruction (Do 
not request a bean from a BeanFactory in a destroy method 
implementation!) 

 

原因是CXF 不支持Timestamp

 

解决方案参照

http://www.blogjava.net/absolutedo/archive/2010/11/27/339190.html

 

在项目中使用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}

 

 

 

分享到:
评论

相关推荐

    WebService之CXF开发指南.rar

    CXF提供了丰富的安全特性,如WS-Security(WS-SecureConversation、WSS-Timestamp等),支持数字签名、加密、用户名令牌、X.509证书等多种安全策略。开发者可以根据需求选择合适的安全方案。 **7. 性能优化与调试**...

    CXF安全Timestamps+Encryption+Signature加密演示案例(含JAR文件)

    CXF支持使用XML Encryption标准来加密Web服务消息中的部分内容或整个消息。这通常通过密钥管理机制实现,如公钥基础设施(PKI)和X.509证书。加密过程包括选择合适的加密算法,如AES,以及定义密钥交换策略。在我们...

    webServiceSprin整合jar包apache-cxf-2.5.9.rar

    CXF是一个用于构建服务端和客户端Web服务的框架,它支持各种协议和规范,包括但不限于JAX-WS(Java API for XML Web Services)和JAX-RS(Java API for RESTful Web Services)。CXF允许开发者使用POJO(Plain Old ...

    Cxf 和wss4j实现ws-security的demo

    CXF使用WSS4J实现WS-Security规范,本例的配置是Timestamp Signature Encrypt,具体使用可以参考我的博客http://blog.csdn.net/wangchsh2008/article/details/6708270

    How to record timestamp of each command in history

    默认情况下,`history`命令会显示过去执行过的命令,但不包含时间戳。为了添加时间戳,我们需要修改`.bashrc`或`.bash_profile`配置文件。这些是用户的启动脚本,每次打开新的终端会话时都会运行。 首先,打开配置...

    ip黑白名单拦截器java示例

    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); ``` 接下来,"字段说明与使用"这部分可能包含如何插入、查询和更新IP列表的说明。例如,插入一条IP黑名单记录的示例: ```sql INSERT INTO ip_list (ip_...

    java webservice实现 (全网)新商务短信接口

    - **开发环境准备**:确保Java环境搭建完成,Web服务器(如Tomcat)和SOAP服务框架(如Apache CXF)安装就绪。 - **接口调用**:根据接口文档定义请求参数,采用MD5算法对敏感信息进行加密处理。 - **错误处理**:...

    使用xfire框架搭建webService的一个demo

    Xfire(现为Apache CXF的一部分)是一个开源的Java框架,它简化了创建和使用Web服务的过程。它支持多种协议和标准,如SOAP、RESTful、WS-*等,并且能够与Spring框架无缝集成,提供灵活的配置和强大的功能。 1. **...

Global site tag (gtag.js) - Google Analytics