37

I'm trying to integrate my Spring Boot version 2.0.1.RELEASE with Swagger.

From this blog post it seemed like it will be easy by just adding two Maven dependencies and everything should work.

So I added the following dependencies to the pom:

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

And created the SwaggerConfig bean:

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

And in the properties file I ended up with these 3 entries during the attempts to make it work:

spring.application.name=cat-service
management.server.servlet.context-path=/cat-service
server.servlet.contextPath=/cat-service

But at the end, when accessing

http://localhost:8080/cat-service/api/v2/api-docs

or the UI page at

http://localhost:8080/cat-service/swagger-ui.html

I get a page not found error.

I found this issues in the swagger github page and this question in stackoverflow but I was not able to change my 404 error.

H3AR7B3A7
5,3593 gold badges22 silver badges42 bronze badges
asked May 31, 2018 at 6:53
7
  • You also need the proper configuration including the @EnableSwagger2 annotation as mentioned in the article. Do you have those, and if so, could you post that in your question as well? Commented May 31, 2018 at 6:58
  • Did you add the swaggerconfiguration? Commented May 31, 2018 at 6:59
  • spring-security-rest is not applicable for you Commented May 31, 2018 at 6:59
  • Duplicate : stackoverflow.com/a/64333853/410439 Commented Oct 13, 2020 at 13:00
  • Does this answer your question? Added Springfox Swagger-UI and it's not working, what am I missing? Commented Aug 18, 2021 at 9:44

22 Answers 22

34

I was able to make it work with Spring boot version 2.0.4.RELEASE and this blog post:

I added these dependencies:

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

And this configuration file:

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 SpringFoxConfig {
 @Bean
 public Docket apiDocket() {
 return new Docket(DocumentationType.SWAGGER_2)
 .select()
 .apis(RequestHandlerSelectors.any())
 .paths(PathSelectors.any())
 .build();
 }
}

And it worked.

The Swagger UI can be reached at /swagger-ui.html#

answered Sep 13, 2018 at 8:43
Sign up to request clarification or add additional context in comments.

1 Comment

spring-boot 2.6.2 onwards better to go with springdoc-ui
32

Please check the reference: https://springfox.github.io/springfox/docs/current/

"2.1.3. Migrating from existing 2.x version"

You can remove springfox-swagger2 and springfox-swagger-ui from your pom.xml and add springfox-boot-starter instead (for example version 3.0.0). Also you can remove the @EnableSwagger2 annotations

And: "swagger-ui location has moved from https://host/context-path/swagger-ui.html to https://host/context-path/swagger-ui/index.html OR https://host/context-path/swagger-ui/ for short. This makes it work much better with pulling it as a web jar and turning it off using configuration properties if not needed."

answered Mar 2, 2021 at 7:54

1 Comment

the path of the ui page made it work - swagger-ui/index.html
19

OpenAPI

Just use springdoc-openapi-ui instead.

The Maven dependency:

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

Or Gradle:

implementation 'org.springdoc:springdoc-openapi-ui:1.6.11'

Links

That's really all there is to it... No annotations / configuration needed. Cheers!

For Spring Security

If you are using spring security, make sure you can reach these paths for it to work:

  • "/swagger-ui.html" <-- for UI
  • "/swagger-ui/**" <-- for UI redirects
  • "/v3/api-docs/**" <-- for json docs and openapi configuration
  • "/v3/api-docs.yaml" <-- for yaml docs

For more information: https://www.baeldung.com/spring-rest-openapi-documentation

answered Jul 7, 2021 at 17:55

3 Comments

As of September 2021, this should be the accepted answer :)
I can verify as well that on February 2022 this is the only answer. I just added this as H3AR7B3A7 mentioned and thats it. Added on keycloak (security) the paths and thats it
I am not using soring security. and i am using spring doc version 1.6.9 . But i am getting same 404 error for swagger-ui. but /v3/api-docs working fine. Is there any other configuration needed
11

First add SwaggerConfig.java file at the same package of your springboot file like the following example.

@Configuration
@EnableSwagger2
@EnableWebMvc
public class SwaggerConfig extends WebMvcConfigurerAdapter { 
 @Bean
 public Docket api() { 
 return new Docket(DocumentationType.SWAGGER_2) 
 .select() 
 .apis(RequestHandlerSelectors.any()) 
 .paths(PathSelectors.any()) 
 .build(); 
 }
 @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/");
 }
}

try this http://localhost:8080/spring-security-rest/api/swagger-ui.html or http://localhost:8080/spring-security-rest/swagger-ui.html

If that does not work, try to change the path in application.properties

Add this to application.properties:

server.servlet-path=/loop-service

and try the following urls:

http://localhost:8080/loop-service/swagger-ui.html (UI Docs)

http://localhost:8080/loop-service/v2/api-docs (JSON Docs)

Result : enter image description here

answered Jun 26, 2019 at 5:23

1 Comment

