SpringBoot集成Swagger2

  技术交流群:233513714

 

 

一、创建maven工程引入依赖

复制代码
<dependencies>
        <!----------swagge依赖----------->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>

        <!----------jackson依赖----------->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.7</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jsckson-annotions</artifactId>
            <version>2.9.7</version> 
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.9.7</version>
        </dependency>
</dependencies>
复制代码

 

二、Swagger2配置

复制代码
@Configuration  //SpringBoot配置注解
@EnableSwagger2 //启用Swagger2功能注解
public class SwaggerConfig {
    @Bean
    public Docket createRestfulApi() {//api文档实例
        return new Docket(DocumentationType.SWAGGER_2)//文档类型:DocumentationType.SWAGGER_2
                .apiInfo(apiInfo())//api信息
                .select()//构建api选择器
                .apis(RequestHandlerSelectors.basePackage("com.datad.dream.controller"))//api选择器选择api的包
                .paths(PathSelectors.any())//api选择器选择包路径下任何api显示在文档中
                .build();//创建文档
    }

    private ApiInfo apiInfo() {//接口的相关信息
        return new ApiInfoBuilder()
                .title("SpringBoot使用Swagger2构建RESTful接口")
                .description("Swagger接口文档路径为:http://localhost:8080/swagger-ui.html")
                .termsOfServiceUrl("http://localhost:8080/swagger-ui.html") 
.contact(
"sunf")
.version(
"1.0")
.license(
"http://springfox.github.io/springfox/docs/current/")
.licenseUrl(
"http://springfox.github.io/springfox/docs/current/")
.build();
}
}
复制代码

 

三、Swagger2注解使@RestController //接口注解@Api(value="用户接口",tags={"catTest"}) //接口简要标注,对中文的支持不太好

复制代码
@RequestMapping(value = "/swagger")  //接口基本路径
public class AnimalController {
    //接口需要的参数,可以有多个,这里只写了一个,它的paramType还有path、query、body、form几种,
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "Token", value = "token", dataType = "String", required = true,defaultValue = "123")})
    //接口功能描述
    @ApiOperation(value = "获取一只猫")
    //接口响应信息,这里定义了一个401,当出现401,接口返回的是自定的错误AnimalError的实例。当然可以定义多个。
    @ApiResponses(value = { @ApiResponse(code = 401, message = "请求未通过认证.", response = AnimalError.class) })
    @RequestMapping(value="/onecat", method = RequestMethod.GET)
    public Cat oneCat(){
      Cat cat = new Cat();
     cat.setAge("1");
     cat.setBreed("波斯猫");
     cat.setName("Tom");
        return cat;
    }
}

@Getter
@Setter
@ToString
@ApiModel(description = "猫咪实体类")
public class Cat{
  @ApiModeProperty(value = "年龄")
  private String age;
   @ApiModeProperty(value = "品种")
  private String breed;
   @ApiModeProperty(value = "名字")
  private String name;
}
复制代码

 

四、查看接口信息

 

1、在浏览器输入网址http://localhost:8080/swagger-ui.html

2、展开接口列表

 

 3、进入接口

 

 4、调用成功后返回

 

posted @   大浪不惊涛  阅读(293)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
点击右上角即可分享
微信分享提示