解决Apache CXF 不支持传递java.sql.Timestamp和java.util.HashMap类型问题
在项目中使用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);} }
转载于:https://www.cnblogs.com/gisblogs/p/3988128.html
解决Apache CXF 不支持传递java.sql.Timestamp和java.util.HashMap类型问题相关推荐
- java.sql.timestamp_java.sql.Date和java.sql.Timestamp转换
在开发web应用中,针对不同的数据库日期类型,我们需要在我们的程序中对日期类型做各种不同的转换.若对应数据库数据是oracle的Date类型,即只需要年月日的,可以选择使用java.sql.Date类 ...
- java.util.Date和java.sql.Timestamp转换
java.sql.Date 只存储日期数据不存储时间数据 // 会丢失时间数据 preparedStatement.setDate(1, new java.sql.Date(date.getTime( ...
- java.sql.Date和java.sql.Timestamp转换
转自:https://www.xuebuyuan.com/1479399.html 在开发web应用中,针对不同的数据库日期类型,我们需要在我们的程序中对日期类型做各种不同的转换.若对应数据库数据是o ...
- 已解决:java.sql.SQLException: Value ‘0000-00-00 00:00:00‘ can not be represented as java.sql.Timestamp
一.问题 一大早到公司 Mysql 中的一个 datetime 字段时碰到了一个 Cause: java.sql.SQLException: Value '0000-00-00 00:00:00' c ...
- 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,默认为 ...
- 错误:Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp;的解决
问题: 代码中查询MySQL的结果集时报错,提示Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp;刚开始 ...
- Cause: java.sql.SQLExceptioValue ‘0000-00-00 00:00:00‘ can not be represented as java.sql.Timestamp
今天使用mybatis创建demo测试的时候发现了一个有意思的bug org.apache.ibatis.exceptions.PersistenceException: ### Error quer ...
- Cause java.sql.SQLDataException Unsupported conversion from LONG to java.sql.Timestamp
今天遇到了一个奇怪的错误,报错如下图所示: org.springframework.dao.DataIntegrityViolationException: Error attempting to g ...
- java date只保留年月日_java.util.Date、java.sql.Date、java.sql.Timestamp区别和总结
在web开发中,避免不了对日期的操作,就几种常见的日期操作做个总结(部分参考网络,在此表示感谢): java.util.Date.java.sql.Datej.java.sql.Timestamp j ...
最新文章
- 照片换色 使用Python 或者 java
- QQ号码采集及邮件发送系统2009
- 【CodeForces - 255A】Greg's Workout (水题)
- 学习Spring Boot:(二十七)Spring Boot 2.0 中使用 Actuator
- linux input子系统分析--概述与数据结构
- Cocos2dx使用 TexturePacker导出的.plist
- 并发编程之Lock接口
- Python3下的WIFI密码爆破
- 关于keil注册机的问题
- 推送给自学程序员们的一本Python书
- 恢复和去除时间(Recovery and Removal Time)
- 运用fiddler工具深度配置证书抓苹果IOS微信小程序或app数据请求
- sql注入中的联合注入
- python 解决 mismatch问题
- tankbot 机器人_优必选首款履带式Jimu机器人 TankBot 登陆Apple Store零售店
- HTML项目案例,适合练手总结。
- elasticsearch基础1——索引、文档
- Android 实现系统打印机打印图片,文本,以及二维码生成与解析
- 分式化简结果要求_分式化简的结果为( ) A. B. C. D.——青夏教育精英家教网——...
- android扁平化按钮素材,扁平化APP图标素材库-Modern UI Icons
热门文章
- 2022-2028中国快时尚服装市场竞争及发展前景预测报告
- python2 安装faiss-gpu 报错 faiss/faiss/python/swigfaiss.i:241: Error: Unable to find ‘faiss/impl/platfo
- Http请求之优雅的RestTemplate
- GitHub上开源的YOLOv5
- 自动驾驶的分级和行业现状
- HashMap 的长度为什么是 2 的幂次方?
- linux locale文件,Linux 怎样修改locale语言设置
- mysql error number 1130,[转]mysql error number 1130的解决方法
- Android Dialog 弹出的时候标题栏闪烁一下的处理方法
- android app 内置图标icon 的标准,(目前是2019年)