【问题标题】:Springfox: Alternate type rules are not being appliedSpringfox:未应用替代类型规则
【发布时间】:2019-02-05 13:56:44
【问题描述】:

我的@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 上运行我的服务并打开我的端点时,我看到的只是:

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

P.S.:@EnableSwagger2 的其余部分确实有效。

【问题讨论】:

    标签: swagger swagger-ui springfox


    【解决方案1】:

    似乎已经有一个内部规则,原始类型 Map&lt;String, Object&gt; 会覆盖开发人员添加到 .alternateTypeRules() 的任何内容。

    我能找到解决此问题的唯一方法是创建一个 class MyInputMap extends Map&lt;String, Object&gt; 并在所有相关端点中使用它,同时还将类型规则调整为:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-02
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-04
      • 2016-07-14
      相关资源
      最近更新 更多