标签:
在我们的日常业务系统开发过程中,随着业务的发展,我们经常需要与外围系统进行接口对接,用以获得对方的业务能力或者将自己的业务能力提供给对方,本文主要介绍外围系统的接口调用的介绍和统一调用的设计与实现。
public class JsonUtils { private static ObjectMapper objectMapper = new ObjectMapper(); /** * Json内容转化为对象 * @param content * @param valueType * @param <T> * @return * @throws IOException */ public static <T> T readValue(String content, Class<T> valueType) throws IOException { return objectMapper.readValue(content,valueType); } /** * 对象转化为Json内容 * @param t * @param <T> * @return * @throws IOException */ public static <T> String writeValueAsString(T t) throws IOException { return objectMapper.writeValueAsString(t); } }
public class XmlUtils { /** * XML内容转化为对象 * @param content * @param valueType * @param <T> * @return */ public static <T> T readValue(String content, Class<T> valueType) { XStream xstream = new XStream(new DomDriver()); xstream.processAnnotations(valueType); return (T) xstream.fromXML(content); } /** * 对象转化为XML内容 * @param t * @param <T> * @return * @throws IOException */ public static <T> String writeValueAsString(T t) throws IOException { XStream xstream = new XStream(new DomDriver("utf8")); xstream.processAnnotations(t.getClass());// 识别obj类中的注解 // 以格式化的方式输出XML return xstream.toXML(t); } }
/** * 通过Json数据生成Bean对象 * @param packagePath * @param rootClassName * @param jsonString */ public static void generateByJson(String packagePath, String rootClassName, String jsonString) throws IOException { JsonNode jsonNode = JacksonObjectMapper.getInstance().readTree(jsonString); Map<String, Object> mergeMap = new HashMap<String, Object>(); mergeMap.put(rootClassName, parseJsonNode(jsonNode)); generateClassByJson(packagePath, rootClassName, mergeMap.get(rootClassName), true); }
/** * 解析Json节点信息 * @param jsonNode * @return */ public static Object parseJsonNode(JsonNode jsonNode) { if(jsonNode.isArray()) { List result = new ArrayList(); for (JsonNode subNode : jsonNode) { Map<String, Object> fieldMap = (Map<String, Object>)parseJsonNode(subNode); mergeField(result, fieldMap); } return result; } Map<String, Object> fieldMap = new LinkedHashMap<String, Object>(); Iterator<Map.Entry<String, JsonNode>> fields = jsonNode.fields(); while (fields.hasNext()) { Map.Entry<String, JsonNode> field = fields.next(); fieldMap.put(field.getKey(), parseJsonNode(field.getValue())); } if(fieldMap.size() == 0) { return jsonNode.asText(); } return fieldMap; }
/** * 生成类文件 * @param packagePath * @param rootClassName * @param data * @param isRoot */ private static void generateClassByJson(String packagePath, String rootClassName, Object data, boolean isRoot) { com.hframe.generator.bean.Class beanClass = new com.hframe.generator.bean.Class(); beanClass.setSrcFilePath("E:\\xfb_workspace\\boomshare\\bs-xfb-wx\\src\\main\\java\\"); beanClass.setClassPackage(packagePath); beanClass.setClassName(rootClassName); beanClass.addConstructor(); Map<String, Object> dataMap = new LinkedHashMap<String, Object>(); if(data instanceof Map) { dataMap = (Map<String, Object>) data; }else if(data instanceof List){ dataMap = (Map<String, Object>) ((List) data).get(0); }else { return ; } beanClass.addImportClass("com.fasterxml.jackson.annotation.JsonProperty"); for (String fieldName : dataMap.keySet()) { Field field = getField(fieldName, dataMap.get(fieldName)); field.addFieldAnno("@JsonProperty(\"" + fieldName + "\")"); beanClass.addField(field); if(!"String".equals(field.getType())) { if(field.getType().startsWith("List<") && !beanClass.getImportClassList().contains("java.util.List")) { beanClass.addImportClass("java.util.List"); } if(isRoot) { beanClass.addImportClass(packagePath + "." + CreatorUtil.getJavaClassName(rootClassName).toLowerCase() + ".*"); } generateClassByJson(packagePath + (isRoot ? ("." + CreatorUtil.getJavaClassName(rootClassName).toLowerCase()) : ""), CreatorUtil.getJavaClassName(fieldName), dataMap.get(fieldName), false); } } Map map = new HashMap(); map.put("CLASS", beanClass); String content = VelocityUtil.produceTemplateContent("com/hframe/generator/vm/poByTemplate.vm", map); System.out.println(content); FileUtils.writeFile(beanClass.getFilePath(), content); }
/** * 通过Xml数据生成Bean对象 * @param packagePath * @param rootClassName * @param xmlString */ public static void generateByXml(String packagePath, String rootClassName,String rootXmlName, String xmlString) throws IOException { Document document = Dom4jUtils.getDocumentByContent(xmlString); Element root = document.getRootElement(); Map<String, Object> mergeMap = new HashMap<String, Object>(); mergeMap.put(rootClassName, parseXmlNode(root)); generateClassByXml(packagePath, rootClassName, rootXmlName, mergeMap.get(rootClassName), true); }
/** * 解析XML节点信息 * @param element * @return */ private static Object parseXmlNode(Element element) { if(checkElementIsArray(element)) { List result = new ArrayList(); String xmlElementName = null; for (Object o : element.elements()) { Element subElement = (Element) o; xmlElementName = subElement.getName();//子元素名称 Map<String, Object> fieldMap = (Map<String, Object>)parseXmlNode(subElement); mergeField(result, fieldMap); } result.add(xmlElementName); return result; } Map<String, Object> fieldMap = new LinkedHashMap<String, Object>(); for (Object o : element.elements()) { Element subElement = (Element) o; fieldMap.put(subElement.getName(), parseXmlNode(subElement)); } if(fieldMap.size() == 0) { return element.getTextTrim(); } return fieldMap; }
/** * 生成类文件 * @param packagePath * @param rootClassName * @param rootXmlName * @param data * @param isRoot */ private static void generateClassByXml(String packagePath, String rootClassName,String rootXmlName, Object data, boolean isRoot) { com.hframe.generator.bean.Class beanClass = new com.hframe.generator.bean.Class(); beanClass.setSrcFilePath("E:\\xfb_workspace\\boomshare\\bs-xfb-wx\\src\\main\\java\\"); beanClass.setClassPackage(packagePath); beanClass.setClassName(rootClassName); beanClass.addConstructor(); beanClass.addAnnotation("@XStreamAlias(\"" + rootXmlName + "\")"); Map<String, Object> dataMap = new LinkedHashMap<String, Object>(); if(data instanceof Map) { dataMap = (Map<String, Object>) data; }else if(data instanceof List){ dataMap = (Map<String, Object>) ((List) data).get(0); }else { return ; } beanClass.addImportClass("com.thoughtworks.xstream.annotations.XStreamAlias"); beanClass.addImportClass("com.thoughtworks.xstream.annotations.XStreamAsAttribute"); for (String fieldName : dataMap.keySet()) { String subElementName = getSubElementName(dataMap.get(fieldName)); Field field = getField(fieldName, dataMap.get(fieldName), subElementName); field.addFieldAnno("@XStreamAlias(\"" + fieldName + "\")"); beanClass.addField(field); if(!"String".equals(field.getType())) { if(field.getType().startsWith("List<") && !beanClass.getImportClassList().contains("java.util.List")) { beanClass.addImportClass("java.util.List"); } if(isRoot) { beanClass.addImportClass(packagePath + "." + CreatorUtil.getJavaClassName(rootClassName).toLowerCase() + ".*"); } if(subElementName != null) { generateClassByXml(packagePath + (isRoot ? ("." + CreatorUtil.getJavaClassName(rootClassName).toLowerCase()) : ""), CreatorUtil.getJavaClassName(subElementName), subElementName, dataMap.get(fieldName), false); }else { generateClassByXml(packagePath + (isRoot ? ("." + CreatorUtil.getJavaClassName(rootClassName).toLowerCase()) : ""), CreatorUtil.getJavaClassName(fieldName), fieldName, dataMap.get(fieldName), false); } } } Map map = new HashMap(); map.put("CLASS", beanClass); String content = VelocityUtil.produceTemplateContent("com/hframe/generator/vm/poByTemplate.vm", map); System.out.println(content); FileUtils.writeFile(beanClass.getFilePath(), content); }
{ "button":[ { "type":"click", "name":"今日歌曲", "key":"V1001_TODAY_MUSIC" }, { "name":"菜单", "sub_button":[ { "type":"view", "name":"搜索", "url":"http://www.soso.com/" }, { "type":"view", "name":"视频", "url":"http://v.qq.com/" }, { "type":"click", "name":"赞一下我们", "key":"V1001_GOOD" }] }] }
public static void main(String[] args) throws IOException { String rootClassPath = Thread.currentThread().getContextClassLoader ().getResource("").getPath(); String jsonString = FileUtils.readFile(rootClassPath + "test.json"); generateByJson("com.wechat.bean.request","Menu",jsonString); }
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY) public class Menu { @JsonProperty("button") private List<Button> buttonList; public Menu() { } public List<Button> getButtonList() { return buttonList; } public void setButtonList(List<Button> buttonList) { this.buttonList = buttonList; } }
public class Button { @JsonProperty("type") private String type; @JsonProperty("name") private String name; @JsonProperty("key") private String key; @JsonProperty("sub_button") private List<SubButton> subButtonList; public Button() { } public String getType(){ return type; } public void setType(String type){ this.type = type; } public String getName(){ return name; } public void setName(String name){ this.name = name; } public String getKey(){ return key; } public void setKey(String key){ this.key = key; } public List<SubButton> getSubButtonList(){ return subButtonList; } public void setSubButtonList(List<SubButton> subButtonList){ this.subButtonList = subButtonList; } }
public class SubButton { @JsonProperty("type") private String type; @JsonProperty("name") private String name; @JsonProperty("url") private String url; @JsonProperty("key") private String key; public SubButton() { } public String getType(){ return type; } public void setType(String type){ this.type = type; } public String getName(){ return name; } public void setName(String name){ this.name = name; } public String getUrl(){ return url; } public void setUrl(String url){ this.url = url; } public String getKey(){ return key; } public void setKey(String key){ this.key = key; } }
public static void main(String[] args) throws IOException { String rootClassPath = Thread.currentThread().getContextClassLoader ().getResource("").getPath(); String jsonString = readFile(rootClassPath + "test.json"); Menu menu = readValue(jsonString, Menu.class); System.out.println(writeValueAsString(menu)); }
{"button":[{"type":"click","name":"今日歌曲","key":"V1001_TODAY_MUSIC","sub_button":null},{"type":null,"name":"菜单","key":null,"sub_button":[{"type":"view","name":"搜索","url":"http://www.soso.com/","key":null},{"type":"view","name":"视频","url":"http://v.qq.com/","key":null},{"type":"click","name":"赞一下我们","url":null,"key":"V1001_GOOD"}]}]}
<persons> <type>001</type> <listPerson> <person> <name>6666554</name> <sex>lavasoft</sex> <tel>man</tel> <addes> <address> <addType>type1</addType> <place>郑州市经三路财富广场1</place> </address> <address> <addType>type2</addType> <place>郑州市经三路财富广场2</place> </address> </addes> </person> <person> <name>7777754</name> <sex>yutian</sex> <tel>man</tel> <addes> <address> <addType>type3</addType> <place>郑州市经三路财富广场3</place> </address> <address> <addType>type4</addType> <place>郑州市经三路财富广场4</place> </address> </addes> </person> </listPerson> </persons>
String xmlString = FileUtils.readFile(rootClassPath + "test.xml");
generateByXml("com.wechat.bean.request","Persons","persons",xmlString);
@XStreamAlias("persons") public class Persons { @XStreamAlias("type") private String type; @XStreamAlias("listPerson") private List<Person> personList; public Persons() { } public String getType(){ return type; } public void setType(String type){ this.type = type; } public List<Person> getPersonList(){ return personList; } public void setPersonList(List<Person> personList){ this.personList = personList; } }
@XStreamAlias("person") public class Person { @XStreamAlias("name") private String name; @XStreamAlias("sex") private String sex; @XStreamAlias("tel") private String tel; @XStreamAlias("addes") private List<Address> addressList; public Person() { } public String getName(){ return name; } public void setName(String name){ this.name = name; } public String getSex(){ return sex; } public void setSex(String sex){ this.sex = sex; } public String getTel(){ return tel; } public void setTel(String tel){ this.tel = tel; } public List<Address> getAddressList(){ return addressList; } public void setAddressList(List<Address> addressList){ this.addressList = addressList; } }
@XStreamAlias("address") public class Address { @XStreamAlias("addType") private String addtype; @XStreamAlias("place") private String place; public Address() { } public String getAddtype(){ return addtype; } public void setAddtype(String addtype){ this.addtype = addtype; } public String getPlace(){ return place; } public void setPlace(String place){ this.place = place; } }
public static void main(String[] args) throws IOException { String rootClassPath = Thread.currentThread().getContextClassLoader ().getResource("").getPath(); String xmlString = readFile(rootClassPath + "test.xml"); System.out.println(xmlString); Persons person = readValue(xmlString, Persons.class); System.out.println(writeValueAsString(person)); }
<persons> <type>001</type> <listPerson> <person> <name>6666554</name> <sex>lavasoft</sex> <tel>man</tel> <addes> <address> <addType>type1</addType> <place>郑州市经三路财富广场1</place> </address> <address> <addType>type2</addType> <place>郑州市经三路财富广场2</place> </address> </addes> </person> <person> <name>7777754</name> <sex>yutian</sex> <tel>man</tel> <addes> <address> <addType>type3</addType> <place>郑州市经三路财富广场3</place> </address> <address> <addType>type4</addType> <place>郑州市经三路财富广场4</place> </address> </addes> </person> </listPerson> </persons>
标签:
原文地址:http://www.cnblogs.com/hframe/p/5152088.html