common-swagger 使用说明
chenweida edited this page 7 years ago

swagger版本: 2.7.0

如何在项目中使用?

1.依赖common-swagger工程

        <dependency>
            <groupId>com.yihu.jw</groupId>
            <artifactId>common-swagger</artifactId>
            <version>${版本以项目中最新的版本为主}</version>
        </dependency>
        
        

2.在项目添加swagger配置

@Configuration
@EnableSwagger2  //开启swagger配置
public class SwaggerConfig {
    public static final String default_API = "default";

    @Bean
    public Docket defaultAPI() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName(default_API)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(false)
                .pathMapping("/") //默认从根目录开始扫描
                .select()
                .paths(or(
                        regex("/.*") //扫描的路径
                ))
                .build()
                .apiInfo(defaultApiInfo());
    }

    private ApiInfo defaultApiInfo() {
        ApiInfo apiInfo = new ApiInfo("API", //大标题
                "API,提供基础服务。", //描述
                "1.0",//版本号
                "No terms of service",//服务的url
                "wenfujian@jkzl.com",// 联系人
                "The Apache License, Version 2.0", //许可证
                "http://www.apache.org/licenses/LICENSE-2.0.html" //许可证url
        );

        return apiInfo;
    }

}
        

3.在控制层中使用详细注释例子

@Controller
@RequestMapping(value = "/demo",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(description = "健康教育文章类")  //添加swagger中类的注释
public class EduArticleController extends BaseController{

    @RequestMapping(value = "/getById",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("获取文章详情")  //添加swagger中函数的注释
    public ResultOneModel<ArticleModel> getById(
        //添加swagger中入参的注释  name是名字  value是中文注释
        @ApiParam(name = "articleId",value = "文章ID")@RequestParam(value = "articleId")String articleId){
       
    }
}