SpringBoot2.2.4.RELEASE集成Swagger <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.0.8.RELEASE</version> </dependency> <dependency> <groupId>com.spring4all</groupId> <artifactId>swagger-spring-boot-starter</artifactId> <version>1.9.1.RELEASE</version> </dependency> #添加掃包 swagger.base-package=com.example.springbootswagger.controller /** * http://127.0.0.1:8080/swagger-ui.html */ @SpringBootApplication @EnableSwagger2Doc public class SpringbootSwaggerApplication { public static void main(String[] args) { SpringApplication.run(SpringbootSwaggerApplication.class, args); } } @RestController @Api("測試接口服務") public class TestController { @GetMapping("/one") @ApiOperation("測試get-單獨傳參") @ApiImplicitParam(name="name",value = "用戶名") public Object testOne(String name){ return name; } @PostMapping("/two") @ApiOperation("測試post-多參數") public Object testTwo(@RequestBody User user){ return user; } } public class User { /** * 多參數可用對象封裝 用@ApiModelProperty注解生成標准json文檔 */ @ApiModelProperty(name = "name",value = "用戶名") private String name; @ApiModelProperty(name = "address",value = "家庭住址") private String address; }
SpringCloud集成Swagger:
注意springcloud版本:各個版本之間差異巨大 <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> </parent> -------------------------------------------------------------------------------- <dependency> <groupId>com.spring4all</groupId> <artifactId>swagger-spring-boot-starter</artifactId> <version>1.9.1.RELEASE</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>26.0-jre</version> </dependency> -------------------------------------------------------------------------------- 各個微服務集成方式同SpringBoot一致 區別主要是Zuul的集成: -------------------------------------------------------------------------------- package com.example.zuulservice.config; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.cloud.netflix.zuul.filters.ZuulProperties; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; import springfox.documentation.swagger.web.SwaggerResource; import springfox.documentation.swagger.web.SwaggerResourcesProvider; import java.util.ArrayList; import java.util.List; /** * zuul 集成 多個服務 swagger */ @Component @Primary class DocumentationConfig implements SwaggerResourcesProvider { // zuul配置能夠使用config實現實時更新 @RefreshScope @ConfigurationProperties("zuul") public ZuulProperties zuulProperties() { return new ZuulProperties(); } @Override public List<SwaggerResource> get() { List resources = new ArrayList<>(); // aapi-member-service name可以自定義 location serviceName 根據自己情況寫 后邊拼接寫死 resources.add(swaggerResource("api-member-service", "/api-member/v2/api-docs", "2.0")); resources.add(swaggerResource("api-order-service", "/api-order/v2/api-docs", "2.0")); return resources; } private SwaggerResource swaggerResource(String name, String location, String version) { SwaggerResource swaggerResource = new SwaggerResource(); swaggerResource.setName(name); swaggerResource.setLocation(location); swaggerResource.setSwaggerVersion(version); return swaggerResource; } } Zull也開啟Swagger: @SpringBootApplication @EnableEurekaClient @EnableZuulProxy @EnableSwagger2Doc public class ZuulServiceApplication { /** * 搭建zuul網關服務 * 根據網關調用接口 http://localhost:8765/swagger-ui.html * http://127.0.0.1:8765/api-order/order/getUsersByFegin * @param args */ public static void main(String[] args) { SpringApplication.run(ZuulServiceApplication.class, args); } } 即可完成Zuul與個服務之間的集成 訪問地址:http://localhost:8765/swagger-ui.html
注意fegin的傳參:
1.interface:
@PostMapping("/user/insertUser")
public JSONObject insertUser(@RequestBody Map<String, String> map);
2.controller:
@PostMapping("/insertUser")
@ApiOperation("測試分布式事務框架lcn")
public int insertUser(@RequestBody User user){
System.out.println(user.getName());
String name =user.getName();
userService.insert(user);
return 1;
}