我正在设计一个应用程序接口,其中一个POST方法接受任何键值对的Map<String, String>
。
@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) Map<String, String> fields) {
// .. does stuff
}
我想为fields
提供一个示例输入,但我似乎无法将其呈现在swagger输出中。这不是使用@Example
的正确方式吗
发布于 2017-01-26 15:33:13
Swagger只提供API,这些注解仍然需要集成到Springfox框架中才能工作。在这个问题发布的时候,@ExampleProperty
和@Example
都没有得到Springfox的支持。这可以在#853和#1536中看到。
从2.9.0版开始,已经实现了这一点。例如,您可以查看this answer。
发布于 2018-10-04 03:47:11
@g00glen00b的答案中提到的问题似乎已经修复。下面是一个代码片段,说明如何做到这一点。
在您的控制器类中:
// omitted other annotations
@ApiImplicitParams(
@ApiImplicitParam(
name = "body",
dataType = "ApplicationProperties",
examples = @Example(
@ExampleProperty(
mediaType = "application/json",
value = "{\"applicationName\":\"application-name\"}"
)
)
)
)
public Application updateApplicationName(
@RequestBody Map<String, String> body
) {
// ...
}
// 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
https://stackoverflow.com/questions/41861164
复制相似问题