本篇文章给大家分享的是有关xml字符串怎样转换成Java对象,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
controller里接收发送方的请求: @RequestBody关键字里的东西,就是http请求的报文
@XmlRootElement //@XmlRootElement关键字必须要有 public class User implements Serializable{ private String userId; private String userName; public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getUserName() { return userName; } 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对象,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。