前后端交互经常会涉及到接口的提供问题,后端提供的接口经常要反馈给前端,在工作中发现给前端提供接口的时候,需要按照一定格式写成xml(或者json文本)导入系统给前端人员使用,但是那么多接口一个个写成xml很烦,然后自己试着引入自定义注解,来将接口生成为xml文件。
自定义注解
- 用于进行接口和参数的注释
注解代码
Information.java
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.PARAMETER, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface Information { public String description() default ""; }
自定义接口转换类
- 将类转化为xml格式文件导出,主要针对springMVC那一套接口
工具类代码
ApiUtils.java
import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import java.lang.annotation.Annotation; import java.lang.reflect.Method; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; public class ApiUtils { public static <T> void transFromClass(Class<T> classtype) { Document document = DocumentHelper.createDocument(); //添加节点信息 Element rootElement = document.addElement("roots"); rootElement.setText("这些接口的类名为:"+classtype.getName()); RequestMapping classRequestMappingAnnotation = classtype.getAnnotation(RequestMapping.class); String rootString = classRequestMappingAnnotation.value()[0]; //获取该类的所有接口 Method[] methods = classtype.getDeclaredMethods(); //声明注解集合 Annotation[] annotations; // 遍历所有的方法得到各方法上面的注解信息 for (Method method : methods) { Element apiElement = rootElement.addElement("api"); //获取方法上的RequestMapping注解信息 RequestMapping requestMapping = method.getAnnotation(RequestMapping.class); //将接口设为<api></api>标签的value属性 apiElement.addAttribute("value", rootString+requestMapping.value()[0]); //获取方法上的Information注解的注释信息构成<description></description> Information apiInformation = method.getAnnotation(Information.class); Element descriptionElement = apiElement.addElement("description"); descriptionElement.setText(apiInformation.description()); //获取方法上的参数信息构成<params></params>标签,里面包含多个<param></param>标签 Element paramsElement = apiElement.addElement("params"); Class<?>[] paramTypes = method.getParameterTypes(); Annotation[][] paramAnnotations = method.getParameterAnnotations(); for (int i = 0; i < paramTypes.length; i++) { Element paramElement = paramsElement.addElement("param"); Element paramType = paramElement.addElement("type"); paramType.setText(paramTypes[i].getName()); Element paramDescription = paramElement.addElement("description"); //将参数上的参数注释输出到<description></description>标签 for (Annotation annotation : paramAnnotations[i]) { if (annotation instanceof Information) { paramDescription.setText(((Information) annotation).description()); } } } } try { OutputFormat format = new OutputFormat(" ", true); Writer fileWriter = new FileWriter("d:\\module.xml"); XMLWriter xmlWriter = new XMLWriter(fileWriter, format); xmlWriter.write(document); xmlWriter.flush(); xmlWriter.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(document.asXML()); } public static void main(String[] args) { transFromClass(TestController.class); } }
测试类(要转换的接口)
TestController.java
@RequestMapping(value = "/report/*") public class TestController { @RequestMapping(value = "/index.action") @Information(description="这个接口用于XXXXX") public String test1(@Information(description="姓名")String name, @Information(description="性别")String sex){ return name; } @RequestMapping(value = "/set.action") @Information(description="这个接口用于OOOOO") public String test2(@Information(description="姓名")String name, int age){ return name; } }
转换结果,生成文件
module.xml
<?xml version="1.0" encoding="UTF-8"?> <roots>这些接口的类名为:TestController <api value="/report/*/index.action"> <description>这个接口用于XXXXX</description> <params> <param> <type>java.lang.String</type> <description>姓名</description> </param> <param> <type>java.lang.String</type> <description>性别</description> </param> </params> </api> <api value="/report/*/set.action"> <description>这个接口用于OOOOO</description> <params> <param> <type>java.lang.String</type> <description>姓名</description> </param> <param> <type>int</type> <description/> </param> </params> </api> </roots>
自定义注解
-关于自定义注解这一块,推荐一篇不错博客
http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html
博主总结的比较细,知识导图也很清晰
问题:没有对接口返回值进行配置,等有时间了想想比较好的办法……