【Spring Boot系列】通过OpenAPI规范构建微服务服务接口

简介: 【4月更文挑战第5天】通过OpenAPI接口构建Spring Boot服务RestAPI接口

/**

import com.sab.inventory.dto.Error;
import com.sab.inventory.dto.Product;
import java.util.UUID;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.*;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Generated;

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2022-04-23T22:00:49.464591+02:00[Europe/Berlin]")
@Validated
@Tag(name = "products", description = "the products API")
public interface ProductsApi {

/**
 * POST /products
 * Add A Product to inventory
 *
 * @param product  (optional)
 * @return All products are returned (status code 201)
 *         or No Product returned (status code 400)
 */
@Operation(
    operationId = "addProduct",
    responses = {
        @ApiResponse(responseCode = "201", description = "All products are returned", content = @Content(mediaType = "application/json", schema = @Schema(implementation =  Product.class))),
        @ApiResponse(responseCode = "400", description = "No Product returned", content = @Content(mediaType = "application/json", schema = @Schema(implementation =  Error.class)))
    }
)
@RequestMapping(
    method = RequestMethod.POST,
    value = "/products",
    produces = { "application/json" },
    consumes = { "application/json" }
)
ResponseEntity<Product> addProduct(
    @Parameter(name = "Product", description = "", schema = @Schema(description = "")) @Valid @RequestBody(required = false) Product product
);


/**
 * GET /products
 * Get All Products
 *
 * @return All products are returned (status code 200)
 *         or No Product returned (status code 404)
 */
@Operation(
    operationId = "getAllProducts",
    responses = {
        @ApiResponse(responseCode = "200", description = "All products are returned", content = @Content(mediaType = "application/json", schema = @Schema(implementation =  Product.class))),
        @ApiResponse(responseCode = "404", description = "No Product returned", content = @Content(mediaType = "application/json", schema = @Schema(implementation =  Error.class)))
    }
)
@RequestMapping(
    method = RequestMethod.GET,
    value = "/products",
    produces = { "application/json" }
)
ResponseEntity<List<Product>> getAllProducts(

);


/**
 * GET /products/{id}
 * Get A Product By ID
 *
 * @param id  (required)
 * @return All products are returned (status code 200)
 *         or No Product returned (status code 404)
 */
@Operation(
    operationId = "getProductById",
    responses = {
        @ApiResponse(responseCode = "200", description = "All products are returned", content = @Content(mediaType = "application/json", schema = @Schema(implementation =  Product.class))),
        @ApiResponse(responseCode = "404", description = "No Product returned", content = @Content(mediaType = "application/json", schema = @Schema(implementation =  Error.class)))
    }
)
@RequestMapping(
    method = RequestMethod.GET,
    value = "/products/{id}",
    produces = { "application/json" }
)
ResponseEntity<Product> getProductById(
    @Parameter(name = "id", description = "", required = true, schema = @Schema(description = "")) @PathVariable("id") UUID id
);


/**
 * PUT /products/{id}
 * Update A Product
 *
 * @param id  (required)
 * @param product  (optional)
 * @return Created product is  returned (status code 200)
 *         or Error (status code 400)
 *         or Product Does not Exist (status code 404)
 */
@Operation(
    operationId = "updateProduct",
    responses = {
        @ApiResponse(responseCode = "200", description = "Created product is  returned", content = @Content(mediaType = "application/json", schema = @Schema(implementation =  Product.class))),
        @ApiResponse(responseCode = "400", description = "Error", content = @Content(mediaType = "application/json", schema = @Schema(implementation =  Error.class))),
        @ApiResponse(responseCode = "404", description = "Product Does not Exist", content = @Content(mediaType = "application/json", schema = @Schema(implementation =  Error.class)))
    }
)
@RequestMapping(
    method = RequestMethod.PUT,
    value = "/products/{id}",
    produces = { "application/json" },
    consumes = { "application/json" }
)
ResponseEntity<Product> updateProduct(
    @Parameter(name = "id", description = "", required = true, schema = @Schema(description = "")) @PathVariable("id") UUID id,
    @Parameter(name = "Product", description = "", schema = @Schema(description = "")) @Valid @RequestBody(required = false) Product product
);

}

相关文章
|
6天前
|
Dubbo Java 应用服务中间件
微服务学习 | Springboot整合Dubbo+Nacos实现RPC调用
微服务学习 | Springboot整合Dubbo+Nacos实现RPC调用
|
2天前
|
Java Docker 微服务
|
3天前
|
消息中间件 Java RocketMQ
Spring Cloud RocketMQ:构建可靠消息驱动的微服务架构
【4月更文挑战第28天】消息队列在微服务架构中扮演着至关重要的角色,能够实现服务之间的解耦、异步通信以及数据分发。Spring Cloud RocketMQ作为Apache RocketMQ的Spring Cloud集成,为微服务架构提供了可靠的消息传输机制。
15 1
|
3天前
|
Dubbo Java 应用服务中间件
Spring Cloud Dubbo: 微服务通信的高效解决方案
【4月更文挑战第28天】在微服务架构的发展中,服务间的高效通信至关重要。Spring Cloud Dubbo 提供了一种基于 RPC 的通信方式,使得服务间的调用就像本地方法调用一样简单。本篇博客将探讨 Spring Cloud Dubbo 的核心概念,并通过具体实例展示其在项目中的实战应用。
13 2
|
5天前
|
XML Java 数据格式
手写spring第八章-定义标记类型Aware接口,实现感知容器对象
手写spring第八章-定义标记类型Aware接口,实现感知容器对象
4 0
|
6天前
|
SQL 安全 Java
微服务之Springboot整合Oauth2.0 + JWT
微服务之Springboot整合Oauth2.0 + JWT
11 1
|
6天前
|
Java 数据安全/隐私保护 Sentinel
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
|
2月前
|
域名解析 弹性计算 tengine
阿里云DNS常见问题之阿里云OpenAPI判断域名的dns服务器是否在阿里云失败如何解决
阿里云DNS(Domain Name System)服务是一个高可用和可扩展的云端DNS服务,用于将域名转换为IP地址,从而让用户能够通过域名访问云端资源。以下是一些关于阿里云DNS服务的常见问题合集:
|
2月前
|
JavaScript API 开发工具
阿里云OpenAPI AssignJobs返回404错误可能有以下几个原因:
【2月更文挑战第20天】阿里云OpenAPI AssignJobs返回404错误可能有以下几个原因:
56 1
|
3月前
|
云安全 安全 API
阿里云——OpenAPI使用——短信服务
阿里云——OpenAPI使用——短信服务
161 0