标签:前后端分离 print api文档 info -- backend require validator pac
1.@Api:标记类 例:@Api(value="测试类",tags="测试类")
2.@ApiOperation:标记方法 例:@ApiOperation(value="测试方法",tags="测试方法")
3.@ApiImplicitParam:用来描述单个参数 例:@ApiImplicitParam(name = "enddate", value = "测试参数3", dataType = "String")
4.@ApiImplicitParams:用来描述多个参数多个用,分割 例:
@ApiImplicitParams({ @ApiImplicitParam(name = "projectinfoid", value = "测试参数", dataType = "String"),
@ApiImplicitParam(name = "startdate", value = "测试参数2", dataType = "String"),
@ApiImplicitParam(name = "enddate", value = "测试参数3", dataType = "String")
})
<!-- swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.2.2</version> </dependency>
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //指定的是要扫描的包,现在是只要在这个包下的类都会生成接口文档 .apis(RequestHandlerSelectors.basePackage("com.hhtl.backend.controller"))
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))//这个是设置只有加上Api注解的类才会生成接口文档
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("EasyWeb API文档") .description("前后端分离开发平台") .termsOfServiceUrl("") .contact("https://easyweb.vip") .version("1.0") .build(); } }
@Api(value = "测试类") @RestController @RequestMapping(value = "test") public class TestController { @ApiOperation(value = "测试方法") @ApiImplicitParams({ @ApiImplicitParam(name = "projectinfoid", value = "测试参数", dataType = "String"), @ApiImplicitParam(name = "startdate", value = "测试参数2", dataType = "String"), @ApiImplicitParam(name = "enddate", value = "测试参数3", dataType = "String") }) @GetMapping(value = "queryTest") public Result queryTest(@RequestParam String projectinfoid, String startdate, String enddate){ try { return Result.ok(); } catch (Exception e) { e.printStackTrace(); } return Result.error(); }
<dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.9.3</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-bean-validators</artifactId> <version>2.9.2</version> </dependency>
swagger: basic: enable: true ## Basic认证用户名 username: 123 ## Basic认证密码 password: 123
@Configuration @EnableSwagger2 @EnableSwaggerBootstrapUI public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.hhtl.backend.controller")) //加了ApiOperation注解的类,才生成接口文档 .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("EasyWeb API文档") .description("前后端分离开发平台") .termsOfServiceUrl("") .contact("https://easyweb.vip") .version("1.0") .build(); } }
package com.hhtl.backend.config; import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI; import io.swagger.annotations.ApiOperation; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.ParameterBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.schema.ModelRef; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Parameter; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.ArrayList; import java.util.List; @Configuration @EnableSwagger2 @EnableSwaggerBootstrapUI public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.hhtl.backend.controller")) //加了ApiOperation注解的类,才生成接口文档 .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .paths(PathSelectors.any()) .build() .globalOperationParameters(setHeaderToken());//请求头所需参数 } private List<Parameter> setHeaderToken() { ParameterBuilder tokenPar = new ParameterBuilder(); List<Parameter> pars = new ArrayList<>(); tokenPar.name("token").description("token").modelRef(new ModelRef("string")).parameterType("header").required(false).build(); pars.add(tokenPar.build()); return pars; } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("EasyWeb API文档") .description("前后端分离开发平台") .termsOfServiceUrl("") .contact("https://easyweb.vip") .version("1.0") .build(); } }
标签:前后端分离 print api文档 info -- backend require validator pac
原文地址:https://www.cnblogs.com/HQ0422/p/13197809.html