【Java】@ApiOperation vs @ApiResponse in Swagger

简介: 【Java】@ApiOperation vs @ApiResponse in Swagger

原文

www.baeldung.com/swagger-api…

引言

本文内容讨论的是 @ApiOperation@ApiResponse 注解的优劣。

介绍Swagger

一个RestFul最重要的是具备“自描述能力”,所谓的自描述能力是能在返回结果的同时,告知客户端调用下 一步的行为,Swagger在一定程度上封装和规范了这些操作。

什么是Swagger?

Swagger represents a set of open-source tools built around OpenAPI Specification. It can help us design, build, document, and consume REST APIs.

Swagger代表了一套围绕OpenAPI规范建立的开源工具。它可以帮助我们设计、构建、记录REST APIs接口。其中最为常用的注解便是 @ApiOperation 和 @ApiResponse。


@RestController
@RequestMapping("/customers")
class CustomerController {
   private final CustomerService customerService;
   public CustomerController(CustomerService customerService) {
       this.customerService = customerService;
   }
   @GetMapping("/{id}")
   public ResponseEntity<CustomerResponse> getCustomer(@PathVariable("id") Long id) {
       return ResponseEntity.ok(customerService.getById(id));
   }
}

下面分别介绍这两个注解的使用,最后是进行对比。

@ApiOperation

关键点:描述单独的一个操作和行为。通常用于单一的路径或者http请求方法。


@ApiOperation(value = "Gets customer by ID", 
        response = CustomerResponse.class, 
        notes = "Customer must exist")
@GetMapping("/{id}")
public ResponseEntity<CustomerResponse> getCustomer(@PathVariable("id") Long id) {
    return ResponseEntity.ok(customerService.getById(id));
}

下面介绍@ApiOperation的一些属性:

value

value属性主要是描述接口的行为,注意参数大小不能超过120个字符。从长度限制来看要尽可能的简化注解的描述。


@ApiOperation(value = "Gets customer by ID")

notes

个人认为把这个注解当成comment更为合适,notes和value的区别是可以填写的长度限制为text,意思是更为详细的叙述接口功能。


@ApiOperation(value = "Gets customer by ID", notes = "Customer must exist")

response

@ApiOperation注解中定义的响应属性应该包含一般的响应类型。大部分情况下我们会把返回结果用统一的对象包装:


class CustomerResponse {
   private Long id;
   private String firstName;
   private String lastName;
   // getters and setters
}

更为常见的是使用类似AjaxDto这样的对象封装响应Code,响应Msg和响应数据Data。

在使用的过程中设置Class类,在Swagger文档中将会对应生成相关的对象以及


@ApiOperation(value = "Gets customer by ID",
        response = CustomerResponse.class,
        notes = "Customer must exist")
@GetMapping("/{id}")
public ResponseEntity<CustomerResponse> getCustomer(@PathVariable("id") Long id) {
    return ResponseEntity.ok(customerService.getById(id));
}

code

也就是响应code码,对应 Http Status

@ApiResponse

@ApiResponse 注解主要用于描述接口的返回状态码以及对应的返回信息。虽然@ApiOperation注解描述了操作和一般的返回类型,但@ApiResponse注解描述了其余可能的状态码。

@ApiResponse注解的特点是方法注解优先于类注解。我们应该在@ApiResponses注解中使用@ApiResponse注解,无论我们有一个还是多个响应。

下面是正确的@ApiResponse 用法案例:


@ApiResponses(value = {  
        @ApiResponse(code = 400, message = "Invalid ID supplied"),  
        @ApiResponse(code = 404, message = "Customer not found")})  
@GetMapping("/{id}")  
public ResponseEntity<CustomerResponse> getCustomer(@PathVariable("id") Long id) {  
    return ResponseEntity.ok(customerService.getById(id));  
}

对于特殊的状态码进行描述:


@ApiOperation(value = "Gets customer by ID", notes = "Customer must exist")  
@ApiResponses(value = {  
        @ApiResponse(code = 200, message = "OK", response = CustomerResponse.class),  
        @ApiResponse(code = 400, message = "Invalid ID supplied"),  
        @ApiResponse(code = 404, message = "Customer not found"),  
        @ApiResponse(code = 500, message = "Internal server error", response = ErrorResponse.class)})  
