本篇文章給大家分享的是有關(guān)xml字符串怎樣轉(zhuǎn)換成Java對(duì)象,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
站在用戶的角度思考問題,與客戶深入溝通,找到雨花臺(tái)網(wǎng)站設(shè)計(jì)與雨花臺(tái)網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名與空間、雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋雨花臺(tái)地區(qū)。
controller里接收發(fā)送方的請(qǐng)求: @RequestBody關(guān)鍵字里的東西,就是http請(qǐng)求的報(bào)文
@XmlRootElement //@XmlRootElement關(guān)鍵字必須要有 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報(bào)文加解密:
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字符串怎樣轉(zhuǎn)換成Java對(duì)象,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。