温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在SpringBoot中使用Swagger2

发布时间:2021-04-13 15:32:22 来源:亿速云 阅读:150 作者:Leah 栏目:开发技术

如何在SpringBoot中使用Swagger2?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

引入依赖

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

注意:jdk1.8以上才能运行swagger2

编写配置类配置Swagger

@Configuration
@EnableSwagger2
public class SwaggerConfig{
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("org.example.yourproject"))//这里填写项目package
                .paths(PathSelectors.any())
                .build();
    }//springfox为我们提供了一个Docket(摘要的意思)类,我们需要把它做成一个Bean注入到spring中, 显然,我们需要一个配置文件,并通过一种方式(显然它会是一个注解)告诉程序,这是一个Swagger配置文件。
  
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2构建RESTful API")
                .description("rest api 文档构建利器")
                .termsOfServiceUrl("https://www.cnblogs.com/yrxing/")
                .contact("xing")
                .version("1.0")
                .build();
    }
  
}//springfox允许我们将信息组合成一个ApiInfo的类,作为构造参数传给Docket

如何在SpringBoot中使用Swagger2

如何在SpringBoot中使用Swagger2

访问:http://localhost:{your_server_port}/swagger-ui.html

Swagger2常用注解使用

如何在SpringBoot中使用Swagger2

@Api()、@ApiOperation()

@RestController
@RequestMapping(value = "/user", produces = APPLICATION_JSON_VALUE) //配置返回值 application/json
@Api(tags = "用户管理")
public class HelloController {
  
    ArrayList<User> users = new ArrayList<>();
  
    @ApiOperation(value = "获取用户列表", notes = "获取所有用户信息")
    @RequestMapping(value = {""}, method = RequestMethod.GET)
    public List<User> hello() {
        users.add(new User("逻辑", "luoji"));
        users.add(new User("叶文杰", "yewenjie"));
        return users;
    }
}

@ApiModel()、@ApiModelProperty()

@ApiModel(description = "用户",value = "用户")
public class User {
  
    private String id;
  
  @ApiModelProperty(value = "用户名")//value属性指明了该字段的含义(描述 Description)
    private String username;
  
   @ApiModelProperty(hidden = true)//此注解可以作用在字段或者方法上,只要 hidden 属性为 true ,该字段或者方法就不会被生成api文档.
    private String password;
  
    private String email;
  
    private Integer age;
  
    private Boolean enabled;
  
}

@ApiParam()

@ApiOperation(value = "获取用户详细信息", notes = "根据url的id来获取用户详细信息")
 @RequestMapping(value = "getUser/{id}", method = RequestMethod.GET)
 
 public User getUser(@ApiParam(naeme = "id",value = "用户id", required = true)  @PathVariable(value = "id") String id) {
     return new User(id, "itguang", "123456");
 }//@ApiParam这个注解,需要注意的是,这个注解方法的参数前面,不能直接用在方法上面.

@ApiImplicitParams()、@ApiImplicitparam()

···
  @Api("测试用例1")
  @Controller
  public class swaggerTestUse(){
      @ApiOperation(value = "apiOperationSwaggerTest", notes = "apiOperationSwagger测试")
      @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id入参", required = true, dataType = "Integer", paramType = "query"),
                        @ApiImplicitParam(name = "brand", value = "brand", required = true, dataType = "BRAND", paramType = "body")
    })
      public void apiOperationSwaggerTest(Integer id, Brand band){
      }
  }

关于如何在SpringBoot中使用Swagger2问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI