springcloud+postman(请求软件)+swagger2(接收参数文档生成器)
1.postman安装后,如下方式
1.1 controller
@RestController public class LoginController { //post请求的映射网址 @PostMapping("/user") //@RequestBody将json封装为对象 public AjaxResult loginUser(@RequestBody User user){ if("admin".equals(user.getUsername())&&"admin".equals(user.getPassword())){ return AjaxResult.myAjaxResult().setMessage("登录成功").setSuccess(true); } return AjaxResult.myAjaxResult().setMessage("登录失败").setSuccess(false); } }
1.2 zuul网关的配置
eureka: client: serviceUrl: defaultZone: http://localhost:1000/eureka/ instance: prefer-ip-address: true #使用ip注册到eureka服务列表 server: port: 2000 spring: application: name: zuul-server zuul: ignoredServices: '*' routes: plat: path: /plat/** #平台服务的请求路径 serviceId: plat-service #plat-service 这是上面controller的配置yml文件名,必须一致 prefix: /cc #请求前缀
1.3 postman软件访问
2.swagger
2.1 导maven包
<!--swagger--> <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.2 先在业务服务层处建立class
2.3 在服务层的swagger的class代码
@Configuration @EnableSwagger2 public class Swagger2 { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("cn.itsource.controller"))//controller的包名 .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("平台服务api") .description("平台服务接口文档说明") .contact(new Contact("solargen", "", " cc@itsource.cn")) .version("1.0") .build(); } }
2.4 在zuul网关层的swagger的2个class代码,记得导入上面的pom包
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("分布式购物系统") .description("购物系统接口文档说明") .contact(new Contact("solargen", "", "cc@itsoruce.cn")) .version("1.0") .build(); } }
@Component @Primary public class DocumentationConfig implements SwaggerResourcesProvider { @Override public List<SwaggerResource> get() { List resources = new ArrayList<>(); //product,user等都是网关中配置路径,而且如果有前缀,需要加上 resources.add(swaggerResource("商品系统", "/services/product/v2/api-docs", "2.0")); resources.add(swaggerResource("用户系统", "/services/user/v2/api-docs", "2.0")); resources.add(swaggerResource("基础系统", "/services/common/v2/api-docs", "2.0")); //上面的3个是虚拟的,需要自己去配路径业务 //location 路由的地址 cc -- cc/plat要和zuul里的路径一致 resources.add(swaggerResource("平台管理", "/cc/plat/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; } }
2.5 搜索swagger的网址
http://localhost:2000/swagger-ui.html
然后点击 平台管理 即可