java自定义注解实现对象到xml配置的转换

本文介绍了一种通过自定义注解的方式来自动生成接口文档的方法。利用Java注解和反射机制,结合DOM4J库将接口信息转换为XML格式,有效减少手动编写文档的工作量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前后端交互经常会涉及到接口的提供问题,后端提供的接口经常要反馈给前端,在工作中发现给前端提供接口的时候,需要按照一定格式写成xml(或者json文本)导入系统给前端人员使用,但是那么多接口一个个写成xml很烦,然后自己试着引入自定义注解,来将接口生成为xml文件。

自定义注解

  1. 用于进行接口和参数的注释
  2. 注解代码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 "";
    }

自定义接口转换类

  1. 将类转化为xml格式文件导出,主要针对springMVC那一套接口
  2. 工具类代码 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);
        }
    }
  3. 测试类(要转换的接口) 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;
        }
    }
  4. 转换结果,生成文件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

这里写图片描述博主总结的比较细,知识导图也很清晰

问题:没有对接口返回值进行配置,等有时间了想想比较好的办法……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值