【SpringBoot系列】通过OpenAPI构建SpringBoot RestAPI

举报
Freedom123 发表于 2024/04/19 15:12:07 2024/04/19
【摘要】 如何通过OpenAPI规范构建SpringBoot 微服务应用接口

/**

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
);

}

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。