springboot整合swagger

68 阅读1分钟

第一步导入依赖

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.6.1</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.6.1</version>
</dependency>

第二步

注意事项: 1 @EnableSwagger2 // 开启swagger 2 选择controller路径 外部是访问controller嘛 (在第二方法中

                   .apis(RequestHandlerSelectors.basePackage(
                    "com.csy.springbootpro1.controller"))
@Configuration
@EnableSwagger2 // 开启swagger
public class SwaggerConfiguration {
    // ApiInfo:给当前项目添加解释说明文字信息
    @Bean
    public ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("SpringBoot整合swaggerAPI文档示例")
                .description("采用restful风格接口")
                .version("1.0")
                .build();
    }

    // 生成接口文档配置:
    @Bean
    public Docket docket(ApiInfo apiInfo) {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage(
                        "com.csy.springbootpro1.controller"))
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

第三步 修改 application.yml 文件 注意层级

image.png

第四步在controller 具体的位置设置swagger

@RestController
@RequestMapping("/swagger")
@Api(value = "该类是演员相关模块的api接口")
public class SwaggerController {
    @Resource
    UserService userService;
    @GetMapping("/all")
    @ApiOperation(value = "查询所有用户" ,notes="得到所有的用户信息")
    public List<Actor> all(){
        return userService.all();
    }
   @RequestMapping("/find")
   @ApiOperation(value = "根据id查询用户" ,notes="得到所有的用户信息")
   // 1 请求方式  2 参数类型 3参数名称 4值的意义 5是否必须
   @ApiImplicitParam(paramType ="query",dataType = "int",name = "id",value = "用户id",required = true)
    public Actor findById(int id){
        return userService.findById(id);
    }
}

第五步 访问 拿我的举例 注意 test是设置的 http://localhost:8080/test/swagger-ui.html

注意 test是设置的 context-path 根据个人情况调整

image.png