问题描述
似乎SwaggerUI中存在一个错误,因为一旦我将@ExampleProperty
的mediaType设置为application/json
,示例值就为空,如下所示:
我尝试了几种方法,但是都没有用。据此,这似乎是一个受欢迎的问题: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",examples =
@Example(
value = {
@ExampleProperty(
mediaType = "application/json",value = "{\"code\" : \"42\",\"message\" : \"Invalid ID supplied\"}")
}))
一个简单的版本(不起作用):
@ApiResponses(
value = {
@ApiResponse(
code = 200,examples =
@Example(
value = {
@ExampleProperty(
value = "{'property': 'test'}",mediaType = "application/json")
}))
但是一旦我将mediaType更改为没有特定类型,它就会起作用:
@ApiResponses(
value = {
@ApiResponse(
code = 200,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',name: 'springfox-boot-starter',name: 'springfox-data-rest',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",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
中定义响应类:response = CatOrDogResponse.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
而不是*/*
。正确的缩进会自动起作用。正如您所看到的,我不再在produces
中使用consumes
和@ApiOperation
,因为它没有用。