我使用Swagger UI框架来记录API。我拥有的API之一是
@GET
@Path("/books/count")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Get total book count in system")
@ApiResponses(value=@ApiResponse(code = 200, message = "Successful operation", response=**need custom response here**))
public Response getBookCount(){
int count = bookService.getCount();
JSONObject jsonObj = new JSONObject();
jsonObj.put("Count", count);
return Response.status(Status.OK).entity(jsonObj.toString()).build();
}上面的API返回一个具有'Count‘字段的Json对象。那么,有没有可能有一个Swagger-UI响应,比如-
@value=@ApiResponse(code= 200,message =“操作成功”,response={"Count“:”int“})
我不想在这里编写一个具有int字段计数的类,因为我有更多的API只有一个或两个字段的自定义响应,并且我不希望为每个自定义响应编写单独的类。
如果这个问题需要进一步的输入,请让我知道。
在此之前,非常感谢您。
发布于 2020-06-30 01:46:17
您可以通过使用泛型类型或对象来执行此操作。对我来说,对象更有意义。
请尝试使用下面的swagger.yml,从中生成的代码将是泛型类型。
openapi: 3.0.1
info:
title: Swagger Bookstore
description: 'This is a sample server Bookstore server.'
version: 1.0.0
servers:
- url: http://bookstore.swagger.io/v2
tags:
- name: book
description: Everything about your Books
paths:
/books/count:
get:
tags:
- book
summary: Get Books count
description: Fetch total book count
operationId: getBookCount
responses:
200:
description: successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/ApiResponse'
400:
description: Invalid response
content: {}
components:
schemas:
ApiResponse:
type: object
properties:
code:
type: integer
format: int32
message:
type: string
response:
type: object您可以看到ApiResponse有一个字段名response,它是作为对象的泛型类型。它可以容纳您指定的任何类型?
response:
type: object由此生成的代码将类似于:
@ApiResponses(value=@ApiResponse(code = 200, message = "Successful operation", response={}))所以,在这里,response={}可以保存您想要的任何内容。例如response={count=5}或您指定的任何类型。
请尝试这将解决您的问题。
https://stackoverflow.com/questions/62639632
复制相似问题