@GetMapping("/{id}")  
public ResponseEntity<CustomerResponse> getCustomer(@PathVariable("id") Long id) {  
    return ResponseEntity.ok(customerService.getById(id));  
}

code 和 message

与@ApiOperation注解中的code属性一样,它应该包含响应的HTTP状态代码。值得一提的是,我们不能用相同的代码属性定义一个以上的@ApiResponse。


@ApiResponse(code = 400, message = "Invalid ID supplied")

response

response可以为特殊的状态码指定对象:


class ErrorResponse {
    private String error;
    private String message;
    // getters and setters
}

其次,让我们为内部服务器错误添加一个新的@ApiResponse。


@ApiResponses(value = {
        @ApiResponse(code = 400, message = "Invalid ID supplied"),
        @ApiResponse(code = 404, message = "Customer not found"),
        @ApiResponse(code = 500, message = "Internal server error", response = ErrorResponse.class)})
@GetMapping("/{id}")
public ResponseEntity<CustomerResponse> getCustomer(@PathVariable("id") Long id) {
    return ResponseEntity.ok(customerService.getById(id));
}

@ApiOperation 和 @ApiResponse 区别

@ApiOperation @ApiResponse
通用用于描述一个操作 用于描述操作的可能的响应
通常用于成功的请求 可以用于成功或者失败的各种请求
只能用在方法级别 可以用于类或者方法级别
可以直接使用 只能在@ApiResponses注解中使用。
默认code为200 没有默认值


相关文章
|
2月前
|
存储 安全 Java
ArrayList vs. LinkedList: Java集合框架的比较与应用
ArrayList vs. LinkedList: Java集合框架的比较与应用
|
8月前
|
Java 程序员 Apache
编程语言比拼之Java VS C++
Java和C++都是非常受欢迎的编程语言,各有各的优势和适用场景。以下是对它们的简要比较:
|
18天前
|
Java Android开发 C++
Kotlin vs Java:选择最佳语言进行安卓开发
【4月更文挑战第13天】Java曾是安卓开发的主流语言,但Kotlin的崛起改变了这一局面。Google在2017年支持Kotlin,引发两者优劣讨论。Java以其成熟稳定、强大生态和跨平台能力占优,但代码冗长、开发效率低和语言特性过时是短板。Kotlin则以简洁语法、空安全设计和高度兼容Java脱颖而出,但社区和生态系统仍在发展中,可能存在学习曲线和性能问题。选择语言应考虑项目需求、团队熟悉度、维护性、性能和生态系统。无论选择哪种,理解其差异并适应新技术至关重要。
|
5月前
|
Java 程序员 C++
C++ vs Python vs Java
C++ vs Python vs Java
32 0
|
7月前
|
开发框架 前端开发 Java
Struts vs. Struts 2:Java Web 开发框架的升级之路与竞争力分析
Struts vs. Struts 2:Java Web 开发框架的升级之路与竞争力分析
48 0
|
8月前
|
安全 Java 编译器
Kotlin 泛型 VS Java 泛型
Kotlin 泛型 VS Java 泛型
36 0
|
9月前
|
Java
ChatGPT告诉你Java内部类 vs. 组合的区别
ChatGPT告诉你Java内部类 vs. 组合的区别
63 0
|
11月前
|
安全 算法 Java
Java 17 VS Java 8: 新旧对决,这些Java 17新特性你不容错过
Java 17 VS Java 8: 新旧对决,这些Java 17新特性你不容错过
901 0
|
11月前
|
Java 编译器 API
Java 9 vs Java 8:引入模块化和JShell的全面升级
Java 9 vs Java 8:引入模块化和JShell的全面升级
119 1
|
11月前
|
分布式计算 并行计算 数据可视化
战斗到底:Java vs. Python - 用哪个更适合处理海量数据?
战斗到底:Java vs. Python - 用哪个更适合处理海量数据?
353 0