设为首页 加入收藏

TOP

使用Spring自定义注解生产Http接口描述信息(一)
2015-02-02 14:11:30 来源: 作者: 【 】 浏览:47
Tags:使用 Spring 定义 注解 生产 Http 接口 描述 信息

最近在做一个手机后台项目,使用的是SpringMVC,开发的接口是HTTP接口。在接口写完后需要在网页中吧接口的名称测试地址等信息添加到网页中,感觉这样很麻烦还容易漏。于是就写了一个自定义注解通过注解的方式将接口的描述信息加入到接口中,通过注解描述接口信息并且生产接口测试地址


先看使用方法及最终效果


?@ResponseBody
?@RequestMapping("/getBusWaiting")
?@AppInterface(value="获取候车信息",group="test",order=1,params={
? ?@InterfaceParam(name="lineName",desc="线路名称",testValue="B2"),
? ?@InterfaceParam(name="isUpDown",desc="上下行标识",testValue="1"),
? ?@InterfaceParam(name="stationNum",desc="站序",testValue="0"),
? ?@InterfaceParam(name="len",desc="长度",testValue="700")
?})
?public AppResponse getBusWaitingInfo(String lineName,Integer isUpDown,Integer stationNum,Integer len){
? AppResponse result = new AppResponse();
? return result;
?}


生成的效果


使用Spring自定义注解生产Http接口描述信息


下面是具体实现


接口描述类


/**?
?*

创建人:王成委?


?*

创建时间:2014年12月8日 下午5:28:11?


?*

类描述: 标记手机App接口,接口描述信息,用于生成接口信息及测试路径


?*

版权说明: ? 2014 Tiamaes


?*/
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AppInterface {


?/**
? *

方法描述:接口名称


? * @return String
? */
?String value();
?
?/**
? *

方法描述:分组


? * @return String
? */
?String group() default "";
?
?/**
? *

方法描述:排序


? * @return int
? */
?int order () default 0;
?
?/**
? *

方法描述:参数列表


? * @return InterfaceParam[]
? */
?InterfaceParam[] params() default {};
}


接口参数类


/**?
?*

创建人:王成委?


?*

创建时间:2014年12月8日 下午5:29:34?


?*

类描述: 手机App接口参数说明


?*

版权说明: ? 2014 Tiamaes


?*/
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface InterfaceParam {
?
?/**
? *

方法描述:参数名称


? * @return String
? */
?String name();
?
?/**
? *

方法描述:接口说明


? * @return String
? */
?String desc();
?
?/**
? *

方法描述:测试参数值


? * @return String
? */
?String testValue() default "";
}


testValue支持自定义变量,在主类中有具体实现


package com.tiamaes.gjds.app.aop;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.StringUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;


import com.tiamaes.gjds.app.annotation.AppInterface;
import com.tiamaes.gjds.app.annotation.InterfaceParam;
import com.tiamaes.gjds.app.base.RequestMethodMapping;
import com.tiamaes.gjds.app.base.RequestMethodParameter;
import com.tiamaes.gjds.app.base.Variable;
import com.tiamaes.gjds.util.SetUtils;


/**?
?*

类描述: 生成接口描述信息并放入Application中


?*

创建人:王成委?


?*

创建时间:2015年1月19日 下午4:42:24?


?*

版权说明: ? 2015 Tiamaes


?* @see com.tiamaes.gjds.app.annotation.AppInterface
?*/


public class InterfaceAnnotationConfigProcesser implements ApplicationContextAware,InitializingBean{

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android JSON解析数据 下一篇使用IntelliJ IDEA 编译开源的机..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: