如何手动描述 java @RequestBody Map<String, String> 的示例输入?
How can I manually describe an example input for a java @RequestBody Map<String, String>?
我正在设计一个 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
的正确使用方法吗?
Swagger只提供了API,这些注解还是要集成到Springfox框架中才能发挥作用。在发布此问题时,Springfox 不支持 @ExampleProperty
和 @Example
。这可以在#853 and #1536.
中看到
从2.9.0版本开始实现。例如,您可以检查 .
@g00glen00b 的回答中提到的问题似乎已经解决。这是如何完成的代码片段。
在你的控制器中class:
// 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
我正在设计一个 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
的正确使用方法吗?
Swagger只提供了API,这些注解还是要集成到Springfox框架中才能发挥作用。在发布此问题时,Springfox 不支持 @ExampleProperty
和 @Example
。这可以在#853 and #1536.
从2.9.0版本开始实现。例如,您可以检查
@g00glen00b 的回答中提到的问题似乎已经解决。这是如何完成的代码片段。
在你的控制器中class:
// 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