Swagger UI Error validation when deployed a spring

2019-07-26 15:50发布

问题:

Swagger-ui.html shows a error at the bottom of the page due to validation

`{"schemaValidationMessages":[{"level":"error","message":"Can't read from file https://www.example.com/v2/api-docs"}]}`

I did some research on it and came to know that we need to turn off the validations to remove the error. but how to turn it off from spring boot application.? or is there any way to edit the swagger-ui.html in a spring boot app ?

回答1:

Since Swagger 2.8.0 many constructors have been deprecated. I think it's better to use the corresponding builder which takes care of all the undefined parameters for us like this:

@Bean
public UiConfiguration uiConfig()
{
    return UiConfigurationBuilder.builder() //
             .displayRequestDuration( true ) //
             .validatorUrl( StringUtils.EMPTY ) // Disable the validator to avoid "Error" at the bottom of the Swagger UI page
             .build();
}

See also this post regarding null or empty string validator



回答2:

I solved it by disabling the validation by adding the following code

`@Bean
 public UiConfiguration getConfig(){
        String validatorUrl = "validatorUrl";
        return new UiConfiguration(validatorUrl);
    }`