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

TimestampAdapter.java

package com.loongtao.general.crawler.webservice.utils;

import java.sql.Timestamp;

import javax.xml.bind.annotation.adapters.XmlAdapter;

/**
* <java.sql.Timestamp类型转换> <功能详细描述>
* 在相应的字段前面 加上 @XmlJavaTypeAdapter(TimestampAdapter.class)
* @author Lilin
*/
public class TimestampAdapter extends XmlAdapter<String, Timestamp> { /**
* <一句话功能简述> <功能详细描述>
*
* @param time
* @return
* @throws Exception
* @see [类、类#方法、类#成员]
*/
public String marshal(Timestamp time) throws Exception {
return DateUtil.timestamp2Str(time);
} /**
* <一句话功能简述> <功能详细描述>
*
* @param v
* @throws Exception
* @see [类、类#方法、类#成员]
*/
public Timestamp unmarshal(String str) throws Exception {
return DateUtil.str2Timestamp(str);
}
}

DateUtil.java

package com.loongtao.general.crawler.webservice.utils;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.ParseException; import org.apache.log4j.Logger; /**
* <一句话功能简述> <功能详细描述>
*
* @author Lilin
* @version
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class DateUtil {
/**
* 注释内容
*/
private static final Logger log = Logger.getLogger(DateUtil.class);
/**
* 默认日期格式
*/
private static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss"; /**
* <默认构造函数>
*/
private DateUtil() {
} /**
* <字符串转换成日期> <如果转换格式为空,则利用默认格式进行转换操作>
*
* @param str
* 字符串
* @param format
* 日期格式
* @return 日期
* @see [类、类#方法、类#成员]
*/
public static Date str2Date(String str, String format) {
if (null == str || "".equals(str)) {
return null;
}
// 如果没有指定字符串转换的格式,则用默认格式进行转换
if (null == format || "".equals(format)) {
format = DEFAULT_FORMAT;
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = null;
try {
date = sdf.parse(str);
return date;
} catch (ParseException e) {
log.error("Parse string to date error!String : " + str);
} return null;
} /**
* <一句话功能简述> <功能详细描述>
*
* @param date
* 日期
* @param format
* 日期格式
* @return 字符串
* @see [类、类#方法、类#成员]
*/
public static String date2Str(Date date, String format) {
if (null == date) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
} /**
* <时间戳转换为字符串> <功能详细描述>
*
* @param time
* @return
* @see [类、类#方法、类#成员]
*/
public static String timestamp2Str(Timestamp time) {
Date date = new Date(time.getTime());
return date2Str(date, DEFAULT_FORMAT);
} /**
* <一句话功能简述> <功能详细描述>
*
* @param str
* @return
* @see [类、类#方法、类#成员]
*/
public static Timestamp str2Timestamp(String str) {
Date date = str2Date(str, DEFAULT_FORMAT);
return new Timestamp(date.getTime());
}
}

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

/*
* Copyright (c) 2014-2024 . All Rights Reserved.
*
* This software is the confidential and proprietary information of
* LoongTao. You shall not disclose such Confidential Information
* and shall use it only in accordance with the terms of the agreements
* you entered into with LoongTao.
*
*/
package com.loongtao.general.crawler.webservice.vo; import java.io.Serializable;
import java.sql.Timestamp; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import com.loongtao.general.crawler.webservice.utils.TimestampAdapter; /**
* @declare: 下载失败信息<br>
* @author: cphmvp
* @version: 1.0
* @date: 2014年9月22日下午3:47:26
*/
public class ErrInfo implements Serializable {
/**
*
*/
private static final long serialVersionUID = -5298849636495962631L;
private String ip; public String getIp() {
return ip;
} public void setIp(String ip) {
this.ip = ip;
} public String getUrl() {
return url;
} public void setUrl(String url) {
this.url = url;
} public int getArticleMediaId() {
return articleMediaId;
} public void setArticleMediaId(int articleMediaId) {
this.articleMediaId = articleMediaId;
} @XmlJavaTypeAdapter(TimestampAdapter.class)
public Timestamp getTimestamp() {
return timestamp;
} public void setTimestamp(Timestamp timestamp) {
this.timestamp = timestamp;
} private String url;
private int articleMediaId;
private Timestamp timestamp; }

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

Map:

用xstream将Map转换成String

package com.loongtao.general.crawler.webservice.utils;

import java.util.HashMap;
import java.util.Map; import javax.xml.bind.annotation.adapters.XmlAdapter; import org.apache.cxf.aegis.type.java5.XmlType; import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType; /**
* <数据模型转换> <Map<String,Object> 与 String之间的转换>
*
* @author Lilin
* @version
* @see [相关类/方法]
* @since [产品/模块版本]
*/
@XmlType(name = "MapAdapter")
@XmlAccessorType(XmlAccessType.FIELD)
public class MapAdapter extends XmlAdapter<String, Map<String, Object>> {
/**
* Convert a bound type to a value type. 转换JAXB不支持的对象类型为JAXB支持的对象类型
*
* @param map
* map The value to be convereted. Can be null.
* @return String
* @throws Exception
* if there's an error during the conversion. The caller is
* responsible for reporting the error to the user through
* {@link javax.xml.bind.ValidationEventHandler}.
*/
public String marshal(Map<String, Object> map) throws Exception {
XStream xs = new XStream(new DomDriver());
return xs.toXML(map);
} /**
* Convert a value type to a bound type. 转换JAXB支持的对象类型为JAXB不支持的的类型
*
* @param model
* The value to be converted. Can be null.
* @return Map<String,Object>
* @throws Exception
* if there's an error during the conversion. The caller is
* responsible for reporting the error to the user through
* {@link javax.xml.bind.ValidationEventHandler}.
*/
@SuppressWarnings("unchecked")
public Map<String, Object> unmarshal(String model) throws Exception {
XStream xs = new XStream(new DomDriver());
return (HashMap) xs.fromXML(model);
}
}

解决Apache CXF 不支持传递java.sql.Timestamp和java.util.HashMap类型问题的更多相关文章

  1. mybatis invalid comparison: java.sql.Timestamp and java.lang.String报错解决方法

    这个错的意思是:java.sql.Timestamp和java.lang.String无效的比较 错误的原因是:拿传入的时间类型参数与空字符串进行比较就会报这个异常 解决方法:只保留非null判断就可 ...

  2. 关于如何处理JSONObject.fromObject(Object obj)无法转换特殊日期(java.sql.Date,java.sql.Timestamp)格式的问题。

    转:关于如何处理JSONObject.fromObject(Object obj)无法转换特殊日期(java.sql.Date,java.sql.Timestamp)格式的问题. 关于JSONObje ...

  3. java.sql.Date和java.util.Date的不同和相互转换方式

    一:前言 这是我在新的公司写的第一份博客吧,来了又一个星期了吧,但是在来的那几天我真的很迷茫的感觉这里是很不适合我的样子,而且我又是来实习的,我很不愿意啊,自己做的又是java web,最原始的ser ...

  4. java.sql.Timestamp类型

    如果想向数据库中插入日期时间的话,可以用java.sql.Timestamp类 一个与 java.util.Date类有关的瘦包装器 (thin wrapper),它允许 JDBC API 将该类标识 ...

  5. JAVA中java.util.Date、java.sql.Timestamp和String之间的互相转换

    java.util.Date与的String互转 java.util.Date---->String /** * 将java.util.Date对象转化为String字符串 * @param d ...

  6. java.util.Date、java.sql.Date、java.sql.Time、java.sql.Timestamp小结

    java.lang.Object ....|__java.util.Date ..........|__java.sql.Date/java.sql.Timestamp /java.sql.Time ...

  7. java.sql.Date和java.sql.Timestamp转换

    转自:https://www.xuebuyuan.com/1479399.html 在开发web应用中,针对不同的数据库日期类型,我们需要在我们的程序中对日期类型做各种不同的转换.若对应数据库数据是o ...

  8. 报错 java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp 原因

    sql异常 java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestam ...

  9. mysql解决Value ‘0000-00-00 00:00:00’ can not be represented as java.sql.Timestamp

    同步发布:http://www.yuanrengu.com/index.php/mysqlsolvetimestamp.html 在使用mysql时,如果数据库中的字段类型是timestamp,默认为 ...

随机推荐

  1. Java和Python运行速度对比

    Java和Python运行速度对比:同一个函数运行一百万次,Java耗时0.577秒,Python耗时78秒--135倍的差距. 版本:Java 8,Python 2.7.10 Java测试代码: i ...

  2. JS匿名函数的理解

    js匿名函数的代码如下:(function(){ // 这里忽略jQuery 所有实现 })(); 半年前初次接触jQuery 的时候,我也像其他人一样很兴奋地想看看源码是什么样的.然而,在看到源码的 ...

  3. 使用virtualenv搭建独立的Python环境

    virtualenv可以搭建虚拟且独立的python环境,可以使每个项目环境与其他项目独立开来,保持环境的干净,解决包冲突问题. 一.安装virtualenv virtualenv实际上是一个pyth ...

  4. calico for kubernetes

    (这一篇中很多错误,勿参考!) The reference urls: https://github.com/kubernetes/kubernetes/blob/master/docs/gettin ...

  5. win平台检查内存泄露

    int main() { _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF); _CrtSetBre ...

  6. 第十一章 TClientDataSet

    第十一章 TClientDataSet 与TTable.TQuery一样,TClientDataSet也是从TDataSet继承下来的,它通常用于多层体系结构的客户端.TClientDataSet最大 ...

  7. codeforces B. New Year Present 解题报告

    题目链接:http://codeforces.com/contest/379/problem/B 题目意思:给定一个有n个钱包的序列,其中第i个钱包需要投入ai个钱币,需要编写一个程序,使得在对第i个 ...

  8. Eclipse 中Tomcat 启动 与直接启动Tomcat的区别

    这段时间不用Java 了突然发现在用的时候出问题了. 首先现在Eclipse和Tomcat,解压后 如图1所示: 图1: 进入里面的bin文件目录后发现有 如图2所示包含了startup.bat,st ...

  9. 【读书笔记】读《JavaScript高级程序设计-第2版》 - 非函数部分

    章节列表: 第08章:BOM 第09章:客户端检测 第10章:DOM 第11章:DOM2和DOM3 第12章:事件 第13章:表单脚本 第14章:错误处理与调试 第17章:Ajax和JSON第20章: ...

  10. Windows下进程间通信及数据共享

    进程是装入内存并准备执行的程序,每个进程都有私有的虚拟地址空间,由代码.数据以及它可利用的系统资源(如文件.管道等)组成. 多进程/多线程是Windows操作系统的一个基本特征.Microsoft W ...