13

I am migrating my project which is based on springMVC to spring boot. it is a multi-module project. I am unable to get the swagger-ui.html up. I have added dispatcher servlet as follows:

@Configuration
public class DispatcherServletConfig {

@Bean
public DispatcherServlet dispatcherServlet() {
    return new DispatcherServlet();
}

@Bean
public ServletRegistrationBean dispatcherServletRegistration() {
    ServletRegistrationBean registration = new 
  ServletRegistrationBean(dispatcherServlet(), "/prefix1/*");
    registration.addUrlMappings("/admin/*");
    registration.addUrlMappings("/prefix2/*");
     registration.setName(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME);
    return registration;
}

}
private static final Set<String> DEFAULT_PRODUCES_AND_CONSUMES = 
          new HashSet<String>(Arrays.asList("application/json",
              "application/xml"));

@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurationSupport {

private static final Set<String> DEFAULT_PRODUCES_AND_CONSUMES = 
          new HashSet<String>(Arrays.asList("application/json",
              "application/xml"));

@Bean
public Docket api() {
    ParameterBuilder aParameterBuilder = new ParameterBuilder();
    aParameterBuilder.name("Authorization").modelRef(new ModelRef("string")).parameterType("header")
            .description("Authorization Header").required(true).build();
    List<Parameter> aParameters = new ArrayList<>();
    aParameters.add((Parameter) aParameterBuilder.build());
    return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any()).build().apiInfo(apiInfo()).globalOperationParameters(aParameters).produces(DEFAULT_PRODUCES_AND_CONSUMES);
}

private ApiInfo apiInfo() {
    Contact contact = new Contact("Pvt. Ltd.", "<url>", "support@email.com");
    return new ApiInfo("API", "Information related to api exposed by system.", "1.0",
            "<url>",
            "<url>", new ArrayList<>());
}
}

i have added the following dependencies in my pom.xml:

 <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-excelant</artifactId>
        <version>3.14</version>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3</version>
    </dependency>
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>com.ibm.icu</groupId>
        <artifactId>icu4j</artifactId>
        <version>3.4.4</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.3.2.RELEASE</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.4</version>
    </dependency>
    <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>javax.annotation-api</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itextpdf</artifactId>
        <version>5.0.6</version>
    </dependency>
    <dependency>
        <groupId>com.opencsv</groupId>
        <artifactId>opencsv</artifactId>
        <version>3.8</version>
    </dependency>
    <dependency>
        <groupId>commons-httpclient</groupId>
        <artifactId>commons-httpclient</artifactId>
        <version>3.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>3.11</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-scratchpad</artifactId>
        <version>3.2-FINAL</version>
    </dependency>
    <dependency>
        <groupId>org.apache.xmlbeans</groupId>
        <artifactId>xmlbeans</artifactId>
        <version>2.6.0</version>
    </dependency>
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-bundle</artifactId>
        <version>1.19.1</version>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.10.19</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.scribe</groupId>
        <artifactId>scribe</artifactId>
        <version>1.3.7</version>
    </dependency>
    <dependency>
        <groupId>com.squareup.retrofit2</groupId>
        <artifactId>retrofit</artifactId>
        <version>2.4.0</version>
    </dependency>
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>logging-interceptor</artifactId>
        <version>3.10.0</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.8.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.8.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jms</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-xml</artifactId>
        <version>2.9.6</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.0</version>
    </dependency>
    <dependency>
        <groupId>com.squareup.retrofit2</groupId>
        <artifactId>retrofit</artifactId>
        <version>2.4.0</version>
    </dependency>
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>logging-interceptor</artifactId>
        <version>3.10.0</version>
    </dependency>
    <dependency>
        <groupId>com.squareup.retrofit2</groupId>
        <artifactId>converter-jackson</artifactId>
        <version>2.4.0</version>
    </dependency>
</dependencies>

and also a plugin:

<plugin>
            <groupId>io.github.robwin</groupId>
            <artifactId>swagger2markup-maven-plugin</artifactId>
            <version>0.9.3</version>
            <configuration>
                <inputDirectory>${project.basedir}/apiDoc/json</inputDirectory>
                <swaggerFile>swagger.json</swaggerFile>
                <outputDirectory>${project.basedir}/apiDoc/asciidoc</outputDirectory>
                <markupLanguage>asciidoc</markupLanguage>
            </configuration>
            <executions>
                <execution>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>process-swagger</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

I have stored the apiDocs under my project folder.

I am getting a 404 error whenever i call my {project_name}/prefix2/swagger-ui.html#

enter image description here and whenever i call {project_name}/swagger-ui.html# url, i get this enter image description here

Also, the swagger-ui.html does not get loaded in the apis. enter image description here

How can i solve this issue? Thanks,

Vaibhav Savala
  • 315
  • 2
  • 7
  • 17

12 Answers12

27

For those who are using the newer version of swagger, here is the solution for accessing the swagger ui page.

Replace this:

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

with this:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

in your pom.xml file.

Access the page via the following url (avoid that .html extension): http://localhost:8080/swagger-ui/

Rebuild and restart the server. And your problem is solved!

  • 2
    Take my money. This saved so much time. The point that I missed earlier was, just updating the version won't work as the `artifactId` itself is changed from `springfox-swagger2` & 'springfox-swagger-ui' to 'springfox-boot-starter'. – Gaurav Apr 12 '22 at 06:19
  • Worked perfect in my case. Thanks mate. – Muhammad Tariq Aug 15 '22 at 08:39
19

Try using

swagger-ui/index.html

instead of

swagger-ui.html
akhil
  • 199
  • 1
  • 3
11

In my case downgrading swagger version did the trick.

Couldn't see swagger ui (404)

<swagger.version>3.0.0</swagger.version>

