在spring(5.0.0.RELEASE)mvc中加载swagger-ui.html时出错

时间:2018-03-31 13:02:06

标签: java spring-mvc swagger-2.0

无法解析引用,原因是:无法解析指针:/ 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()));
}

enter image description here

2 个答案:

答案 0 :(得分:4)

您有两种选择:

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应用程序共享的lib中)。例如:

@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...

}

答案 1 :(得分:1)

你应该使用原始类,比如 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