最近在做一个手机后台项目,使用的是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;
?}
生成的效果

下面是具体实现
接口描述类
/**?
?*
创建人:王成委?
?* 创建时间: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{