springboot集成Swagger2

Swagger2简介

       我们提供Restful接口的时候,API文档是尤为的重要,它承载着对接口的定义,描述等。它还是和API消费方沟通的重要工具。在实际情况中由于接口和文档存放的位置不同,我们很难及时的去维护文档。

1.随项目自动生成强大RESTful API文档,减少工作量
2.API文档与代码整合在一起,便于同步更新API说明
3.页面测试功能来调试每个RESTful API

项目搭建

    添加依赖:
    

<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>

创建Swagger2配置类

导入相应的包即可。将指定扫描包下的注解 改成 自己的包名称

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.sss"))// 指定扫描包下面的注解
                .paths(PathSelectors.any())
                .build();
    }
    // 创建api的基本信息
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("集成Swagger2构建RESTful APIs")
                .description("集成Swagger2构建RESTful APIs")
                .termsOfServiceUrl("https://www.baidu.com")
                .contact("itlike")
                .version("1.0.0")
                .build();
    }
}

新建测试类:

@Api(value="用户controller",tags={"用户操作接口"})
@RestController
public class userController {

    @Autowired
    private HeroService heroService;

        // 根据当前的id获取记录
        @ApiOperation(value="获取Hero信息",notes="注意问题点",httpMethod="GET")
        @ApiImplicitParams({@ApiImplicitParam(name="id",value="用户id",dataType="Long", paramType = "path")})
        @GetMapping("/test/{id}")
        @ResponseBody
        public Hero test(@PathVariable("id") Integer id, Model model) {

            Hero hero = heroService.getOneHero(id);
            return hero;
        }
    }

启动类,访问   http://localhost:8080/swagger-ui.html

点击获取信息展开详细信息:

查询结果:

到此,springboot集成swagger2完成。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值