'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#
and whenever i call {project_name}/swagger-ui.html# url, i get this

Also, the swagger-ui.html does not get loaded in the apis.

How can i solve this issue? Thanks,
Solution 1:[1]
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!
Solution 2:[2]
Try using
swagger-ui/index.html
instead of
swagger-ui.html
Solution 3:[3]
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>
Solution 4:[4]
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/" );
}
};
}
}
Solution 5:[5]
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/");
}*
Solution 6:[6]
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
Solution 7:[7]
(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>
Solution 8:[8]
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.
Solution 9:[9]
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
Solution 10:[10]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
