公司项目要求解析出RESTFull风格发布的WebService接口的所有请求方法,参数,参数类型,已经方法是返回类型,想来想去都不知道该怎么办,思来想去然后又研究RESTFull风格WebService的xml文件,发现其实对于Java简单类型的 方法是能在xml文件中体现的,但是对于复杂类型,比如参数是一个对象,或者返回值是一个对象或者List,Map等。这些返回类型在xml文件中的 response下representation这个节点的属性type会是一个"application/json"。对于这些返回类型或者参数我没法 知道是什么类型,所以我就默认给这些Object类型。对于void类型的方法,会在response的status属性等于204,所以我就笨笨的用HttpClient抓去整个xml文件,然后用dome4j解析这个xml,幸好的是我在项目只需要解析简单类型的方法,所以想想也能达到要求.
1 package xxx.xxx.common; 2 3 import java.util.List; 4 5 import org.apache.commons.httpclient.HttpClient; 6 import org.apache.commons.httpclient.HttpStatus; 7 import org.apache.commons.httpclient.methods.GetMethod; 8 import org.dom4j.Document; 9 import org.dom4j.DocumentException; 10 import org.dom4j.DocumentHelper; 11 import org.dom4j.Element; 12 13 import com.google.common.collect.Lists; 14 import com.qya.vo.transform.TransformParamVO; 15 import com.qya.vo.transform.TransformVO; 16 17 import com.qya.utils.string.StringUtil; 18 19 public class TransformWsUrlUtil { 20 21 protected final String DEFAULT_URL = "http://localhost:8080/bpms/services?_wadl"; 22 23 public static List<TransformVO> transform(String url) { 24 String xml = getTransformXml(url); 25 Document doc = null; 26 try { 27 doc = DocumentHelper.parseText(xml); 28 } catch (DocumentException e) { 29 e.printStackTrace(); 30 } 31 List<TransformVO> lst = Lists.newArrayList(); 32 String rootPath = ""; 33 Element resourcesEl = doc.getRootElement().element("resources"); 34 if (resourcesEl.hasContent()) { 35 List<Element> els = resourcesEl.elements(); 36 for (Element resEl : els) { 37 rootPath = resEl.attribute(0).getText(); 38 if (resEl.hasContent()) { 39 lst.addAll(getMethod(rootPath, resEl.elements())); 40 } 41 } 42 } 43 return lst; 44 } 45 46 // 所有方法列表 47 public static List<TransformVO> getMethod(String rootPath, List<Element> els) { 48 List<TransformVO> lst = Lists.newArrayList(); 49 String wsPath = ""; 50 String returnType = ""; 51 for (Element el : els) { 52 returnType = transform2JavaType(getReturnType(el)); 53 wsPath = el.attribute("path").getText(); 54 if(!"java.lang.Object".equals(returnType)){ //此处我做了判断,只需要简单类型和void类型的方法 55 TransformVO vo = new TransformVO(); 56 vo.setUrl(transform2JavaType(rootPath + wsPath)); 57 vo.setUrlMethodName(transform2JavaType(returnType + " " + replaceSeparatorLine(wsPath) + getParamters(el))); 58 vo.setMethodName(transform2JavaType(wsPath)); 59 vo.setReturnType(returnType); 60 List<TransformParamVO> paramLst = getParamterVO(el); 61 vo.setTransformParamLst(paramLst); 62 lst.add(vo); 63 } 64 } 65 return lst; 66 } 67 68 /** 69 * 获取方法的所有参数 70 * 71 * @param el 72 * @return 参数字符串 73 */ 74 public static String getParamters(Element el) { 75 StringBuffer buf = new StringBuffer("("); 76 Element requestEl = el.element("method").element("request"); 77 if (!StringUtil.isNullOrEmpty(requestEl)) { 78 if (requestEl.hasContent()) { 79 for (Element paramEl : (List<Element>) requestEl.elements()) { 80 if ("param".equals(paramEl.getName())) { 81 buf.append(paramEl.attributeValue("type") + " " + paramEl.attributeValue("name") + ", "); 82 } 83 if ("representation".equals(paramEl.getName())) { 84 if (paramEl.hasContent()) { 85 for (Element pEl : (List<Element>) paramEl.elements()) { 86 buf.append(pEl.attributeValue("type") + " " + pEl.attributeValue("name") + ", "); 87 } 88 } 89 } 90 } 91 } 92 } 93 buf.append(")"); 94 return buf.toString(); 95 } 96 97 public static List<TransformParamVO> getParamterVO(Element el) { 98 List<TransformParamVO> lst = Lists.newArrayList(); 99 Element requestEl = el.element("method").element("request"); 100 if (!StringUtil.isNullOrEmpty(requestEl)) { 101 if (requestEl.hasContent()) { 102 for (Element paramEl : (List<Element>) requestEl.elements()) { 103 if ("param".equals(paramEl.getName())) { 104 TransformParamVO vo = new TransformParamVO(); 105 vo.setName(paramEl.attributeValue("name")); 106 vo.setType(transform2JavaType(paramEl.attributeValue("type"))); 107 lst.add(vo); 108 } 109 if ("representation".equals(paramEl.getName())) { 110 if (paramEl.hasContent()) { 111 for (Element pEl : (List<Element>) paramEl.elements()) { 112 TransformParamVO vo = new TransformParamVO(); 113 vo.setName(pEl.attributeValue("name")); 114 vo.setType(transform2JavaType(pEl.attributeValue("type"))); 115 lst.add(vo); 116 } 117 } 118 } 119 } 120 } 121 } 122 return lst; 123 } 124 125 /* 126 * 获取返回类型 127 */ 128 public static String getReturnType(Element el) { 129 String returnType = ""; 130 List<Element> returnTypeEls = el.element("method").element("response").elements("representation"); 131 if (!StringUtil.isNullOrEmpty(returnTypeEls)) { 132 if (returnTypeEls.isEmpty()) { 133 if (!StringUtil.isNullOrEmpty(el.element("method").element("response").attribute("status"))) { 134 if ("204".equals(el.element("method").element("response").attribute("status").getText())) { 135 returnType = "void"; 136 } 137 } 138 } else { 139 for (Element returnTypeEl : returnTypeEls) { 140 if (returnTypeEl.hasContent()) { 141 for (Element pEl : (List<Element>) returnTypeEl.elements()) { 142 returnType = pEl.attributeValue("type"); 143 } 144 } else { 145 returnType = "java.lang.Object"; 146 } 147 } 148 } 149 } 150 return returnType; 151 } 152 153 private static String transform2JavaType(String str) { 154 if (!StringUtil.isNullOrEmpty(str)) { 155 if (str.contains("xs:int")) { 156 str = str.replaceAll("xs:int", "java.lang.Integer"); 157 } 158 if (str.contains("xs:string")) { 159 str = str.replaceAll("xs:string", "java.lang.String"); 160 } 161 if (str.contains("xs:boolean")) { 162 str = str.replaceAll("xs:boolean", "java.lang.Boolean"); 163 } 164 if (str.contains("xs:double")) { 165 str = str.replaceAll("xs:double", "java.lang.Double"); 166 } 167 if (str.contains("xs:dateTime")) { 168 str = str.replaceAll("xs:dateTime", "java.util.Date"); 169 } 170 if (str.lastIndexOf(",") > 0) { 171 str = str.substring(0, str.lastIndexOf(",")) + ")"; 172 } 173 } 174 return str; 175 } 176 private static String replaceSeparatorLine(String str) { 177 if (!StringUtil.isNullOrEmpty(str)) { 178 if (str.contains("/")) { 179 str = str.replaceAll("\\/", ""); 180 } 181 } 182 return str; 183 } 184 185 /** 186 * 后去需要转换的xml内容 187 * 188 * @param url 189 * 访问的webservice url 190 * @return xml内容 191 */ 192 private static String getTransformXml(String url) { 193 String xml = ""; 194 HttpClient httpClient = new HttpClient(); 195 GetMethod getMethod = new GetMethod(url); 196 try { 197 int statusCode = httpClient.executeMethod(getMethod); 198 if (statusCode != HttpStatus.SC_OK) { 199 System.err.println("Method failed: " + getMethod.getStatusLine()); 200 } 201 // 读取内容 202 byte[] responseBody = getMethod.getResponseBody(); 203 // 处理内容 204 xml = new String(responseBody); 205 } catch (Exception e) { 206 System.err.println("页面无法访问"); 207 } finally { 208 getMethod.releaseConnection(); 209 } 210 return xml; 211 } 212 213 }
TransformVO.java
1 package com.qya.vo.transform; 2 3 import java.io.Serializable; 4 import java.util.List; 5 6 import com.google.common.collect.Lists; 7 8 public class TransformVO implements Serializable { 9 10 private static final long serialVersionUID = -8192032660217273046L; 11 private String url; 12 private String urlMethodName; 13 private String methodName; 14 private String returnType; 15 private List<TransformParamVO> transformParamLst = Lists.newArrayList(); 16 public String getUrl() { 17 return url; 18 } 19 public void setUrl(String url) { 20 this.url = url; 21 } 22 public String getUrlMethodName() { 23 return urlMethodName; 24 } 25 public void setUrlMethodName(String urlMethodName) { 26 this.urlMethodName = urlMethodName; 27 } 28 public String getMethodName() { 29 return methodName; 30 } 31 public void setMethodName(String methodName) { 32 this.methodName = methodName; 33 } 34 public String getReturnType() { 35 return returnType; 36 } 37 public void setReturnType(String returnType) { 38 this.returnType = returnType; 39 } 40 public List<TransformParamVO> getTransformParamLst() { 41 return transformParamLst; 42 } 43 public void setTransformParamLst(List<TransformParamVO> transformParamLst) { 44 this.transformParamLst = transformParamLst; 45 } 46 }
TransformParamVO.java
1 package com.qya.vo.transform; 2 3 import java.io.Serializable; 4 5 public class TransformParamVO implements Serializable { 6 7 private static final long serialVersionUID = 2728404017781534263L; 8 private String type; 9 private String name; 10 public String getType() { 11 return type; 12 } 13 public void setType(String type) { 14 this.type = type; 15 } 16 public String getName() { 17 return name; 18 } 19 public void setName(String name) { 20 this.name = name; 21 } 22 23 }
StringUtil.java
1 package com.qya.utils.string.StringUtil 2 3 public class StringUtil { 4 5 /** 6 * 判断是否为空后null 7 * 8 * @param obj 9 * 对象 10 * @return boolean 11 * @since 1.0 12 */ 13 public static boolean isNullOrEmpty(Object obj) { 14 return obj == null || "".equals(obj.toString()); 15 } 16 }