我正在设计一个api,其中一个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
的正确方法吗?
答案 0 :(得分:7)
虽然已经在Swagger中实现了@ExampleProperty
和@Example
属性,但Spring尚不支持它们。问题仍然存在:
答案 1 :(得分:4)
@ 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