Java 在Swagger中为请求参数提供样本值
我在Spring Boot RestController中有一个rest方法的方法签名,如下所示:Java 在Swagger中为请求参数提供样本值,java,rest,spring-boot,swagger,Java,Rest,Spring Boot,Swagger,我在Spring Boot RestController中有一个rest方法的方法签名,如下所示: @RequestMapping( value = "/path", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE ) @ApiImplicitPa
@RequestMapping(
value = "/path",
method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
@ApiImplicitParams({
@ApiImplicitParam(
name = "message",
value = "Message that is sent to the method",
required = true,
dataType = "string",
paramType = "body"
)
})
public @ResponseBody String receiveMessage(@RequestBody String message) {
// ...
return "{\"success\": true}";
}
我想为消息
参数提供一个JSON字符串的“示例”值(例如{“key”:“value”}
)。有人知道我如何使用招摇过市的注释来实现这一点吗?我试过了
@ApiImplicitParams({
@ApiImplicitParam(
// ...
example = "...JSON value..."
)
})
但它不起作用。我希望文档中有一个“样本值”,读者可以点击它,让文档中的参数值字段填充给定的样本值。这可能吗
下面是一个屏幕截图,它可能看起来像:
只是为了防止“无用”的回答:由于我的业务逻辑,我无法将参数的类型从
String
更改为某些类类型。不幸的是,您无法提供原子参数a(字符串、数字等)的示例或示例值
如果参数是具有架构的对象,则只能提供示例,您只需在属性描述中添加example
属性即可:
properties:
firstName:
description: first name
type: string
example: John
最后,您可以在参数的描述中添加一个示例值(apimplicitparam
注释中的value
)
对于springboot用户,假设您有一个REST方法,接受
json
body,但由于某些原因,它没有显式地使用@RequestBody
。按照以下步骤生成正确的招摇过市文档
更新附加型号的SpringFox配置bean
@Bean
public Docket apiDocket() {
return new Docket(DocumentationType.SWAGGER_2)
// ...
.additionalModels(new TypeResolver().resolve(YourRequestModel.class));
}
更新@apimplicitparams
@PostMapping("/your-api")
@ApiOperation(value = "brief description", notes = "Greater details come here")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "header", name = "x-locale", example = "en"),
@ApiImplicitParam(paramType = "body", dataType = "YourRequestModel")
})
public YourResponsetModel processRequest() {
// ...
return null;
}
这将生成带有可选标题x-locale
,以及类型为YourRequestModel
的body
的us-Swagger。您可以尝试以下操作:
public MyObject myController(@ApiParam(example = "examplestring") @RequestParam("name") String name,
@ApiParam(example = "1")@RequestParam("eventNo") int eventNo, @ApiParam(example = "2")@RequestParam("halRequestNo") int halRequestNo){
为什么不使用
@ApiModelProperty
将数据类用作参数和文档?
public MyObject myController(@ApiParam(example = "examplestring") @RequestParam("name") String name,
@ApiParam(example = "1")@RequestParam("eventNo") int eventNo, @ApiParam(example = "2")@RequestParam("halRequestNo") int halRequestNo){