Spring Boot 整合Swagger2

Spring Boot 整合Swagger2

依赖导入

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

编写配置类

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .apis(RequestHandlerSelectors.basePackage("com.xy.video.controller"))
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("毕设测试Swagger")
                .contact(new Contact("念月",
                        "https://www.cnblogs.com/zero-vic/",
                        "1623272666@qq.com"))
                .description("喜欢就去给她说吧,不要给自己留遗憾了!")
                .version("5.2.0")
                .termsOfServiceUrl("http://baidu.com")
                .license("LICENSE")
                .licenseUrl("链接地址")
                .build();
    }

}

Controller层

@RestController
@Api(tags = "用于用户管理的接口")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("user")
    @ApiOperation(value = "用户查询所有接口",notes = "分页查询用户信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功"),
            @ApiResponse(code = 500, message = "操作失败,根据msg查看具体的错误信息")
    })
    public Map<String,Object> findAllUser(){
        return userService.findAllUser();
    }
}

pojo

@ApiModel
public class User implements Serializable {
    /**
     * id(雪花算法生成)
     */
    @ApiModelProperty(value = "用户ID",name = "id",dataType = "long",required = true)
    private Long id;

地址:http://localhost:8080/swagger-ui.html

posted @ 2021-01-22 14:38  念月_xy  阅读(149)  评论(0编辑  收藏  举报