SwaggerUI 响应示例不适用于 mediaType JSON
SwaggerUI Response Examples do not work with mediaType JSON
SwaggerUI 中似乎存在错误,因为一旦我将 @ExampleProperty
的媒体类型设置为 application/json
,示例值就为空,如您在此处所见:
我尝试了几种方法,但 none 有效。据此,这似乎是一个流行的问题:https://github.com/springfox/springfox/issuesW/2352
我尝试了一个不合逻辑的解决方案(无效):
@ApiOperation(
value = "Sends a request to the AiController to predict the request",
produces = "application/json",
consumes = "application/json",
authorizations = @Authorization(value = "Bearer"))
@ApiResponses(
value = {
@ApiResponse(
code = 200,
message = "Successfully retrieved predictions",
examples =
@Example(
value = {@ExampleProperty(mediaType = "application/json", value = "test")}))
另一个(也不起作用):
@ApiOperation(
value = "Sends a request to the AiController to predict the request",
produces = "application/json",
consumes = "application/json",
authorizations = @Authorization(value = "Bearer"))
@ApiResponses(
value = {
@ApiResponse(
code = 200,
message = "Successfully retrieved predictions",
examples =
@Example(
value = {
@ExampleProperty(
mediaType = "application/json",
value = "{\"code\" : \"42\", \"message\" : \"Invalid ID supplied\"}")
}))
更简单的版本(不工作):
@ApiResponses(
value = {
@ApiResponse(
code = 200,
message = "Successfully retrieved predictions",
examples =
@Example(
value = {
@ExampleProperty(
value = "{'property': 'test'}",
mediaType = "application/json")
}))
但是一旦我将 mediaType 更改为没有特定类型,它就会起作用:
@ApiResponses(
value = {
@ApiResponse(
code = 200,
message = "Successfully retrieved predictions",
examples =
@Example({
@ExampleProperty(
mediaType = "*/*",
value = "{\n\"predictions\": [ \n \"cat\" \n]\n}")
}))
输出为:
{
"predictions": [
"cat"
]
}
这几乎是我想要的,但当然缩进是错误的。
还有其他方法可以做一个@ApiResponse
例子吗?我不能用我的 DTO 举个例子吗 (?):
@ApiModel(
value = "Cat or Dog response",
description = "Response of the prediction whether it's a cat or dog")
@Data
public class CatOrDogResponse {
@ApiModelProperty(value = "Images to predict", example = "test", required = true)
private String[] predictions;
}
我用过的 Spring Fox 版本:
compile group: 'io.springfox', name: 'springfox-swagger2', version: '3.0.0'
compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '3.0.0'
compile group: 'io.springfox', name: 'springfox-boot-starter', version: '3.0.0'
compile group: 'io.springfox', name: 'springfox-data-rest', version: '3.0.0'
compile group: 'io.springfox', name: 'springfox-bean-validators', version: '3.0.0'
最后答案很简单。我在 GitHub 上找到了这个:https://github.com/springfox/springfox/issues/2538#issuecomment-637265748
该错误应该已修复,但仍未修复。要获得用于响应的示例值,需要调整 REST 类型的注释:
@PostMapping(value = "/predict", produces = "application/json", consumes = "application/json")
我用我的方法给你一个具体的例子:
@ApiOperation(value = "Sends a request to the AiController to predict the request", response =
CatOrDogResponse.class, authorizations = @Authorization(value = "Bearer"))
@ApiResponses(value = {@ApiResponse(code = 200, message = "Successfully retrieved predictions"),
@ApiResponse(code = 401, message = "You are not authorized to send a request"), @ApiResponse(code = 403,
message = "Accessing the resource you were trying to reach is forbidden"), @ApiResponse(code = 404,
message = "The resource you were trying to reach is not found")})
@PostMapping(value = "/predict", produces = "application/json", consumes = "application/json")
public Mono<CatOrDogResponse> predict(@RequestHeader HttpHeaders headers, @ApiParam(name = "Cat or Dog Prediction "
+ "Request", value = "Parameters for predicting whether an image or multiple images is/are a cat or dog") @Valid @RequestBody CatOrDogRequest catOrDogRequest) {
...
}
重要的是你像我一样在@ApiOperation
中定义响应class:response = CatOrDogResponse.class
在响应 class 中定义响应的示例值:
@ApiModel(value = "Cat or Dog response", description = "Response of the prediction whether it's a cat or dog")
@Data
public class CatOrDogResponse {
@ApiModelProperty(value = "Images to predict", example = "cat")
private String[] predictions;
}
现在发生的变化是最终自动将响应类型声明为 application/json
而不是 */*
。正确的缩进会自动工作。如您所见,我不再在 @ApiOperation
中使用 produces
和 consumes
,因为它们没用。
SwaggerUI 中似乎存在错误,因为一旦我将 @ExampleProperty
的媒体类型设置为 application/json
,示例值就为空,如您在此处所见:
我尝试了几种方法,但 none 有效。据此,这似乎是一个流行的问题:https://github.com/springfox/springfox/issuesW/2352
我尝试了一个不合逻辑的解决方案(无效):
@ApiOperation(
value = "Sends a request to the AiController to predict the request",
produces = "application/json",
consumes = "application/json",
authorizations = @Authorization(value = "Bearer"))
@ApiResponses(
value = {
@ApiResponse(
code = 200,
message = "Successfully retrieved predictions",
examples =
@Example(
value = {@ExampleProperty(mediaType = "application/json", value = "test")}))
另一个(也不起作用):
@ApiOperation(
value = "Sends a request to the AiController to predict the request",
produces = "application/json",
consumes = "application/json",
authorizations = @Authorization(value = "Bearer"))
@ApiResponses(
value = {
@ApiResponse(
code = 200,
message = "Successfully retrieved predictions",
examples =
@Example(
value = {
@ExampleProperty(
mediaType = "application/json",
value = "{\"code\" : \"42\", \"message\" : \"Invalid ID supplied\"}")
}))
更简单的版本(不工作):
@ApiResponses(
value = {
@ApiResponse(
code = 200,
message = "Successfully retrieved predictions",
examples =
@Example(
value = {
@ExampleProperty(
value = "{'property': 'test'}",
mediaType = "application/json")
}))
但是一旦我将 mediaType 更改为没有特定类型,它就会起作用:
@ApiResponses(
value = {
@ApiResponse(
code = 200,
message = "Successfully retrieved predictions",
examples =
@Example({
@ExampleProperty(
mediaType = "*/*",
value = "{\n\"predictions\": [ \n \"cat\" \n]\n}")
}))
输出为:
{
"predictions": [
"cat"
]
}
这几乎是我想要的,但当然缩进是错误的。
还有其他方法可以做一个@ApiResponse
例子吗?我不能用我的 DTO 举个例子吗 (?):
@ApiModel(
value = "Cat or Dog response",
description = "Response of the prediction whether it's a cat or dog")
@Data
public class CatOrDogResponse {
@ApiModelProperty(value = "Images to predict", example = "test", required = true)
private String[] predictions;
}
我用过的 Spring Fox 版本:
compile group: 'io.springfox', name: 'springfox-swagger2', version: '3.0.0'
compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '3.0.0'
compile group: 'io.springfox', name: 'springfox-boot-starter', version: '3.0.0'
compile group: 'io.springfox', name: 'springfox-data-rest', version: '3.0.0'
compile group: 'io.springfox', name: 'springfox-bean-validators', version: '3.0.0'
最后答案很简单。我在 GitHub 上找到了这个:https://github.com/springfox/springfox/issues/2538#issuecomment-637265748
该错误应该已修复,但仍未修复。要获得用于响应的示例值,需要调整 REST 类型的注释:
@PostMapping(value = "/predict", produces = "application/json", consumes = "application/json")
我用我的方法给你一个具体的例子:
@ApiOperation(value = "Sends a request to the AiController to predict the request", response =
CatOrDogResponse.class, authorizations = @Authorization(value = "Bearer"))
@ApiResponses(value = {@ApiResponse(code = 200, message = "Successfully retrieved predictions"),
@ApiResponse(code = 401, message = "You are not authorized to send a request"), @ApiResponse(code = 403,
message = "Accessing the resource you were trying to reach is forbidden"), @ApiResponse(code = 404,
message = "The resource you were trying to reach is not found")})
@PostMapping(value = "/predict", produces = "application/json", consumes = "application/json")
public Mono<CatOrDogResponse> predict(@RequestHeader HttpHeaders headers, @ApiParam(name = "Cat or Dog Prediction "
+ "Request", value = "Parameters for predicting whether an image or multiple images is/are a cat or dog") @Valid @RequestBody CatOrDogRequest catOrDogRequest) {
...
}
重要的是你像我一样在@ApiOperation
中定义响应class:response = CatOrDogResponse.class
在响应 class 中定义响应的示例值:
@ApiModel(value = "Cat or Dog response", description = "Response of the prediction whether it's a cat or dog")
@Data
public class CatOrDogResponse {
@ApiModelProperty(value = "Images to predict", example = "cat")
private String[] predictions;
}
现在发生的变化是最终自动将响应类型声明为 application/json
而不是 */*
。正确的缩进会自动工作。如您所见,我不再在 @ApiOperation
中使用 produces
和 consumes
,因为它们没用。