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

Abd*_*mun 4 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()));
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

Pau*_*son 6

您有两种选择:

1)将“错误”替换为“字符串”(小写)。

new ResponseMessageBuilder()
                        .code(500).message("500 message")
                        .responseModel(new ModelRef("string")).build(),
Run Code Online (Sandbox Code Playgroud)

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

new ResponseMessageBuilder()
                        .code(500).message("500 message")
                        .responseModel(new ModelRef("ErrorInfo")).build(),
Run Code Online (Sandbox Code Playgroud)

在此示例中,class 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...

}
Run Code Online (Sandbox Code Playgroud)