解决Apache CXF 不支持传递java.sql.Timestamp和java.util.HashMap类型问题
2023-09-11 14:19:51 时间
在项目中使用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); } }
相关文章
- java代理使用 apache ant实现文件压缩/解压缩
- Eclipse的java代码出错:The import org.apache cannot be resolved
- Java容器 | 基于源码分析List集合体系
- Tomcat部署项目时出错java.lang.IllegalStateException: ContainerBase.addChild: start:org.apache.catalina.Life
- Idea:Java文件打包可执行的JAR文件,并指定JDK编译版本
- 【转】Java计算文件的hash值
- tomcat启动提示java.lang.UnsatisfiedLinkError: D:softdevToolapache-tomcat-7.0.57bintcnative-1.dll: C
- 映射 SQL 和 Java 类型
- Ubuntu安装java的几种方式以及多个JAVA版本的切换
- 阿里互联网一线大厂 Java 岗面试题库(2022 年版)这次 38k!稳了
- 最新秋招,Java八股文!含答案,JAVA核心知识点最详细版(面试必备)
- 解决bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: ORA-00911: 无效字符
- 第七节:详细讲解Java中的日期,java.util.date
- JAVA-安装apache tomcat服务器
- vue 的H5与app壳java方法交互
- Java IDEA中设置导入import java.xx.*和设置导入具体的import java.xx.yy;
- java整理软件--- Java OCR 图像智能字符识别技术,可识别中文,但是验证码不可以识别...已测识别中文效果很好
- 如何用Eclipse调试(debug)Java代码?
- Java 单向链表翻转
- Java .class 反编译 Luyten,导出 .java文件,查看jar 包目录,超级简单,实用
- mybatis或hibernate框架连接数据库报错:java.sql.SQLException: No suitable driver或No suitable driver found for http://maven.apache.org
- 『Java练习生的自我修养』java-se进阶³ • 线程的等待与唤醒
- Java实例---简单的上课管理系统
- JAVA学习.java.sql.date 与java.util.date以及gettime()方法的分析
- 一个Java的权限框架-Shiro
- Java中的BigInteger在ACM中的应用
- Java小白入门200例79之Java类的封装
- Java小白入门200例71之Java创建对象
- 【SQL Bugs】Caused by: java.sql.SQLSyntaxErrorException: ORA-01789: 查询块具有不正确的结果列数(原因记录)
- java.io.FileNotFoundException: D:tomcat2apache-tomcat-5.5.26webappsROOTworkflatUpload (拒绝访问。)
- Java new 一个对象在堆上开辟的内存大小