springboot + swagger 整合

话不多说 实现 丝袜哥的接口分为三步(把大象装进冰箱总共分几步?):
1.添加依赖
2.配置类
3.controller 加配置

1.maven依赖

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

2.swagger 配置类

@Configuration
@EnableSwagger2
public class Swagger2 {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.didispace.web"))//扫描包
            //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) //被注解的方法
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiI标题")
                .description("---一个说明-----")
                .termsOfServiceUrl("http://blog.didispace.com/")
                .contact("程序猿")
                .version("1.0")
                .build();
    }

}

3.在controller 中配置

@Api(tags = "测试接口描述")            //描述
@RestController
@RequestMapping("/test")
public class TestController {
    @Resource
    private UserService userService;
    
    @GetMapping("/getUser")
    @ApiOperation(value = "获取用户信息", notes = "通过用户ID获取用户信息")
    public String getUser(@RequestParam String name, @RequestParam int age) {

        return name;
    }
}

然后通过 http://localhost:8001/swagger-ui.html#/ 就可以访问了

在这里插入图片描述

简单的搭建成功了 其他的参数 继续百度就行了

猜你喜欢

转载自blog.csdn.net/hgdzw/article/details/100561266