Works fine:

<swagger.version>2.9.2</swagger.version>

Dependencies:

 <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>${swagger.version}</version>
  </dependency>
  <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>${swagger.version}</version>
  </dependency>
valijon
  • 1,304
  • 2
  • 20
  • 35
  • This saved my day.. Thanks for your answer valijon.. – manish bansal Oct 20 '20 at 12:59
  • 3
    I tried this but it did not work. The problem is that I was able to access `/v2/api-docs`, but not `swagger-ui.html` – Chetan Oswal Oct 26 '20 at 12:29
  • @ChetanOswal I had exact same situation. Please, check if maven is reloading dependencies after the change – valijon Oct 26 '20 at 14:03
  • 1
    Hey @valijon, thanks for your prompt reply. I solved the issue. I opened my app in `incognito` mode and the page `swagger-ui.html` was rendering. Then, I again opened normally and it wasn't working. I **inspected** the page, went to network tab, right clicked an API and clicked on option `Clear browser cache`, and voila! The page rendered properly. Surprised, yes I'm. – Chetan Oswal Oct 26 '20 at 14:13
7

I believe there was a problem with webMvcConfigurer. In this particular case your SwaggerConfig Bean is annotated only with @Configuration and @EnableSwagger2. Those two annotations are enough to read configuration and run Springfox on url:

http://servert:port /project-name/api/v2/api-docs

But if you want to expose a webPage you need to enable webMvc by adding @EnableWebMvc annotation on your configuration bean, and provide webMvcConfigurer with proper resource handlers.

Without any other configurations code I posted below is enough to expose Swagger UI:

@Configuration
@EnableSwagger2
@EnableWebMvc
public class SwaggerConfig {

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


    @Bean
    public WebMvcConfigurer webMvcConfigurer()
    {
        return new WebMvcConfigurer()
        {
            @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/" );
            }
        };
    }
}
dgebert
  • 1,235
  • 1
  • 13
  • 30
2

Try adding the following code in your swagger config file:

@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
    registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}*
ross
  • 2,684
  • 2
  • 13
  • 22
scott_dennis
  • 77
  • 1
  • 7
2

(Reputation prevents me from commenting answers)

In my case downgrading swagger also did the trick

From:

<swagger.version>3.0.0</swagger.version>

To

<swagger.version>2.9.2</swagger.version>
Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107
OskarS
  • 65
  • 8
2

for Swagger 3.0, you can use

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

then the UI is http://localhost:8080/swagger-ui/ instead of http://localhost:8080/swagger-ui.html

jguo
  • 21
  • 1
1

Check the jar version of swagger-ui. adding

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

and in pom.xml

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

with url : http://localhost:8080/swagger-ui.html

This worked fine for me. If you are using some other version of springfox-swagger-ui jar, just open that jar and and check the path for swagger-ui.html and change your URL accordingly.

David Buck
  • 3,752
  • 35
  • 31
  • 35
Noha
  • 59
  • 3
0

In a spring boot project you have to do :

The configuration of Swagger mainly centers around the Docket bean.

@Bean
public Docket api() {                
    return new Docket(DocumentationType.SWAGGER_2)          
      .select()                                       
      .apis(RequestHandlerSelectors.basePackage("org.example.sp.controller"))
      .paths(PathSelectors.ant("/foos/*"))                     
      .build();
}

Swagger 2 is enabled through the @EnableSwagger2 annotation.

To verify that Springfox is working, you can visit the following URL in your browser:

http://servert:port /project-name/api/v2/api-docs

The result is a JSON response with a large number of key-value pairs, which is not very human-readable. Fortunately, Swagger provides Swagger UI for this purpose.

And to check you swagger-ui, you can do it via url : http://server:8080/toto/swagger-ui.html

And with this aproche we have to annotate our endpoints by some swagger annotations: There is a simple doc here

Or you can follow an example here

Abder KRIMA
  • 3,418
  • 5
  • 31
  • 54
  • i have this bean as well already. Still, it doesnt work. what i think is this issue https://github.com/springfox/springfox/issues/2037, but the solutions provided did not work. @TinyOS – Vaibhav Savala Nov 24 '18 at 07:31
  • I did not see @EnableSwagger2 any where ! Could you remove all what you have about swagger in your projet (excepte the dependencies ) and try to create a class SwaggerConfig where you put what i gave you then you try again and tell me what you get ? – Abder KRIMA Nov 24 '18 at 20:44
  • I have added EnableSwagger2 annotation and the Docket Bean was added in SwaggerConfig itself. i have edited my post. I think there is a problem with dependencies. i ll post my dependencies as well. @TinyOS – Vaibhav Savala Nov 25 '18 at 13:00
0

I got the same problem too.

1.)first you need to add the following annotations to the config file:

@SpringBootApplication
@EnableSwagger2
public class PostApplication {

   public static void main(String[] args) {
      SpringApplication.run(PostApplication.class, args);
   }

}

2.) second Add 3 dependencies in pom.xml

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

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

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

3.)finally open http://localhost:8081/swagger-ui.html 8081: your server port

0

use the following dependency, it is very straightforward, you will not need to add anything else

     <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-ui</artifactId>
        <version>1.6.9</version>
    </dependency>

Url is

 http://localhost:8090/swagger-ui/index.html

and make sure you are authorized to view all the APIs you want to view in spring security

       .anyRequest().permitAll();

 
Odwori
  • 1,460
  • 13
  • 14
0

Below are the dependencies I have for the spring-boot project -

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.0-RC2</version> 

<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.0.0-M4</version>

Main class has this annotation -

@ComponentScan(basePackages = {"org.springdoc", "pckage_name_of_your_main_class"})
Hardik
  • 1
  • 3