SpringBoot集成Swagger2生成Api文档

SpringBoot整合Swagger2

一、添加Swagger2 pom依赖文件

1、此处为根目录下pom依赖

[java]  view plain  copy
  1.   
  2.   2.4.0  
  3.   
[java]  view plain  copy
  1.   
  2.     
  3.                      
  4.       io.springfox  
  5.       springfox-swagger-ui                 
  6.       ${swagger.version}  
  7.       
  8.                      
  9.       io.springfox  
  10.       springfox-swagger2                  
  11.       ${swagger.version}  
  12.       
  13.       
  14.     
  15.   

2、当前工程下依赖

[java]  view plain  copy
  1.   
  2.              
  3.    io.springfox  
  4.    springfox-swagger-ui  
  5.    
  6.               
  7.    io.springfox  
  8.    springfox-swagger2  
  9.    
  10.   

二、新建Swagger2配置类

下面为示例:

[java]  view plain  copy
  1. package com.fanxf;  
  2.   
  3. import org.springframework.context.annotation.Bean;  
  4. import org.springframework.context.annotation.Configuration;  
  5. import springfox.documentation.builders.ApiInfoBuilder;  
  6. import springfox.documentation.builders.PathSelectors;  
  7. import springfox.documentation.builders.RequestHandlerSelectors;  
  8. import springfox.documentation.service.ApiInfo;  
  9. import springfox.documentation.service.Contact;  
  10. import springfox.documentation.spi.DocumentationType;  
  11. import springfox.documentation.spring.web.plugins.Docket;  
  12. import springfox.documentation.swagger2.annotations.EnableSwagger2;  
  13.   
  14. /** 
  15.  * @author fanxf 
  16.  * @date 2018/2/28 10:10 
  17.  */  
  18. @EnableSwagger2  
  19. @Configuration  
  20. public class Swagger {  
  21.   
  22.     @Bean  
  23.     public Docket createRestApi() {  
  24.         return new Docket(DocumentationType.SWAGGER_2)  
  25.                 .apiInfo(apiInfo())  
  26.                 .select()  
  27.                 .apis(RequestHandlerSelectors.basePackage("com.fanxf.service.demo.controller")) //当前包路径  
  28.                 .paths(PathSelectors.any())  
  29.                 .build();  
  30.     }  
  31.     //构建 api文档的详细信息函数  
  32.     private ApiInfo apiInfo() {  
  33.         return new ApiInfoBuilder()  
  34.                 .title("api文档"//页面标题  
  35.                 .contact(new Contact("fanxf","www.fanxf.com","[email protected]")) //创建人  
  36.                 .version("1.0"//版本号  
  37.                 .description("api文档"//描述  
  38.                 .build();  
  39.     }  
  40. }  

讲解:1、@Configuration注解,让Spring来加载该类配置,

          2、@EnableSwagger2注解来启用Swagger2。

          3、通过buildDocket函数创建DocketBean之后,apiInfo()用来创建该Api的基本信息(这些基本信息会展现在文档页面中)。      

          4、select()函数返回一个ApiSelectorBuilder实例用来控制哪些接口暴露给Swagger来展现,本例采用指定扫描的包路径

                来定义,Swagger会扫描该包下所有Controller定义的API,并产生文档内容(除了被@ApiIgnore注解的API)。

三、controller编写

代码如下:

[java]  view plain  copy
  1. package com.fanxf.service.demo.controller;  
  2.   
  3. import com.fanxf.ColorRespDto;  
  4. import com.fanxf.domain.Style;  
  5. import com.fanxf.service.StyleService;  
  6. import io.swagger.annotations.ApiImplicitParam;  
  7. import io.swagger.annotations.ApiOperation;  
  8. import org.apache.commons.lang.StringUtils;  
  9. import org.springframework.beans.factory.annotation.Autowired;  
  10. import org.springframework.web.bind.annotation.GetMapping;  
  11. import org.springframework.web.bind.annotation.PathVariable;  
  12. import org.springframework.web.bind.annotation.RequestMapping;  
  13. import org.springframework.web.bind.annotation.RestController;  
  14.   
  15. import java.util.ArrayList;  
  16. import java.util.List;  
  17.   
  18. /** 
  19.  * @author fanxf 
  20.  * @date 2018/2/6 16:13 
  21.  */  
  22. @RestController  
  23. @RequestMapping("/demo")  
  24. public class ColorController {  
  25.   
  26.     @Autowired  
  27.     private StyleService styleService;  
  28.   
  29.     @ApiOperation(value = "查询车辆颜色", notes = "根据车型id查询所有颜色")  
  30.     @ApiImplicitParam(name = "id", value = "车型id", dataType = "String", paramType = "path")  
  31.     @GetMapping("/color/{id}")  
  32.     public ColorRespDto getList(@PathVariable String id) {  
  33.         Style style = styleService.selectById(id);  
  34.     if (null == style) {  
  35.         return null;  
  36.     }  
  37.         List list = new ArrayList();  
  38.         String[] str = style.getColor().split(".");  
  39.         for (String color : str) {  
  40.             list.add(color);  
  41.         }  
  42.         ColorRespDto dto = new ColorRespDto();  
  43.         dto.setColor(list);  
  44.         return dto;  
  45.     }  
  46. }  

讲解:1、@ApiOperation:用在方法上,说明方法的作用,标注在具体请求上,value和notes的作用差不多,都是对请求进行

                 说明

           2、@ApiImplicitParams:用在方法上包含一组参数说明

             3@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面

              paramType:参数放在哪个地方

              header 请求参数的获取:@RequestHeader

              query 请求参数的获取:@RequestParam

              path(用于restful接口) 请求参数的获取:@PathVariable

              body(不常用)

              form(不常用)

              name:参数名

              dataType:参数类型

              required:参数是否必须传

              value:参数的意思

              defaultValue:参数的默认值

完成controller后启动springboot

启动成功后打开浏览器输入http://localhost:1010/swagger-ui.html(1010为当前启功端口号),就能看到前文所展示的RESTful API的页面。 

如图:

SpringBoot集成Swagger2生成Api文档_第1张图片

可以看到其中的controller


点击然后进行访问:

SpringBoot集成Swagger2生成Api文档_第2张图片

你可能感兴趣的:(Java)