Spring boot添加Swagger配置


导入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;
}
}

 

posted @ 2018-10-17 15:01  惑丶  阅读(248)  评论(0编辑  收藏  举报