Spring boot添加Swagger配置

本文详细介绍如何在SpringBoot项目中集成Swagger2,包括配置SwaggerConfig类,使用Docket、UiConfiguration等创建REST API文档,以及如何在控制器类中使用@Api、@ApiOperation等注解来描述API接口。


导入Swagger

Gradle

  dependencies{

  compile('io.springfox:springfox-swagger2:2.8.0')
  compile('io.springfox:springfox-swagger-ui:2.8.0')
  compile 'io.swagger:swagger-jersey2-jaxrs:1.5.8'
  compile('com.mangofactory:swagger-springmvc:1.0.2')
  compile('com.mangofactory:swagger-models:1.0.2')
  compile('com.wordnik:swagger-annotations:1.3.11')*
  }

@Configuration
@EnableSwagger2
public class SwaggerConfig {
  @Bean
  public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
      .select()

      .protocols(new HashSer<String>(Lists.newArrayList("http")))
      .apis(RequestHandlerSelectors.basePackage("com.arthur.demo.controller"))----->对应控制层的API接口层包路径
      .paths(PathSelectors.any())
      .pathMapping("/")
      .build()
      .apiInfo(apiInfo());
  }
  @Bean
public UiConfiguration uiConfig(){
  return UiConfiguration.DEFAULT;
  }
  private ApiInfo apiInfo(){
    return new ApiInfoBuilder()
      .title("API Title")
      .description("API接口说明文档")
      .termsOfServiceUrl(" API terms of service")
      .version("1.0")
      .build();
  }
}

 

Swagger注解

 

 

举例说明

@RestController
@Api(value="获取邮箱地址",description = "getEmail(外层显示)",produces=MediaType.APPLICATION_FROM_URLENCODED_VALUE)
public class LoginController {

@ApiOperation("用户登陆", notes = "通过该接口获取激活邮箱信息", httpMethod = "POST", produces = MediaType.APPLICATION_FROM_URLENCODED_VALUE)
@PostMapping(value = "/login")
public ResponseEntity Login(@ApiParam(value = "环境", required = true) @RequestParam(value = "baseurl", required = true) String baseurl
, @ApiParam(value = "账号", required = true) @RequestParam(value = "userName", required = true) String userName) {
return baseurl,userName;
}
}

 

转载于:https://www.cnblogs.com/Byronlyu/p/9804205.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值