SpringBoot+Swagger整合API
SpringBoot+Swagger整合API
Swagger:整合规范的api,有界面的操作,测试
1.在pom.xml加入swagger依赖
<!--整合Swagger2配置类--> <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>
2.在application.yml开启swagger
#引入swagger swagger: enable: true
3.配置SwaggerConfiguration.java
注意:@Value("${swagger2.basePackage:com.spingcloud.serviceconsumer.controller}") 添加扫描的api入口
@Configuration @EnableSwagger2 public class SwaggerConfiguration { @Value("${swagger2.basePackage:com.spingcloud.serviceconsumer.controller}") private String swagger2BasePackage; @Value("${swagger2.title:系统API文档}") private String swagger2Title; @Value("${swagger2.api.version:2.0}") private String apiVersion; @Bean public Docket createRestApi() { //添加query参数start ParameterBuilder tokenPar = new ParameterBuilder(); List<Parameter> pars = new ArrayList<Parameter>(); // tokenPar.name("token").description("令牌").modelRef(new ModelRef("string")).parameterType("query").required(true).build(); pars.add(tokenPar.build()); //添加query参数end return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select().apis(RequestHandlerSelectors.basePackage(swagger2BasePackage)) .paths(PathSelectors.any()) .build() // .globalOperationParameters(pars) //添加验证 // .securitySchemes(securitySchemes()) // .securityContexts(securityContexts()) //添加验证 ; } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title(swagger2Title) .version(apiVersion) .build(); } private List<ApiKey> securitySchemes() { List<ApiKey> apiKeyList= new ArrayList(); apiKeyList.add(new ApiKey("token", "令牌", "header")); return apiKeyList; } private List<SecurityContext> securityContexts() { List<SecurityContext> securityContexts=new ArrayList<>(); securityContexts.add( SecurityContext.builder() .securityReferences(defaultAuth()) .forPaths(PathSelectors.regex("^(?!auth).*$")) .build()); return securityContexts; } List<SecurityReference> defaultAuth() { AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; authorizationScopes[0] = authorizationScope; List<SecurityReference> securityReferences=new ArrayList<>(); securityReferences.add(new SecurityReference("Authorization", authorizationScopes)); return securityReferences; } }
4.在Controller类上添加:
@RestController //@RequestMapping("/api/v1/order") @RequestMapping(value = "api") @Api("OrderFeginController相关的api") public class OrderFeginController { @Autowired private ProductOrderFeginService productOrderFeginService; //限流:对外提供一个服务接口,允许最大并发数为10 // private final Semaphore permit = new Semaphore(4, true); //当调用微服务出现异常会降级到saveOrderFail方法中 @HystrixCommand(fallbackMethod = "saveOrderFail") @RequestMapping(value = "/saveorder",method = RequestMethod.GET) @ApiOperation(value = "根据id查询商品信息", notes = "查询数据库中某个的商品信息") @ApiImplicitParams({ @ApiImplicitParam(name = "user_id",paramType = "query", value = "用户ID", required = true), @ApiImplicitParam(name = "product_id",value = "商品ID",required = true,paramType = "query",dataType = "string") }) public Object saveorder(@RequestParam("user_id")int userId, @RequestParam("product_id") int productId) throws InterruptedException { return productOrderFeginService.saveorder(userId, productId); } //注意,方法签名一定要要和api方法一致 private Object saveOrderFail(int userId, int productId){ System.out.println("controller中的降级方法"); Map<String, Object> msg = new HashMap<>(); msg.put("code", -1); msg.put("msg", "抢购人数太多,您被挤出来了,稍等重试"); return msg; } }