Springfox:替代类型规则未应用

时间:2019-02-05 13:56:44

标签: swagger swagger-ui springfox

我的@EnableSwagger2带注释的类包含以下方法:

@Bean
    public Docket myServiceApi() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("My Service API").apiInfo(apiInfo()).select()
            .paths(PathSelectors.regex("/api.*")).build()
            .alternateTypeRules(
                newRule(
                    typeResolver.resolve(Map.class, String.class, Object.class),
                    typeResolver.resolve(InputExample.class)
                )
            )
            ;

    }

InputExample是一个类,其中包含许多用@ApiModelProperty注释的属性。

REST控制器中的方法如下:

@ApiOperation(
        value = "Do stuff",
        consumes = MediaType.APPLICATION_JSON_VALUE,
        produces = MediaType.APPLICATION_JSON_VALUE,
        response = SomeOutput.class
    )
    @RequestMapping(
        value = "/api/v1/stuff",
        method = RequestMethod.POST,
        consumes = {MediaType.APPLICATION_JSON_VALUE},
        produces = {MediaType.APPLICATION_JSON_VALUE}
    )
    @ApiResponses(
        value = {
            @ApiResponse(code = 200, message = "Service execution successful"),
            @ApiResponse(code = 400, message = "Bad input data"),
            @ApiResponse(code = 500, message = "An internal server error occurred"),
            @ApiResponse(code = 503, message = "The service is currently unavailable")
        }
    )
    public ResponseEntity<SomeOutput> doServiceStuff(
        HttpServletRequest request,
        @RequestBody Map<String, Object> inputContent
    ) throws
        ValidationException,
        ServiceUnavailableException,
        IOException,
        WorkflowDocumentProcessingException
    {
    ...
    }

可悲的是,当我运行我的服务并在Swagger UI上打开端点时,我看到的只是:

Parameters in Swagger UI

这可能是由什么引起的?我该如何调试呢?

P.S .: @EnableSwagger2-类的其余部分确实起作用。

1 个答案:

答案 0 :(得分:0)

似乎已经有一个内部规则,其原始类型为Map<String, Object>,覆盖了开发人员添加到.alternateTypeRules()的所有内容。

我发现解决此问题的唯一方法是创建一个class MyInputMap extends Map<String, Object>并将其用于所有相关端点,同时还将类型规则调整为:

newRule(
   typeResolver.resolve(MyInputMap.class),
   typeResolver.resolve(InputExample.class)
)