- 从本文你可以学会使用SpringBoot结合Swagger开放API,同时使用SpringCloud Feign组件来调用API。
- 本文如题分为两部分SpringBoot结合Swagger开放API,SpringCloud Feign调用API。
- 项目源码:iponkan-openapi
SpringBoot结合Swagger开放API
- 项目模块构建(Maven多模块项目搭建)
- 引入的依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>io.swaggergroupId>
<artifactId>swagger-annotationsartifactId>
dependency>
- 定义数据传输层对象
@ApiModel(description = "订单")
public class OrderDto {
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.springfoxgroupId>
<artifactId>springfox-swagger2artifactId>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger-uiartifactId>
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() {
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.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
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的实现原理。