Braindead
Braindead

Reputation: 61

Trying to reach swagger-ui or api/api-docs but got a 404 error

It's my first post so feel free to leave some feedback or if i do something wrong :)

I am using spring-boot and resteasy :

<!-- Spring Boot -->
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-dependencies</artifactId>
 <version>2.1.0.RELEASE</version>
 <type>pom</type>
 <scope>import</scope>
</dependency>
<dependency>
  <groupId>com.paypal.springboot</groupId>
  <artifactId>resteasy-spring-boot-starter</artifactId>
  <version>2.3.4-RELEASE</version>
</dependency>

I am trying to use Swagger to have a view of my endpoints, so I added this dependency :

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

This dependencies are loaded to my repo, everything looks good.


I added this class to make the easiest config class :

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

When i Launch the app in the debug mod I enter in this previous Bean. Evrything looks fine.


When I launch the Spring app :

@SpringBootApplication
@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class,RabbitAutoConfiguration.class})
public class SituationServicesApplication implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(SituationServicesApplication.class, args);
    }
    @Override
    public void run(String... args) throws Exception {
    }
}

Everything looks fine :

2019-07-06 15:43:27.222  INFO 6336 --- [           main] pertySourcedRequestMappingHandlerMapping : Mapped URL path [v2/api-docs] onto method [public org.springframework.http.ResponseEntity<springfox.documentation.spring.web.json.Json> springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)]

But when I try to reach the swagger-ui.html :

http://localhost:8092/test/v2/api-docs

or

http://localhost:8092/test/swagger-ui.html

I've got a 404 error :

"RESTEASY003210: Could not find resource for full path: http://localhost:8092/test/v2/api-docs".

I tried to modify the default URL by adding a property : springfox.documentation.swagger.v2.path=v2/api-docs-test

It stills 404.

I tried from scratch with a new empty project and it worked just fine. Something is wrong with my project I guess.

I am sure of the URL used.

Do you know how I can debug this issue? Can I see some created source from Swagger? If I can put some log on it to know where the issue comes from?

Upvotes: 6

Views: 10966

Answers (2)

Amol Ambekar
Amol Ambekar

Reputation: 1

@Pierrot, could you please check whether the context-path and port configured in application and the one used in the URL to test swagger UI is correct? As you mentioned that when you created a new project from scratch it worked fine, the issue may be with the context-path/port.

Upvotes: 0

Romil Patel
Romil Patel

Reputation: 13727

If you are using Spring Security, then you have to add some more configuration as below.

Complete Working Example

SecurityConfig.java

package com.swagger.demo.security;


import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter implements WebMvcConfigurer{

    //Swagger Resources
        @Override 
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        }

    //If you are using Spring Security Add Below Configuration

    @Override
    public void configure(WebSecurity web) throws Exception {
        web
          .ignoring()
            .antMatchers("/v2/api-docs", "/configuration/**", "/swagger*/**", "/webjars/**")
            .antMatchers(HttpMethod.OPTIONS,"/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception{

         http
         .csrf().disable()
         .authorizeRequests()
         .antMatchers("/**", "/v2/api-docs", "/configuration/**", "/swagger*/**", "/webjars/**")
         .permitAll()
         .anyRequest().authenticated(); 
    }

}

SwaggerConfig.java

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
   @Bean
   public Docket apiDocket() {

       Docket docket =  new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.swagger.demo"))
                .paths(PathSelectors.any())
                .build();

       return docket;

    } 
}

pom.xml

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-core</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency> 

Upvotes: 3

Related Questions