设为首页 加入收藏

TOP

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


?private Log logger = LogFactory.getLog(getClass());
?
?private Map> mappers =
? ?new HashMap>();
?
?private WebApplicationContext applicationContext;
?
?/**
? *

方法描述:加载带有{@link com.tiamaes.gjds.app.annotation.AppInterface}注解的接口


? *

首先需要获取所有接口,然后过滤方法中带有@AppInterface


? *

创建人: 王成委?


? *

创建时间: 2015年1月10日 上午10:50:06


? */
?public void loadHandlerMapping(){
? this.logger.info("初始化配置");
? Map handlers = BeanFactoryUtils.beansOfTypeIncludingAncestors(
? ? applicationContext, HandlerMapping.class, true, false);
?
? for(Entry entry : handlers.entrySet()){
? ?HandlerMapping mapping = entry.getValue();
? ?if(mapping instanceof RequestMappingHandlerMapping){
? ? RequestMappingHandlerMapping requestHandler = (RequestMappingHandlerMapping)mapping;
? ? Map handlerMethods = requestHandler.getHandlerMethods();
? ? for(Entry handlerMethod : handlerMethods.entrySet()){
? ? ?AppInterface annotation = handlerMethod.getValue().getMethodAnnotation(AppInterface.class);
? ? ?if(annotation== null)continue;
? ? ?PatternsRequestCondition patternsCondition = handlerMethod.getKey().getPatternsCondition();
? ? ?String requestUrl = SetUtils.first(patternsCondition.getPatterns());
? ? ?this.register(requestUrl, annotation,handlerMethod.getValue().getBeanType());
? ? }
? ?}
? }
?}
?
?/**
? *

方法描述:注册方法


? *

创建人: 王成委?


? *

创建时间: 2015年1月10日 上午10:50:06


? * @param requestUrl
? * @param annotation
? * @param beanType
? */
?private void register(String requestUrl, AppInterface annotation,
? ?Class beanType) {
? String group = annotation.group();
? List groupMappers = this.mappers.get(group);
? if(groupMappers == null)groupMappers = new ArrayList();
? RequestMethodMapping mapper = new RequestMethodMapping();
? mapper.setGroup(group);
? mapper.setController(beanType.getName());
? mapper.setOrder(annotation.order());
? mapper.setName(annotation.value());
? mapper.setUrl(requestUrl);
? mapper.setParams(this.toParameters(annotation.params()));
? groupMappers.add(mapper);
? this.mappers.put(group, groupMappers);
?}
?
?/**
? *

方法描述:读取参数


? *

创建人: 王成委?


? *

创建时间: 2015年1月10日 上午10:50:06


? * @param params
? * @return
? */
?private List toParameters(InterfaceParam[] params){
? List parameters = new ArrayList();
?
? for(InterfaceParam param : params){
? ?RequestMethodParameter bean = new RequestMethodParameter();
? ?bean.setName(param.name());
? ?bean.setDesc(param.desc());
? ?if(StringUtils.startsWithIgnoreCase(param.testValue(), "#")){
? ? String var = param.testValue();
? ? String value = getByVariable(var.substring(var.indexOf("#")+1));
? ? bean.setTestValue(value);
? ?}else{
? ? bean.setTestValue(param.testValue());
? ?}
? ?parameters.add(bean);
? }
? return parameters;
?}
?
?/**
? *

方法描述:获取变量的值


? *

创建人: 王成委?


? *

创建时间: 2015年1月10日 上午10:50:06


? * @param var
? * @return
? */
?private String getByVariable(String var){
? Variable variable = Variable.valueOf(var);
? return variable.getValue();
?}


?/**
? *

方法描述:对接口方法根据分组排序


? *

创建人: 王成委?


? *

创建时间: 2015年1月20日 下午16:00:06


? */
?private void orderMappers(){
? for(Entry> entry : this.mappers.entrySet() ){
? ?Collections.sort(entry.getValue(), new Comparator() {
? ? ?
? ? @Override
? ? public int compare(RequestMethodMapping o1, RequestMethodMapping o2) {
? ? ?Integer one = o1.getOrder();
? ? ?Integer two = o2.getOrder();
? ? ?if(one != null && two != null) return one.intValue()-two.intValue();
? ? ?return 0;
? ? }
? ?});
? }
?}


?@Override
?public void se

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

评论

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