如何手动描述java @RequestBody Map <String,String>的示例输入?

Jos*_*h J 11 java swagger spring-boot springfox

我正在设计一个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
}
Run Code Online (Sandbox Code Playgroud)

我想提供一个示例输入,fields但我似乎无法让它在swagger输出中呈现.这不是正确的使用方法@Example吗?

g00*_*00b 7

虽然这些@ExampleProperty和@Example属性已经在Swagger中实现,但Springfox还没有支持它们.问题仍然存在:


Mic*_*ihs 6

@ 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;
    }

}
Run Code Online (Sandbox Code Playgroud)

此外,您需要将以下行添加到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));
}
Run Code Online (Sandbox Code Playgroud)

可以在这里找到更多文档:http : //springfox.github.io/springfox/docs/snapshot/#q27