我正在设计一个api,其中一个POST方法采用Map
任何键值对.
@RequestMapping(value = "/start", method = RequestMethod.POST) public void startProcess( @ApiParam(examples = @Example(value = { @ExampleProperty( mediaType="application/json", value = "{\"userId\":\"1234\",\"userName\":\"JoshJ\"}" ) })) @RequestBody(required = false) Mapfields) { // .. does stuff }
我想提供一个示例输入,fields
但我似乎无法让它在swagger输出中呈现.这不是正确的使用方法@Example
吗?
虽然这些@ExampleProperty
和@Example
属性已经在Swagger中实现,但Springfox还没有支持它们.问题仍然存在:
https://github.com/springfox/springfox/issues/853
https://github.com/springfox/springfox/issues/1536
@ g00glen00b的答案中提到的问题似乎已解决。这是如何完成的代码段。
在您的控制器类中:
// omitted other annotations @ApiImplicitParams( @ApiImplicitParam( name = "body", dataType = "ApplicationProperties", examples = @Example( @ExampleProperty( mediaType = "application/json", value = "{\"applicationName\":\"application-name\"}" ) ) ) ) public Application updateApplicationName( @RequestBody Mapbody ) { // ... } // Helper class for Swagger documentation - see http://springfox.github.io/springfox/docs/snapshot/#q27 public static class ApplicationProperties { private String applicationName; public String getApplicationName() { return applicationName; } public void setApplicationName(String applicationName) { this.applicationName = applicationName; } }
此外,您需要将以下行添加到Swagger配置中:
// omitted other imports... import com.fasterxml.classmate.TypeResolver; @Bean public Docket api(TypeResolver resolver) { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()) // the following line is important! .additionalModels(resolver.resolve(DashboardController.ApplicationProperties.class)); }
可以在这里找到更多文档:http : //springfox.github.io/springfox/docs/snapshot/#q27