Adding the addResourceHandlers did the trick for me. Idk what the code means or does, but thanks 👍
5

I also had the same issue (404 Not Found with springfox 3.0.0). By setting the logging level to "DEBUG", I was able to see endpoints for /v3/api-docs and they worked, but there was nothing about "swagger-ui".

I finally found https://github.com/springfox/springfox/issues/3285, which indicates that:

the new url in 3.0.0 is /swagger-ui/index.html or /swagger-ui/ rather than /swagger-ui.html"

Could they not have added a debug log to indicate where the swagger UI is available?

double-beep
5,68819 gold badges43 silver badges50 bronze badges
answered Feb 18, 2021 at 13:00

Comments

4

Springfox swagger UI opens at path /swagger-ui/index.html.

Make sure you are allowing correct ResourceLocations and path excluded from interceptors

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

Note: i'm using Springfox swagger version 3.0.0 and Springboot version 2.5.3

answered Dec 2, 2021 at 7:00

Comments

2

This worked for me, I used the WebMvcConfigurer instead of the WebMvcConfigurerAdapter because that class is already deprecated.

@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {
 @Bean
 public Docket productApi() {
 return new Docket(DocumentationType.SWAGGER_2)
 .select() 
 .apis(RequestHandlerSelectors.basePackage("com.illary.controller"))
 .paths(PathSelectors.any())
 .build() 
 .apiInfo(metaData());
 }
 private ApiInfo metaData() {
 return new ApiInfoBuilder()
 .title("Spring Boot Swagger App")
 .description("\"Spring Boot Swagger Server App\"")
 .version("1.0.0")
 .license("Apache License Version 2.0")
 .licenseUrl("https://www.apache.org/licenses/LICENSE-2.0\"")
 .build();
 }
 public ApiInfo apiInfo() {
 final ApiInfoBuilder builder = new ApiInfoBuilder();
 builder.title("Swagger Test App").version("1.0").license("(C) Copyright Test")
 .description("The API provides a platform to query build test swagger api");
 return builder.build();
 }
 @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/");
 }
}
answered Aug 16, 2018 at 15:48

Comments

2

For Spring Boot, just use the dependency below, it's all it needs to work on the URL /swagger-ui/ (the trailing slash is mandatory).

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

Before trying that I was trying to use the classic dependencies of swagger2 and swagger-ui, and none of the suggested URLs were working.

answered Jul 11, 2021 at 19:48

1 Comment

come on :D one trailing slash was my problem :) thanks but why is it even necessary??
1

Another possibility is the location of your Swagger config file; you need to place it at the same package or a subpackage of the spring boot file's. Like the above picture:

Swagger config and Spring Boot file hierarchy

answered May 23, 2019 at 6:07

Comments

1

Don't forget to change server.contextPath to server.servlet.contextPath if you upgrade Spring Boot to 2+.

answered Sep 27, 2019 at 14:09

Comments

0

Solution: You just need to remove @EnableWebMvc from the configuration classes.

Description: @EnableWebMvc turn on the class org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport. In spring-boot, there is an autoconfiguration class org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration, which has the annotation @ConditionalOnMissingBean(WebMvcConfigurationSupport.class).

What we get in the end: By adding @EnableWebMvc to the project, we ourselves become responsible for everything, since we turn off spring-boot auto-configuration.

answered Apr 24, 2020 at 10:20

1 Comment

removing it causes Error creating bean with name webMvcRequestHandlerProvider among other things...
0

After adding below line in application.properties, it started working

spring.web.resources.static-locations: classpath:/webapp/

But I'm not sure why do we have to add this. Adding the code, which I think could be relevant. Dependencies are as below:

 <parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>2.4.1</version>
 <relativePath/> <!-- lookup parent from repository -->
 </parent>
 ...
 <dependencies>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter</artifactId>
 </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 <dependency>
 <groupId>javax.validation</groupId>
 <artifactId>validation-api</artifactId>
 <version>2.0.1.Final</version>
 </dependency>
 <!-- Swagger Dependencies -->
 <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>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-test</artifactId>
 <scope>test</scope>
 </dependency>
 </dependencies>

And main class as

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
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;
@SpringBootApplication
@EnableSwagger2
public class ProxyApplication {
 public static void main(String[] args) {
 SpringApplication.run(ProxyApplication.class, args);
 }
 @Bean
 public Docket api() {
 return new Docket(DocumentationType.SWAGGER_2)
 .select()
 .apis(RequestHandlerSelectors.basePackage("com.ghsatpute.proxy"))
 .paths(PathSelectors.any())
 .build();
 }
}
answered Dec 23, 2020 at 18:25

Comments

0

Adding this if it can help somebody. This URL worked for me

http://localhost:8003/v2/api-docs

and for swagger-ui this url worked for me out of the box:

http://localhost:8003/swagger-ui.html 

Check if you are using right configured url for swagger specs. The urls like http://localhost:8080/spring-security-rest/api/swagger-ui/

