【问题标题】:Getting error while loading swagger-ui.html in spring(5.0.0.RELEASE) mvc在 spring(5.0.0.RELEASE) mvc 中加载 swagger-ui.html 时出错
【发布时间】:2021-04-01 02:14:14
【问题描述】:

无法解析引用,因为:无法解析指针:/definitions/错误不存在于文档中

我点击了这个链接http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api,但是在我为自定义响应消息添加 globalResponseMessage() 方法时遇到了错误。我不明白是什么原因。 请帮忙....TIA

 @Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .build()
            .apiInfo(apiInfo())
            .consumes(getContentType())
            .produces(getContentType())
            .useDefaultResponseMessages(false)
            .globalResponseMessage(RequestMethod.GET, newArrayList(
                    new ResponseMessageBuilder()
                            .code(500).message("500 message")
                            .responseModel(new ModelRef("Error")).build(),
                    new ResponseMessageBuilder()
                            .code(403)
                            .message("Forbidden!!!!!")
                            .build()));
}

【问题讨论】:

  • 我遇到了同样的问题,并发现了一个建议 - github.com/springfox/springfox/issues/1443 在测试之前我与我的团队讨论过,我们不需要覆盖默认响应,所以我只是删除了它并没有测试 hydra 和 jonaskoperdraat 的建议

标签: java spring-mvc swagger-2.0


【解决方案1】:

你有两种选择:

1) 将“Error”替换为“string”(小写)。

new ResponseMessageBuilder()
                        .code(500).message("500 message")
                        .responseModel(new ModelRef("string")).build(),

2) 将“Error”替换为您在响应正文中用于错误信息的类的名称(或为此定义一个Error 类)。示例:

new ResponseMessageBuilder()
                        .code(500).message("500 message")
                        .responseModel(new ModelRef("ErrorInfo")).build(),

在本例中,ErrorInfo 类应位于您的 Web 应用程序的类路径中(可能位于多个 Web 应用程序共享的库中)。示例:

@XmlRootElement
public class ErrorInfo {

    private String url;

    @ApiModelProperty(notes = "HTTP Status Code")
    private int statusCode;

    @ApiModelProperty(notes = "HTTP Reason Phrase")
    private String reasonPhrase;

    @ApiModelProperty(notes = "Mensage to the user")
    private String message;

    @ApiModelProperty(notes = "Ticket created on IT help desk if applicable", required = false)
    private String helpDeskTicket;

    @ApiModelProperty(notes = "Debug information (e.g., stack trace), not visible if runtime environment is 'production'", required = false)
    private String debugInfo;

    public ErrorInfo() {
        // required by Jackson deserialization.
    }

    // ... other constructors, get/set methods...

}

【讨论】:

    【解决方案2】:

    您应该使用原始类,例如 String。如果要使用自定义类(如 Error),您应该在 Docket 定义中添加 additionalModels(typeResolver.resolve (CustomResponseClass.class)) 来解析此模型。这是一个运行良好的代码:

    @Autowired
    TypeResolver typeResolver;
    
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
              .useDefaultResponseMessages(false)
              .directModelSubstitute(Object.class, java.lang.Void.class)
              .select()
              .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
              .build()
              .pathMapping("/")
              .apiInfo(apiInfo())
              .additionalModels(typeResolver.resolve (MensagemVo.class) )
              .globalResponseMessage(RequestMethod.GET,
                    newArrayList(new ResponseMessageBuilder()
                                .code(500)
                                .message("Execution error message")
                                .responseModel(new ModelRef("String"))
                                .build(),
                          new ResponseMessageBuilder()
                                .code(422)
                                .message("Validation error message")
                                .responseModel(new ModelRef("MensagemVo"))
                                .build())
              );
    }
    

    其中TypeResolve 来自com.fasterxml.classmate 包。

    结果图片: documentation result

    【讨论】:

      猜你喜欢
      • 2017-02-19
      • 1970-01-01
      • 2018-08-28
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 2020-10-20
      • 1970-01-01
      • 2019-08-06
      相关资源
      最近更新 更多