DataBean.javapackagecom.xidian.wq.imaopay.model.mesdata.base;importjavax.xml.bind.annotation.XmlRootElement;/***
*@authorBYSocket
* 报文头-[报文基础信息]*/@XmlRootElement(name="data_bean")public classDataBean
{//版本信息(data_version)
String version;消息类型(data_type)//000001 - 注册[个人信息Post|Respose]//000002 - 绑定银行卡[首次绑定,保存个人信息]//000003 - 绑定银行卡[二次,校验个人信息]//100001 - 支付[钱包支付]//100002 - 查询//200001
String data_type;订单编号(order_id)//总长20位, 有字母要用大写
String batch_no;//平台账号(platform_account)
String user_name;请求状态//000
String trans_state;//签名信息(msg_sign)
String msg_sign;//时间戳(randomTime)
privateString rd_time;//随机数(randomNum)
privateString rd_num;//密钥(keySign)
privateString k_sign;//保留域
String reserve;publicString getVersion()
{returnversion;
}public voidsetVersion(String version)
{this.version =version;
}publicString getData_type()
{returndata_type;
}public voidsetData_type(String data_type)
{this.data_type =data_type;
}publicString getBatch_no()
{returnbatch_no;
}public voidsetBatch_no(String batch_no)
{this.batch_no =batch_no;
}publicString getUser_name()
{returnuser_name;
}public voidsetUser_name(String user_name)
{this.user_name =user_name;
}publicString getTrans_state()
{returntrans_state;
}public voidsetTrans_state(String trans_state)
{this.trans_state =trans_state;
}publicString getMsg_sign()
{returnmsg_sign;
}public voidsetMsg_sign(String msg_sign)
{this.msg_sign =msg_sign;
}publicString getRd_time()
{returnrd_time;
}public voidsetRd_time(String rd_time)
{this.rd_time =rd_time;
}publicString getRd_num()
{returnrd_num;
}public voidsetRd_num(String rd_num)
{this.rd_num =rd_num;
}publicString getK_sign()
{returnk_sign;
}public voidsetK_sign(String k_sign)
{this.k_sign =k_sign;
}publicString getReserve()
{returnreserve;
}public voidsetReserve(String reserve)
{this.reserve =reserve;
}
}
RegBean.javapackagecom.xidian.wq.imaopay.model.mesdata.base;importjavax.xml.bind.annotation.XmlRootElement;/***
*@authorBYSocket
* 报文体-[用户注册报文体信息]*/@XmlRootElement(name="reg_bean")public classRegBean
{//注册报文体流水号(reg_data_sn)
privateString reg_sn;用户编号(user_info_id)//业务系统的用户编号只做记录不在支付系统中逻辑处理
privateInteger user_id;//注册账号(reg_account)
privateString reg_no;//注册途径(reg_way)
privateString reg_way;//保留域
privateString reserve;提交日期(set_time)//业务系统上传
privateString set_time;//后台信息返回路径(return_url)
privateString ret_url;//备注
String remarks;publicString getReg_sn()
{returnreg_sn;
}public voidsetReg_sn(String reg_sn)
{this.reg_sn =reg_sn;
}publicInteger getUser_id()
{returnuser_id;
}public voidsetUser_id(Integer user_id)
{this.user_id =user_id;
}publicString getReg_no()
{returnreg_no;
}public voidsetReg_no(String reg_no)
{this.reg_no =reg_no;
}publicString getReg_way()
{returnreg_way;
}public voidsetReg_way(String reg_way)
{this.reg_way =reg_way;
}publicString getReserve()
{returnreserve;
}public voidsetReserve(String reserve)
{this.reserve =reserve;
}publicString getSet_time()
{returnset_time;
}public voidsetSet_time(String set_time)
{this.set_time =set_time;
}publicString getRet_url()
{returnret_url;
}public voidsetRet_url(String ret_url)
{this.ret_url =ret_url;
}publicString getRemarks()
{returnremarks;
}public voidsetRemarks(String remarks)
{this.remarks =remarks;
}
}
注册报文体MsgRegBean.java,支持内容的列表。packagecom.xidian.wq.imaopay.model.mesdata;importjava.util.List;importjavax.xml.bind.annotation.XmlElement;importjavax.xml.bind.annotation.XmlElementWrapper;importjavax.xml.bind.annotation.XmlRootElement;importcom.xidian.wq.imaopay.model.mesdata.base.DataBean;importcom.xidian.wq.imaopay.model.mesdata.base.RegBean;
@XmlRootElement( name= "msg_bean")public classMsgRegBean
{privateDataBean dataBean;private ListregBeans;publicDataBean getDataBean()
{returndataBean;
}
@XmlElement(name= "data_bean")public voidsetDataBean(DataBean dataBean)
{this.dataBean =dataBean;
}
@XmlElementWrapper(name= "reg_beans")
@XmlElement(name= "reg_bean")public ListgetRegBeans()
{returnregBeans;
}public void setRegBeans(ListregBeans)
{this.regBeans =regBeans;
}
}
然后针对的我们用Jaxb实现一个转换的Util类:packagecom.xidian.wq.imaopay.util;importjava.io.StringReader;importjava.io.StringWriter;importjava.util.ArrayList;importjava.util.List;importjavax.xml.bind.JAXBContext;importjavax.xml.bind.JAXBException;importjavax.xml.bind.Marshaller;importjavax.xml.bind.Unmarshaller;importcom.alibaba.fastjson.JSON;importcom.xidian.wq.imaopay.model.mesdata.MsgRegBean;importcom.xidian.wq.imaopay.model.mesdata.base.DataBean;importcom.xidian.wq.imaopay.model.mesdata.base.RegBean;/***
*@authorBYSocket
* Jaxb2.0 处理Xml与Object转换
**/
public classJaxbObjectAndXmlUtil
{/***@paramxmlStr 字符串
*@paramc 对象Class类型
*@return对象实例*/@SuppressWarnings("unchecked")public static T xml2Object(String xmlStr,Classc)
{try{
JAXBContext context=JAXBContext.newInstance(c);
Unmarshaller unmarshaller=context.createUnmarshaller();
T t= (T) unmarshaller.unmarshal(newStringReader(xmlStr));returnt;
}catch (JAXBException e) { e.printStackTrace(); return null; }
}/***@paramobject 对象
*@return返回xmlStr*/
public staticString object2Xml(Object object)
{try{
StringWriter writer= newStringWriter();
JAXBContext context=JAXBContext.newInstance(object.getClass());
Marshaller marshal=context.createMarshaller();
marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true); //格式化输出
marshal.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");//编码格式,默认为utf-8
marshal.setProperty(Marshaller.JAXB_FRAGMENT, false);//是否省略xml头信息
marshal.setProperty("jaxb.encoding", "utf-8");
marshal.marshal(object,writer);return newString(writer.getBuffer());
}catch (Exception e) { e.printStackTrace(); return null;}
}public static voidmain(String[] args)
{/**构造测试报文头对象*/DataBean dataBean= newDataBean();
dataBean.setBatch_no("N20150204");
dataBean.setData_type("000001");
dataBean.setVersion("v1.0");
dataBean.setUser_name("xx");
dataBean.setMsg_sign("未知");
dataBean.setRd_num("6631383");
dataBean.setRd_time("20150204");
dataBean.setK_sign("75CC479AAC09F00BA28F0E968B1BC9D1B90ADCC2");/**构造测试报文体对象*/RegBean regBean= newRegBean();
regBean.setReg_sn("REG20150204");
regBean.setUser_id(12);
regBean.setReg_no("33");
regBean.setReg_way("pc");
regBean.setSet_time("20150204 16:18");
regBean.setRet_url("未知");
regBean.setRemarks("无备注");
MsgRegBean msgBean= newMsgRegBean();
List regBeans = new ArrayList();
regBeans.add(regBean);
regBeans.add(regBean);
msgBean.setRegBeans(regBeans);
msgBean.setDataBean(dataBean);
String xmlStr= JaxbObjectAndXmlUtil.object2Xml(msgBean);//构造报文 XML 格式的字符串
System.out.println("对象转xml报文: \n"+xmlStr);
MsgRegBean msgBean2= JaxbObjectAndXmlUtil.xml2Object(xmlStr, MsgRegBean.class);
System.out.println("报文转xml转: \n"+JSON.toJSONString(msgBean2));
}
}运行下测试main函数,可以得到如下的输出:
对象转xml报文:<?xmlversion ="1.0"encoding="utf-8"standalone="yes"?>
N20150204
000001
75CC479AAC09F00BA28F0E968B1BC9D1B90ADCC2
未知
6631383
20150204
xx
v1.0
33
REG20150204
pc
无备注
未知
20150204 16:18
12
33
REG20150204
pc
无备注
未知
20150204 16:18
12
报文转xml转:
{"dataBean":{"batch_no":"N20150204","data_type":"000001","k_sign":"75CC479AAC09F00BA28F0E968B1BC9D1B90ADCC2","msg_sign":"未知","rd_num":"6631383","rd_time":"20150204","user_name":"xx","version":"v1.0"},"regBeans":[{"reg_no":"33","reg_sn":"REG20150204","reg_way":"pc","remarks":"无备注","ret_url":"未知","set_time":"20150204 16:18","user_id":12},{"reg_no":"33","reg_sn":"REG20150204","reg_way":"pc","remarks":"无备注","ret_url":"未知","set_time":"20150204 16:18","user_id":12}]}
代码中间注意点:
Marshaller.JAXB_FORMATTED_OUTPUT :决定是否在转换成xml时同时进行格式化(即按标签自动换行,否则即是一行的xml) 。
Marshaller.JAXB_ENCODING :xml的编码方式。
Marshaller.JAXB_FRAGMENT :是否省略xml头信息
marshal.setProperty(“jaxb.encoding”, “utf-8″); : 设置编码格式 utf-8