apidocgen项目用法

1、依赖包
<dependency>
        <groupId>com.codegen</groupId>
<artifactId>apidocgen</artifactId>
<version>1.0-SNAPSHOT</version>
    </dependency>
2、controller类中(仅限于rose框架)添加注解

@ApiParamList 参数列表
@ApiResult 结果列表

package com.sohu.controllers;

import org.apache.log4j.Logger;
import org.perf4j.LoggingStopWatch;
import org.perf4j.StopWatch;
import org.perf4j.aop.Profiled;

import com.codegen.annotation.ApiParam;
import com.codegen.annotation.ApiParam.ApiParamList;
import com.codegen.annotation.ApiResult;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;

/**
 * @author qiaowang
 */
@Path("myforum")
public class MyForumController {

    /**
     * @return
     */
    @Get("topic")
    @ApiParamList(value = { @ApiParam(type = "int", isRequired = false,name = "topicId", desc = "主题ID")})
    @ApiResult(returnJson = "{\"data\":[{\"caseId\":5,\"isLike\":false,\"picId\":67,\"picUrl\":\"http://img.itc.cn/focushome/e1/d2/e1d23d8582f9f8c8e63026e4b0489c36.jpg\"},{\"caseId\":11,\"isLike\":false,\"picId\":108,\"picUrl\":\"http://img.itc.cn/focushome/8a/2d/8a2d8c012ca5edff23791ce39ea820f9.jpg\"}],\"errorCode\":0,\"pageNo\":1,\"pageSize\":10,\"pageTotal\":1}")
    public String getTopics() {

        return "topiclist";
    }

    @Get("topic/{topicId:[0-9]+}")
    @ApiParamList(value = { @ApiParam(type = "int", name = "topicId", desc = "主题ID")})
    @ApiResult(returnJson = "{\"data\":[{\"caseId\":5,\"isLike\":false,\"picId\":67,\"picUrl\":\"http://img.itc.cn/focushome/e1/d2/e1d23d8582f9f8c8e63026e4b0489c36.jpg\"},{\"caseId\":11,\"isLike\":false,\"picId\":108,\"picUrl\":\"http://img.itc.cn/focushome/8a/2d/8a2d8c012ca5edff23791ce39ea820f9.jpg\"}],\"errorCode\":0,\"pageNo\":1,\"pageSize\":10,\"pageTotal\":1}")
    public String showTopic(@Param("topicId") int topicId) {
        return "topic";
    }

    /**
     * @param topicId
     * @param commentId
     * @return
     * @throws InterruptedException 
     */
    @Get("topic/{topicId:[0-9]+}/comment/{commentId:[0-9]+}")
    @ApiParamList(value = { @ApiParam(type = "int", name = "topicId", desc = "主题ID"), 
            @ApiParam(type = "int", name = "commentId", desc = "主题ID")})
    @ApiResult(returnJson = "返回结果")
    //@Profiled
    public String showComment(Model model, @Param("topicId") int topicId, @Param("commentId") int commentId) throws InterruptedException {
        Logger rootLogger = Logger.getLogger("fileAppender");
        StopWatch stopWatch = new LoggingStopWatch("showComment", String.valueOf(topicId));
        // for demo purposes just sleep
        Thread.sleep((long) Math.random() * 1000L);
        model.add("name", "wangqiao");
        model.add("commentContent", "sohu");
        stopWatch.lap("firstBlock");
        Thread.sleep((long) Math.random() * 2000L);
        stopWatch.stop("secondBlock");
        return "myforum/comment";
    }

    /**
     * @return
     */
    @Post("topic")
    @ApiParamList(value = { @ApiParam(type = "int", name = "topicId", desc = "主题ID")})
    @ApiResult(returnJson = "{\"data\":[{\"caseId\":5,\"isLike\":false,\"picId\":67,\"picUrl\":\"http://img.itc.cn/focushome/e1/d2/e1d23d8582f9f8c8e63026e4b0489c36.jpg\"},{\"caseId\":11,\"isLike\":false,\"picId\":108,\"picUrl\":\"http://img.itc.cn/focushome/8a/2d/8a2d8c012ca5edff23791ce39ea820f9.jpg\"}],\"errorCode\":0,\"pageNo\":1,\"pageSize\":10,\"pageTotal\":1}")
    public String createTopic() {
        return "topic";
    }

    /**
     * @param topicId
     * @return
     */
    @Post("topic/{topicId:[0-9]+}/comment")
    @ApiParamList(value = { @ApiParam(type = "int", name = "topicId", desc = "主题ID")})
    @ApiResult(returnJson = "{\"data\":[{\"caseId\":5,\"isLike\":false,\"picId\":67,\"picUrl\":\"http://img.itc.cn/focushome/e1/d2/e1d23d8582f9f8c8e63026e4b0489c36.jpg\"},{\"caseId\":11,\"isLike\":false,\"picId\":108,\"picUrl\":\"http://img.itc.cn/focushome/8a/2d/8a2d8c012ca5edff23791ce39ea820f9.jpg\"}],\"errorCode\":0,\"pageNo\":1,\"pageSize\":10,\"pageTotal\":1}")
    public String createComment(@Param("topicId") int topicId) {
        return "comment";
    }

    @Get("topic/jsonlist")
    @ApiParamList(value = { @ApiParam(type = "int", name = "topicId", desc = "主题ID")})
    @ApiResult(returnJson = "{\"data\":[{\"caseId\":5,\"isLike\":false,\"picId\":67,\"picUrl\":\"http://img.itc.cn/focushome/e1/d2/e1d23d8582f9f8c8e63026e4b0489c36.jpg\"},{\"caseId\":11,\"isLike\":false,\"picId\":108,\"picUrl\":\"http://img.itc.cn/focushome/8a/2d/8a2d8c012ca5edff23791ce39ea820f9.jpg\"}],\"errorCode\":0,\"pageNo\":1,\"pageSize\":10,\"pageTotal\":1}")
    public String returnJson() {

        return "@HelloWorld";

    }
}



2、编译工程 打包 (默认会到target/classes中寻找controller)
mvn -U clean compile package

3、在工程中调用 apidocgen 的service方法
public class Test {

    public static void main(String[] args) throws Exception {
        
        String apiPkg = "com.sohu.controllers";
        
        AnnotationService annotationService = new AnnotationService();
        annotationService.createApiDoc(apiPkg);
    }
}


在main方法中执行即可

猜你喜欢

转载自wangqiaowqo.iteye.com/blog/1988683