Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swagger-ui.html page not working springboot

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>", "[email protected]");
    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,

like image 745
Vaibhav Savala Avatar asked Nov 23 '18 21:11

Vaibhav Savala


2 Answers

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!

like image 159
Abhijith Ravindran Avatar answered Oct 20 '22 18:10

Abhijith Ravindran


Try using

swagger-ui/index.html

instead of

swagger-ui.html
like image 41
akhil Avatar answered Oct 20 '22 18:10

akhil