didn't work , and I had been getting 404.

answered Jan 31, 2021 at 7:45

Comments

0

Below are the steps to enable swagger in spring boot application:

  1. Add springfox-swagger2 and springfox-swagger-ui dependencies.
  2. To enable the Swagger2 in your project you should use @EnableSwagger2
  3. Define a docket bean ad below

 @Bean
 public Docket docket(){
 return new Docket(DocumentationType.SWAGGER_2).groupName("group-name").apiInfo(apiInfo()).select().paths(predicate()).build();
 }
 private Predicate<String> predicate() {
 return or(regex("/api/v1.*"), regex("/api/v2.*"));
 }
 
 private ApiInfo apiInfo() {
 return new ApiInfoBuilder().title("The title of the your choice")
 .contact("[email protected]").license("Licence name ").version("1.0").build();
 }
 
answered Jul 12, 2021 at 16:44

Comments

0

Swagger UI url pattern: localhost:<server.port>/<server.servlet.context-path>/swagger-ui.html

answered Oct 20, 2021 at 15:52

Comments

0

For anyone still looking for this, using OpenAPI 3.0, not Springfox, I got it running using WebMvcConfigurationSupport without the need of @EnableWebMvc. The only Dependency needed:

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

I initially just got the JSON and a 404 on the UI.

What ultimately worked was adding the right resource handlers to my WebConfig and configuring my SecurityConfig to allow unrestricted access for everyone.

The resource handlers in my WebConfig:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
 registry.addResourceHandler("/swagger-ui/**")
 .addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/4.10.3/swagger-initializer.js")
 .addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/4.10.3/index.html")
 .addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/4.10.3/swagger-ui.css")
 .addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/4.10.3/index.css")
 .addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/4.10.3/swagger-ui-bundle.js")
 .addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/4.10.3/swagger-ui-standalone-preset.js");
}

Important was to add each one specifically (with version and everything, ** didn't work). Otherwise it wouldn't run in the cloud.

I also needed to add the paths "/v3/api-docs/**", "/swagger-ui/**" my SecurityConfig to allow access to these resources. So in my case I added those here:

@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
 httpSecurity.addFilterBefore(...
 .authorizeRequests().antMatchers("/v3/api-docs/**", "/swagger-ui/**", "...")...
}

and:

@Override
public void configure(WebSecurity web) throws Exception {
 web.ignoring().antMatchers(HttpMethod.GET, "/v3/api-docs/**", "/swagger-ui/**", "...");
}
answered Jul 7, 2022 at 7:45

Comments

0

I had the same issue. All I had to do was downgrading the Swagger-UI and SpringFox dependencies from 3.0.0 to 2.9.2 version.

answered Mar 30, 2023 at 19:39

Comments

0

Kindly note the compiler version issue as below:

49 = Java 5
50 = Java 6
51 = Java 7
52 = Java 8
53 = Java 9
54 = Java 10
55 = Java 11
56 = Java 12
57 = Java 13
58 = Java 14
59 = Java 15
60 = Java 16
61 = Java 17
62 = Java 18
63 = Java 19
64 = Java 20

if you are using java version 8/11 then below dependency you can use. Note: springdoc-openapi v1.7.0 is the latest Open Source release supporting Spring Boot 2.x and 1.x.

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

use the above dependency and access the api using below sample url. http://localhost:8765/swagger-ui.html

for Spring-boot v3 (Java 17 & Jakarta EE 9)

 <dependency>
 <groupId>org.springdoc</groupId>
 <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
 <version>2.2.0</version>
 </dependency>
answered Aug 11, 2023 at 5:32

Comments

0

Some times it is not able to get the default group name because of a conflict so just add groupName to the Docket build like this:

@Configuration
@EnableSwagger2
public class SwaggerConfig {
 @Bean
 public Docket api() {
 Docket docket = new Docket(DocumentationType.SWAGGER_2)
 .select()
 .apis(RequestHandlerSelectors.any())
 .paths(PathSelectors.any())
 .build()
 .groupName("Your-Group-Name");
 return docket;
 }
}
answered Dec 31, 2023 at 14:39

Comments

-1

It became working for me after removing @EnableWebMvc

answered Feb 14, 2019 at 10:03

1 Comment

removing it causes Error creating bean with name webMvcRequestHandlerProvider among other things...
-1

Faced same issue just resolved with change in dependency Refer https://www.vojtechruzicka.com/documenting-spring-boot-rest-api-swagger-springfox/

compile "io.springfox:springfox-swagger2:2.9.2"

Previously was using - Not working

compile group: 'io.springfox', name: 'springfox-swagger2', version: '3.0.0'

answered Sep 3, 2020 at 19:39

Comments

-1

I was stuck for a day with this issue. My issue was incorrect port configuration. Check your application.yml/application.properties file for port configuration. There I had mistakenly added both server.port , management.server.port with different values 🤣 .

answered Jan 5, 2022 at 2:46

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.