SpringBoot结合Swagger开放API,SpringCloud Feign调用API

tech2023-02-04  82

从本文你可以学会使用SpringBoot结合Swagger开放API,同时使用SpringCloud Feign组件来调用API。本文如题分为两部分SpringBoot结合Swagger开放API,SpringCloud Feign调用API。项目源码:iponkan-openapi

SpringBoot结合Swagger开放API

项目模块构建(Maven多模块项目搭建)引入的依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> </dependency> 定义数据传输层对象 @ApiModel(description = "订单") public class OrderDto { /** * 单据uuid */ private String id; /** * 单号 */ private String number; /** * 来源 */ private String source; @ApiModelProperty("订单uuid") @NotNull public String getId() { return id; } public void setId(String id) { this.id = id; } @ApiModelProperty("单号") @NotNull public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } @ApiModelProperty("订单来源") public String getSource() { return source; } public void setSource(String source) { this.source = source; } } 定义开放接口 @Api("订单服务") @RequestMapping(value = "order", produces = "application/json;charset=utf-8") public interface OrderService { String DEFAULT_CONTEXT_ID = "iponkan-openapi.orderService"; @ApiOperation("保存订单,支持重复调用") @PostMapping("post") @ResponseBody String post(@Valid @RequestBody OrderDto order) throws Exception; @ApiOperation("获取订单,根据订单号") @GetMapping("get") @ResponseBody OrderDto get(@RequestParam String number) throws Exception; } 实现开放接口 @RestController(OrderService.DEFAULT_CONTEXT_ID) public class OrderServiceImpl implements OrderService { @Autowired private OrderProcessor processor; public String post(@Valid OrderDto order) throws Exception { return processor.process(order); } @Override public OrderDto get(String number) throws Exception { OrderDto dto = new OrderDto(); dto.setId(UUID.randomUUID().toString()); dto.setNumber("20200902000001"); dto.setSource("IPONKAN"); return dto; } } 增加Swagger的配置 引入依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> </dependency> 增加一个配置类 @Configuration @ComponentScan("cn.iponkan.openapi") public class SwaggerConfig { @Bean public Docket swaggerSpringMvcPlugin() { return new Docket(DocumentationType.SWAGGER_2) // .apiInfo(swaggerDemoApiInfo()) // .select() // .build(); } private ApiInfo swaggerDemoApiInfo() { // 构建联系实体,在UI界面会显示 Contact contact = new Contact("", "", ""); return new ApiInfoBuilder().contact(contact) // 文档标题 .title("OpenApi RESTful API文档") // 文档描述 .description("OpenApi RESTful API文档") // 文档版本 .version("1.0.0") // .build(); } } 启动类上加上注解 @EnableSwagger2 启动项目后查看结果

SpringCloud Feign调用API

引入Spring Cloud Feign依赖 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> 定义调用接口的客户端 @FeignClient public interface OpenApiClient { @RequestLine("GET get?number={number}") Order get(URI baseUri, @Param("number") String number); } 配置调用接口的客户端 @Configuration @Import({ FeignClientsConfiguration.class, HttpMessageConvertersAutoConfiguration.class}) public class ProviderConfig { @Bean public OpenApiClient openApiClient(Decoder decoder, Encoder encoder) { return Feign.builder() // .encoder(encoder) // .decoder(decoder) // .logLevel(Logger.Level.FULL) // .target(Target.EmptyTarget.create(OpenApiClient.class)); } } 使用客户端调用接口 @RestController @RequestMapping("feign/order") public class OrderController { @Autowired private OpenApiClient openApiClient; @RequestMapping("get") public Order get() throws Exception { return openApiClient.get(new URI("http://localhost:9999/openapi-web/service/order"), "123"); } } 调用结果,数据成功返回

总结

SpringCloud Feign:是HTTP请求调用的一个轻量级框架。可以使用Java接口注解方式调用HTTP请求。留下一个疑问?Feign的实现原理。
最新回复(0)