Skip to content
Advertisement

SWAGGER – Fetch error v2/apidocs undefined

My swagger was working fine with this code.

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiDetails());
    }
    
    private ApiInfo apiDetails() {
        return new ApiInfoBuilder()
                .title(" API")
                .description(" API documentation")
                .version("2.0.1")
                .build();
    }
}

I wanted to add Authorize option in swagger page. So I did this config.

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiDetails())
                .securityContexts(Arrays.asList(securityContext()))
                .securitySchemes(Arrays.asList(apiKey()))
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                ;
    }
    
    private ApiInfo apiDetails() {
        return new ApiInfoBuilder()
                .title("smartportal API")
                .description("smartportal API documentation")
                .version("2.0.1")
                .build();
    }
    
    private ApiKey apiKey() {
        return new ApiKey("JWT","Authorization","header");
    }
    
    private SecurityContext securityContext() {
        return SecurityContext.builder().securityReferences(defaultAuth()).build();
    }
    private List<SecurityReference> defaultAuth(){
        AuthorizationScope authorizationScope = new AuthorizationScope("global","accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        return Arrays.asList(new SecurityReference("JWT", authorizationScopes));
    }
}

From the time I added this new configurations I am getting this error. Swagger error

I have added the v2/api-docs in my security web ignore & http disble configurations already.

.antMatchers("/v2/api-docs/**").permitAll()
            .antMatchers("/v2/api-docs").permitAll()
            .antMatchers("/swagger-resources/configuration/ui").permitAll()
            .antMatchers("/swagger-ui/index.html").permitAll()

My swagger dependencies:

    <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
         <groupId>io.springfox</groupId>
         <artifactId>springfox-swagger-ui</artifactId>
         <version>2.9.2</version>
         </dependency>

please help me with the issue

Advertisement

Answer

My bad.

mistake in the configuration code.

Here is the wrong part of the code corrected.

private List<SecurityReference> defaultAuth(){
        AuthorizationScope authorizationScope = new AuthorizationScope("global","accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return Arrays.asList(new SecurityReference("JWT", authorizationScopes));
    }

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement