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

2

6

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?

Jilolo answered 6/7, 2019 at 13:51 Comment(5)
Hello @Pierrot are you using Spring Security, if yes you have to add some configurationHeraclitus
@Pierrot where is the /test path defined?Corncrib
Hi Madhu : In the JAXRS config : @Component @ApplicationPath("/test/") public class SituationServicesJaxrsApplication extends ServiceApplication { }Jilolo
Something very strange just happend : I removed it and paste it again and I have been able to access the HTML :o But it is empty of information... : "No operations defined in spec! " It's better than nothing, but still don't understand what just happent and why I am unable to have API's information :s The Docket Bean still very open : .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any())Jilolo
If the "/test/' isn't specified it doesn't work... @ApplicationPath("/")Jilolo
H
3

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> 
Heraclitus answered 6/7, 2019 at 14:43 Comment(8)
There would probably be a 401 or 403 and not a 404 if this was a spring security related issue.Corncrib
Hello @MadhuBhat yes, for security reason it might be 403/401. but when I have tried to implement swagger there was 404 and 403 due to spring security which prevents the resources to loadHeraclitus
Hello Patel, thanks a lot for the answer. Wer not using Spring Security so it cannot be the issue / as far as I know the only constraint we have for the API call is that we need to mention the From in the header for the call : $header-From={name}. If we miss it it returns a 401 response not a 404. But thanks for the advice it's good to know and could help someone else :)Jilolo
@Pierrot Hello, Can you share your project to identify the issue, if possibleHeraclitus
Unfortunately it's a professional and a huge project, I cannot share it :/ The problem is the miss of log / I cannot have a look about the issue because Spring doesn't seems to detect the issue, only the fact that the URL doesn't exist : "RESTEASY003210: Could not find resource for full path: http://localhost:8092/test/v2/api-docs". But it should exist because I enabled it and it was generated :-DJilolo
I understand, Have you added @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/"); }Heraclitus
Hi, I didn't try this because I am using Spring boot and in this doc it was specified for "Configuration Without Spring Boot" only :) I can have a try but I need to extends Spring Mvc right ?Jilolo
Hello @Pierrot yes, you need to add <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> Have you tried on it?Heraclitus
G
0

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

Gipson answered 6/7, 2019 at 17:51 Comment(1)
Hi thanks for the answer ! I am pretty sure about the URL :/ I might force the port to the Swagger side ? I am using a specific 8092 port. But shouldn't be a problem ?Jilolo

© 2022 - 2024 — McMap. All rights reserved.