使用xfire webservice接口开发,obj与xml相互转换好用工具类,不需要写大量的转换代码,亲测可用
生活随笔
收集整理的這篇文章主要介紹了
使用xfire webservice接口开发,obj与xml相互转换好用工具类,不需要写大量的转换代码,亲测可用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
webservice接口開發,舊工程中存在使用xfire開發的接口,對象轉換為xml和xml轉換為對象的時候需要些大量的代碼,工作量很大?,F在提供一個比較好的對象轉換為xml的工具。
<!-- https://mvnrepository.com/artifact/commons-betwixt/commons-betwixt --><dependency><groupId>commons-betwixt</groupId><artifactId>commons-betwixt</artifactId><version>0.8</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.jdom</groupId><artifactId>jdom2</artifactId><version>2.0.6</version></dependency>舊代碼:對象轉xml 及 xml轉對象寫法,需要寫大量的轉換代碼, 如下:
Ret 對象
package com.gblfy;import org.jdom2.Element; import org.jdom2.Namespace;public class Ret {private String code = ""; // 如果成功返回true,失敗返回falseprivate String message = ""; // 如果成功返回操作成功,失敗則返回失敗原因private static final String RET_FORMAT = "<Ret><code>%s</code><message>%s</message></Ret>";public String packBody() {return String.format(RET_FORMAT, this.getCode(), this.getMessage());}public void unpackBody(Element element) {Namespace nameSpace = element.getNamespace();this.setCode(element.getChildText("code", nameSpace));this.setMessage(element.getChildText("message", nameSpace));}public String getCode() {return code;}public void setCode(String code) {this.code = code;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}}StepMindModel 對象
package com.gblfy;import lombok.Data;@Data public class StepMindModel {private String stepMind;private String stepName; }Response對象
package com.gblfy;import lombok.Data;import java.util.List;@Data public class Response {List<StepMindModel> list;Ret ret; }現在完全可以使用apache提供的包進行轉換,使用jar:commons-betwixt-0.8.jar
工具轉換演示代碼如下:
package com.gblfy;import lombok.extern.slf4j.Slf4j; import org.apache.commons.betwixt.io.BeanReader; import org.apache.commons.betwixt.io.BeanWriter;import java.io.*; import java.util.ArrayList; import java.util.List;@Slf4j public class XmlParseUtil {//根標簽 根據需求定義private static final String XML_ROOT_NODE_NAME = "response";//xml header頭部public static final String XML_HEADER = "<?xml version='1.0' encoding='UTF-8' ?>"; // public static final String XML_HEADER = "";/*** 將XML字符串 轉換成 對象** @param strInMsg : XML內容* @param clazz* @return*/public static Object xml2Obj(String strInMsg,@SuppressWarnings("rawtypes") Class clazz) {BeanReader beanReader = new BeanReader();Object parse = null;StringReader xmlReader = new StringReader(strInMsg);beanReader.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);beanReader.getBindingConfiguration().setMapIDs(false);try {beanReader.registerBeanClass(XML_ROOT_NODE_NAME, clazz);parse = beanReader.parse(xmlReader);} catch (Exception e) {log.error("", e);}return parse;}/*** 將 對象 轉換成 XML字符串** @param inObj* @return*/public static String obj2xml(Object inObj) {StringWriter sw = new StringWriter();BeanWriter beanWriter = new BeanWriter(sw);sw.write(XML_HEADER);try {beanWriter.setEndOfLine("");beanWriter.getBindingConfiguration().setMapIDs(false);beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);beanWriter.write(XML_ROOT_NODE_NAME, inObj);} catch (Exception e) {log.error("", e);}return sw.toString();}/*** 將XML文件轉換成 對象** @param fileName* @param clazz* @return*/public static Object file2Object(String fileName, Class clazz) {String fileContent = file2String(fileName);return xml2Obj(fileContent, clazz);}/*** 將XML文件轉換成 對象** @param file* @param clazz* @return*/public static Object file2Object(File file, Class clazz) {String fileContent = file2tring(file);return xml2Obj(fileContent, clazz);}/*** 讀取文件全部內容** @param fileName* @return*/private static String file2String(String fileName) {File file = new File(fileName);return file2tring(file);}/*** 讀取文件全部內容** @param file* @return*/private static String file2tring(File file) {String encoding = "ISO-8859-1";Long filelength = file.length();byte[] filecontent = new byte[filelength.intValue()];try {FileInputStream in = new FileInputStream(file);in.read(filecontent);in.close();} catch (FileNotFoundException e) {log.error("", e);} catch (IOException e) {log.error("", e);}try {return new String(filecontent, encoding);} catch (UnsupportedEncodingException e) {log.error("", e);return null;}}//----------------------------測試方法-------------------------------public static void main(String[] args) {Ret obj = new Ret();obj.setCode("true");obj.setMessage("成功");//對象轉換為xml:<response><code>true</code><message>成功</message></response>String xmlString = XmlParseUtil.obj2xml(obj);System.out.println("對象轉換為xml:" + xmlString);Ret obj2 = (Ret) XmlParseUtil.xml2Obj(xmlString, Ret.class);//xml轉換為對象:true==成功System.out.println("xml轉換為對象:" + obj2.getCode() + "==" + obj2.getMessage());Response response = new Response();List<StepMindModel> list = new ArrayList<StepMindModel>();StepMindModel model = new StepMindModel();model.setStepMind("同意");model.setStepName("三級部門經理審核");StepMindModel model2 = new StepMindModel();model2.setStepMind("同意");model2.setStepName("二級部門經理審核");list.add(model);list.add(model2);response.setList(list);response.setRet(obj);//對象轉換為xml: <?xml version='1.0' encoding='UTF-8' ?><response><list><StepMindModel><stepMind>同意</stepMind><stepName>三級部門經理審核</stepName></StepMindModel><StepMindModel><stepMind>同意</stepMind><stepName>二級部門經理審核</stepName></StepMindModel></list><ret><code>true</code><message>成功</message></ret></response>System.out.println("對象轉換為xml:" + XmlParseUtil.obj2xml(response));} }總結
以上是生活随笔為你收集整理的使用xfire webservice接口开发,obj与xml相互转换好用工具类,不需要写大量的转换代码,亲测可用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据装载指定一张表或者多张表直接装载到目
- 下一篇: SprinBoot 集成 Flowabl