共计 1686 个字符,预计需要花费 5 分钟才能阅读完成。
本篇文章给大家分享的是有关 xml 字符串怎样转换成 Java 对象,丸趣 TV 小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着丸趣 TV 小编一起来看看吧。
controller 里接收发送方的请求:@RequestBody 关键字里的东西,就是 http 请求的报文
@XmlRootElement
//@XmlRootElement 关键字必须要有
public class User implements Serializable{
private String userId;
public void setUserId(String userId) {
this.userId = userId;
public void setUserName(String userName) {
this.userName = userName;
public static String beanToXml(Object obj, Class ? load) throws JAXBException {JAXBContext context = JAXBContext.newInstance(load);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, GBK
StringWriter writer = new StringWriter();
marshaller.marshal(obj, writer);
return writer.toString();
public static Object xmlToBean(String xmlStr, Class ? load) throws JAXBException, IOException {JAXBContext context = JAXBContext.newInstance(load);
Unmarshaller unmarshaller = context.createUnmarshaller();
Object object = unmarshaller.unmarshal(new StringReader(xmlStr));
return object;
}
xml 报文加解密:
private static String decode(String key, String data) {
try {byte[] bytes = Base64.getDecoder().decode(key);
byte[] databytes = Base64.getDecoder().decode(data);
Cipher cipher = Cipher.getInstance( AES
SecureRandom random = SecureRandom.getInstance( SHA1PRNG
random.setSeed(bytes);
KeyGenerator keyGen = KeyGenerator.getInstance( AES
keyGen.init(128, random);
SecretKey secret = keyGen.generateKey();
cipher.init(Cipher.DECRYPT_MODE, secret);
return new String(cipher.doFinal(databytes));
} catch (Exception e) {log.debug(e.getMessage());
return null;
}
以上就是 xml 字符串怎样转换成 Java 对象,丸趣 TV 小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注丸趣 TV 行业资讯频道。
正文完