• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

SpringBoot整合Swagger2的示例

springboot 搞代码 4年前 (2022-01-09) 15次浏览 已收录 0个评论

一、导入maven包 

<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 createRestApi() {
    return new Docket(DocumentationType<em style="color:transparent">本文来源[email protected]搞@^&代*@码)网9</em>.SWAGGER_2)
        .pathMapping("/")
        .select()
        .apis(RequestHandlerSelectors.basePackage("com.nvn.controller"))
        .paths(PathSelectors.any())
        .build().apiInfo(new ApiInfoBuilder()
            .title("SpringBoot整合Swagger")
            .description("SpringBoot整合Swagger,详细信息......")
            .version("1.0")
            .build());
  }
}

三、添加注解

@RestController
@Api(tags = "用户管理相关接口")
@RequestMapping("/user")
public class UserController {

  @PostMapping("/")
  @ApiOperation("添加用户的接口")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "username", value = "用户名", defaultValue = "李四"),
      @ApiImplicitParam(name = "address", value = "用户地址", defaultValue = "深圳", required = true)
  }
  )
  public RespBean addUser(String username, @RequestParam(required = true) String address) {
    return new RespBean();
  }

  @GetMapping("/")
  @ApiOperation("根据id查询用户的接口")
  @ApiImplicitParam(name = "id", value = "用户id", defaultValue = "99", required = true)
  public User getUserById(@PathVariable Integer id) {
    User user = new User();
    user.setId(id);
    return user;
  }
  @PutMapping("/{id}")
  @ApiOperation("根据id更新用户的接口")
  public User updateUserById(@RequestBody User user) {
    return user;
  }
}

四、注解说明

  • @Api注解可以用来标记当前Controller的功能。
  • @ApiOperation注解用来标记一个方法的作用。
  • @ApiImplicitParam注解用来描述一个参数,可以配置参数的中文含义,也可以给参数设置默认值,这样在接口测试的时候可以避免手动输入。
  • 如果有多个参数,则需要使用多个@ApiImplicitParam注解来描述,多个@ApiImplicitParam注解需要放在一个@ApiImplicitParams注解中。
  • @ApiImplicitParam注解中虽然可以指定参数是必填的,但是却不能代替@RequestParam(required = true),前者的必填只是在Swagger2框架内必填,抛弃了Swagger2,这个限制就没用了,所以假如开发者需要指定一个参数必填,@RequestParam(required = true)注解还是不能省略。

五、如果参数是一个对象,对于参数的描述可以放在实体类中。

@ApiModel
public class User {
  @ApiModelProperty(value = "用户id")
  private Integer id;
  @ApiModelProperty(value = "用户名")
  private String username;
  @ApiModelProperty(value = "用户地址")
  private String address;
  //getter/setter
}

六、效果

附:如果我们的Spring Boot项目中集成了Spring Security,那么如果不做额外配置,Swagger2文档可能会被拦截,此时只需要在Spring Security的配置类中重写configure方法,添加如下过滤即可:

@Override
public void configure(WebSecurity web) throws Exception {
  web.ignoring()
      .antMatchers("/swagger-ui.html")
      .antMatchers("/v2/**")
      .antMatchers("/swagger-resources/**");
}

以上就是SpringBoot整合Swagger2的示例的详细内容,更多关于SpringBoot整合Swagger2的资料请关注搞代码其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:SpringBoot整合Swagger2的示例

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址