Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting error while loading swagger-ui.html in spring(5.0.0.RELEASE) mvc

Could not resolve reference because of: Could not resolve pointer: /definitions/Error does not exist in document

I followed this link http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api , but getting above error while I add globalResponseMessage() methhod for custom response message.I can't understand what's the reason. Please help....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

like image 718
Abdullah Al Mamun Avatar asked Mar 31 '18 13:03

Abdullah Al Mamun


People also ask

Can we use Swagger in Spring MVC?

Swagger Spring MVC scans the Spring MVC controllers on start-up and registers a documentation controller that exposes the operations the controllers allows. This documentation follows the Swagger specification: any client that understands this specification can use the API.

What is swagger UI HTML?

What is Swagger UI? Swagger UI, a part of Swagger, is an open source tool that generates a web page that documents the APIs generated by the Swagger specification. This UI presentation of the APIs is user friendly and easy to understand, with all logic complexity kept behind the screen.

Where is swagger UI in HTML?

This will be located at localhost:8080/swagger-ui. html. Swagger will display the API endpoints which you have configured it to generate documentation for. From here, the user can make the API calls from this location eliminating the need for a separate REST client.


1 Answers

You have two alternatives:

1) Replace "Error" with "string" (lower case).

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

2) Replace "Error" with the name of the class you use for error information in the response body (or define an Error class for that). Example:

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

In this example, class ErrorInfo should be in the classpath of your web application (could be in a lib shared by several web apps). Example:

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

}
like image 164
Paulo Merson Avatar answered Nov 15 '22 00:11

Paulo Merson