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

2

6

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

Fullerton answered 31/3, 2018 at 13:2 Comment(1)
I had the same issue and found that one as a suggestion - github.com/springfox/springfox/issues/1443 before test it I discuss with my team and we didn't need to override the default response, so I just removed it and didn't test the suggestion from hydra and jonaskoperdraatHunch
S
9

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

}
Spermatophyte answered 24/4, 2018 at 12:29 Comment(0)
B
2

You should use primitives classes, like String. In case to use custom classes (like Error), you should resolve this model adding additionalModels(typeResolver.resolve (CustomResponseClass.class)) in the Docket definition. Here is a code that works fine:

@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())
          );
}

Where TypeResolve is from com.fasterxml.classmate package.

Image of result: documentation result

Brough answered 31/3, 2021 at 14:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.