将Java类转成xml的工具类_javabean和xml相互转化的工具类

javabean和xml相互转化的工具类

package com.xykj.koala.course.common.util;

import javax.xml.bind.JAXBContext;

import javax.xml.bind.JAXBException;

import javax.xml.bind.Marshaller;

import javax.xml.bind.Unmarshaller;

import java.io.IOException;

import java.io.StringReader;

import java.io.StringWriter;

/**

* javabean和xml相互转化的工具类

* @author junzhongliu

* @date 2018/10/22 18:58

*/

public class JaxbUtil {

/**

* java对象转成xml文件

* @param obj java对象

* @param load 类对象

* @param encoding 编码格式

* @return

* @throws JAXBException

*/

public static String beanToXml(Object obj, Class> load,String encoding) throws JAXBException {

JAXBContext context = JAXBContext.newInstance(load);

Marshaller marshaller = context.createMarshaller();

marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);

marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);

StringWriter writer = new StringWriter();

marshaller.marshal(obj, writer);

return writer.toString();

}

/**

* xml文件转换成java对象

* @param xmlPath xml文件路径

* @param load java对象.class

* @param * @return

* @throws JAXBException

* @throws IOException

*/

@SuppressWarnings("unchecked")

public static T xmlToBean(String xmlPath, Classload) throws JAXBException, IOException {

JAXBContext context = JAXBContext.newInstance(load);

Unmarshaller unmarshaller = context.createUnmarshaller();

return (T) unmarshaller.unmarshal(new StringReader(xmlPath));

}

/**

* JavaBean转换成xml,默认编码UTF-8

* @param obj java对象

* @return

*/

public static String convertToXml(Object obj) {

return convertToXml(obj, "UTF-8");

}

/**

* JavaBean转换成xml

* @param obj java对象

* @param encoding 编码格式

* @return

*/

public static String convertToXml(Object obj, String encoding) {

String result = null;

try {

JAXBContext context = JAXBContext.newInstance(obj.getClass());

Marshaller marshaller = context.createMarshaller();

marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);

StringWriter writer = new StringWriter();

marshaller.marshal(obj, writer);

result = writer.toString();

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

/**

* JavaBean转换成xml去除xml声明部分

* @param obj java对象

* @param encoding 编码

* @return

*/

public static String convertToXmlIgnoreXmlHead(Object obj, String encoding) {

String result = null;

try {

JAXBContext context = JAXBContext.newInstance(obj.getClass());

Marshaller marshaller = context.createMarshaller();

marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);

marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

StringWriter writer = new StringWriter();

marshaller.marshal(obj, writer);

result = writer.toString();

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

/**

* xml转换成JavaBean

* @param xml xml字符串

* @param c java字节码

* @return

*/

@SuppressWarnings("unchecked")

public static T converToJavaBean(String xml, Classc) {

T t = null;

try {

JAXBContext context = JAXBContext.newInstance(c);

Unmarshaller unmarshaller = context.createUnmarshaller();

t = (T) unmarshaller.unmarshal(new StringReader(xml));

} catch (Exception e) {

e.printStackTrace();

}

return t;

}

}

具体的使用方式:

package com.xykj.koala.course.common.data;

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;

import com.xykj.koala.course.common.util.JaxbUtil;

import com.xykj.koala.pay.entity.SignUtil;

import lombok.Data;

import javax.xml.bind.annotation.XmlAccessType;

import javax.xml.bind.annotation.XmlAccessorType;

import javax.xml.bind.annotation.XmlElement;

import javax.xml.bind.annotation.XmlRootElement;

import java.util.Map;

/**

* 支付回调流水,将微信回调回来的xml数据格式化成json格式数据

* * *

*

* *

*

* *

*

* *

*

* *

*

* *

*

* *

*

* *

*

* *

*

* *

*

* *

*

* *

*

* *

*

* *

*

* *

*

* 10

* *

*

* *

*

*

* @author junzhongliu

*/

@Data

@XmlRootElement(name = "xml")

@XmlAccessorType(XmlAccessType.FIELD)

public class WxPayCallBack {

@XmlElement(name = "appid")

private String appid;

@XmlElement(name = "attach")

private String attach;

@XmlElement(name = "bank_type")

private String bank_type;

@XmlElement(name = "cash_fee")

private Integer cash_fee;

@XmlElement(name = "device_info")

private String device_info;

@XmlElement(name = "fee_type")

private String fee_type;

@XmlElement(name = "is_subscribe")

private String is_subscribe;

@XmlElement(name = "mch_id")

private String mch_id;

@XmlElement(name = "nonce_str")

private String nonce_str;

@XmlElement(name = "openid")

private String openid;

@XmlElement(name = "out_trade_no")

private String out_trade_no;

@XmlElement(name = "result_code")

private String result_code;

@XmlElement(name = "sign")

private String sign;

@XmlElement(name = "time_end")

private String time_end;

@XmlElement(name = "total_fee")

private Integer total_fee;

@XmlElement(name = "trade_type")

private String trade_type;

@XmlElement(name = "transaction_id")

private String transaction_id;

@XmlElement(name = "settlement_total_fee")

private Integer settlement_total_fee;

@XmlElement(name = "cash_fee_type")

private String cash_fee_type;

@XmlElement(name = "coupon_fee")

private Integer coupon_fee;

@XmlElement(name = "coupon_count")

private Integer coupon_count;

@XmlElement(name = "sign_type")

private Integer sign_type;

@XmlElement(name = "err_code")

private String err_code;

@XmlElement(name = "err_code_des")

private String err_code_des;

@XmlElement(name = "return_code")

private String return_code;

@XmlElement(name = "return_msg")

private String return_msg;

public static void main(String[] args) throws Exception{

String str = "10";

WxPayCallBack orderInfo = JaxbUtil.converToJavaBean(str, WxPayCallBack.class);

System.out.println(JSONObject.toJSONString(orderInfo));

Mapmap = JSON.parseObject(JSONObject.toJSONString(orderInfo),Map.class);

String koalaSign = SignUtil.createSign(map);

System.out.println(koalaSign);

System.out.println("AADC5246AB379418CAD3073237F452D1".equals(koalaSign));

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值