springfox: Can't find Swagger UI endpoint

Please take the time to search the repository, if your question has already been asked or answered.

  • What version of the library are you using? 2.8.0 With spring boot.
    <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
    <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>

What kind of issue is this?

  • Question. Is this a question about how to do a certain thing?

Hello,

I’m sure this is a stupid question but I’ve followed the steps on http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api to get the swagger / swagger ui to work.

My docket config looks as follows:

  @Bean
  public Docket mainConfig() {
    return new Docket(DocumentationType.SWAGGER_2)
        .select().apis(RequestHandlerSelectors.any())
        .paths(PathSelectors.any())
        .build()
        .pathMapping("/swagger")
        .directModelSubstitute(LocalDate.class, String.class)
        .genericModelSubstitutes(ResponseEntity.class);
  }

With this I can go to /v2/api-docs to see the json version. However I can’t find the UI endpoint. When going to http://localhost:8080/swagger-resources/ I only see:

[
{
"name": "default",
"url": "/v2/api-docs",
"swaggerVersion": "2.0",
"location": "/v2/api-docs"
}
]

I’ve tried:

To no avail.

Thank you!

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 4
  • Comments: 69 (16 by maintainers)

Commits related to this issue

Most upvoted comments

I have encountered this problem before and the problem was in the below line in application.properties spring.resources.add-mappings=false

remove it or change it’s value to true

Hello @nWidart,

I was facing the same issue, but I’ve figured out what was happening in my case…

Although I see you have fixed the problem already, for those whom this suggestion hasn’t worked, if you’re using @EnableMvc in any java config file, remove it. I’ve not quite believed at the beginning, but it has worked.

Give it a try…

Regards,

Luciano Daher.

Hi @dilipkrish , I’m facing the same 404 issue. The /v2/api-docs works fine, and the response to http://localhost:8080/swagger-resources/ looks exactly same as @nWidart 's description. I don’t know if it’s resulted from multiple modules with spring boot in my scenario, because the log shows

No mapping found for HTTP request with URI [/swagger-ui.html] in DispatcherServlet with name ‘dispatcherServlet’


It work fine now. I found there ware 2 WebMvcConfigurationSupport in the project… @nWidart

Hello,

Problem is not fixed for me. I don’t have this @EnableMvc personally.

It should be available @ http://localhost:8080/swagger-ui.html. If you don’t see it, are you seeing a 404 error or are you seeing a 401 (unauthorized)? Since its based on the tutorial, is that repository shareable?

Hello,

I’m getting a 404 on that path.

Same error with version 3.0.0. I downgraded to 2.9.2 and it worked.

@rs-renato Please try 2.9.2. But I sincerely doubt it will fix it.

Are you seeingunable to infer base host? It is a very environment specific problem. Some of the common reasons for those are

  • You are running in a load balanced environment or are fronted by an api gateway. If thats the case you need to set up the x-forward headers correctly and enable that behavior in spring boot.
  • If you are running a boot app but elected to configure the web application yourself, either by @EnableWebMvc annotation or implementing WebMvcConfigurationSupport, then you need to configure your swagger-ui on your own (add resource handlers). The web jar will not be auto configured for you.
  • You’re running the application through your IDE and have very specific environmental settings that run the project. If this is the case, you may look at working with a standard deployment archive uber jar, war etc. that can be deployed to a standard container using a reproducible build.
  • Lastly, If your running into this issue because you have configured your servlet incorrectly, that is not easy to figure out as a library without digging into your source.

What would help me figure out the problem easier is one of the following

  • provide reproducible steps for demonstrating the behavior
  • share a repository that demonstrate this behavior

Hope that helps.

I would like to share some tips: if your project enabled with @EnableWebMvc, that means your swagger resource won’t be able to load into META-INF/resources. In order to load it, in case that project is really need annotations @EnableWebMvc for other reason, you need to override resource handlers and add the following lines below:

@Configuration
@EnableWebMvc
class MvcWebConfig : WebMvcConfigurer {
    override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/")
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

springfox-swagger-ui 2.9.2, is not working with AWS ALB. It is trying to redirect on the /api/null/swagger-resources/configuration/ui when trying to access /swagger-resources/configuration/ui. not sure why null is being added.

At local it works OK though with localhost.

This is the config I have …/swagger-resources/configuration/ui" is being redirected to /api/null/swagger-resources/configuration/ui.

/**

  • {@inheritdoc} */ @override public void addViewControllers(ViewControllerRegistry registry) { registry.addRedirectViewController(“/api/customer-api/api-docs”, “/api/api-docs”).setKeepQueryParams(true); registry.addRedirectViewController(“/api/swagger-resources/configuration/ui”, “/swagger-resources/configuration/ui”); registry.addRedirectViewController(“/api/swagger-resources/configuration/security”, “/swagger-resources/configuration/security”); registry.addRedirectViewController(“/api/swagger-resources”, “/swagger-resources”); }

/**

  • {@inheritDoc} */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(“/api/**”).addResourceLocations(“classpath:/META-INF/resources/”); }

I can second @luismh - same issue and versions - removed @EnableWebMvc and works fine.

Actually, come to think of it, removing @EnableWebMvc fixes quite a few things!

Facing the same issue here with 2.8.0, 2.7.0 works well

I’ve faced with the same issue on 3.0.0 and 2.10.5 - swagger-ui.html returns 404. On 2.9.2 it works https://github.com/mfvanek/spring5-mvc-with-embedded-tomcat

Update: I’ve restored 3.0.0 version with fixes according to official documentation. Everything is OK, but the documentation is slightly confusing and misleading

Just my 5 cents… i compiled my project with Java 11 and it gave me same issue. I downgraded to version Jave 8 and it works without further changes to above.

With having trouble in Java 11, the entire Project technically hasn’t built, so it wasn’t even creating the db tables (hibernate), but it showed as running in tomcat.

@rs-renato Please try 2.9.2. But I sincerely doubt it will fix it.

Are you seeingunable to infer base host? It is a very environment specific problem. Some of the common reasons for those are

  • You are running in a load balanced environment or are fronted by an api gateway. If thats the case you need to set up the x-forward headers correctly and enable that behavior in spring boot.
  • If you are running a boot app but elected to configure the web application yourself, either by @EnableWebMvc annotation or implementing WebMvcConfigurationSupport, then you need to configure your swagger-ui on your own (add resource handlers). The web jar will not be auto configured for you.
  • You’re running the application through your IDE and have very specific environmental settings that run the project. If this is the case, you may look at working with a standard deployment archive uber jar, war etc. that can be deployed to a standard container using a reproducible build.
  • Lastly, If your running into this issue because you have configured your servlet incorrectly, that is not easy to figure out as a library without digging into your source.

What would help me figure out the problem easier is one of the following

  • provide reproducible steps for demonstrating the behavior
  • share a repository that demonstrate this behavior

Hope that helps.

I had the same issue, just removed @EnableWebMvc and works! Thanks.

  • Spring Boot 2.0.5.RELEASE
  • springfox-swagger-ui 2.9.2

i solved the problem, the reason is I have an endpoint from spring mvc, which conflict with /swagger-ui.html

https://stackoverflow.com/questions/49152204/swagger-ui-html-400-bad-request

@PutMapping("/{id}")
  public void update(@PathVariable("id") ObjectId departmentId,
      @RequestBody DepartmentUpdate update){
    update.validate();
    departmentApp.update(departmentId, update);
  }

I’m using spring boot 2.0.3 and swagger 2.9.2. swagger-ui also give me the error unable to infer base host. Through debug the springfox.js of 2.8.0, it turns out that swagger-resources request expect array but get a object by wrapped @RestControllerAdvice. So the js promt unable to infer base host. After remove the wrap behavior of swagger, swagger-ui works.

Hello @nWidart,

I was facing the same issue, but I’ve figured out what was happening in my case…

Although I see you have fixed the problem already, for those whom this suggestion hasn’t worked, if you’re using @EnableMvc in any java config file, remove it. I’ve not quite believed at the beginning, but it has worked.

Give it a try…

Regards,

Luciano Daher.

This is what just helped me. I was migrating an old project and still had this. Thanks a million!!

Hello @nWidart,

I was facing the same issue, but I’ve figured out what was happening in my case…

Although I see you have fixed the problem already, for those whom this suggestion hasn’t worked, if you’re using @EnableMvc in any java config file, remove it. I’ve not quite believed at the beginning, but it has worked.

Give it a try…

Regards,

Luciano Daher.

This worked for me. Now I am finding the right configuration.

If you are running a boot app but elected to configure the web application yourself, either by @EnableWebMvc annotation or implementing WebMvcConfigurationSupport, then you need to configure your swagger-ui on your own (add resource handlers). The web jar will not be auto configured for you.

Are there docs or any starting places for doing this?

I dont understand the problem you’re having

I have:

    <!-- Swagger -->
    <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
    <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>

Suggest to move to SpringDoc

Den 05-12-2022 kl. 00:06 skrev Yahm:

Hi, Although it’s an old issue, the solutions proposed here didn’t work for me. what worked was replacing the dependencies in the pom.xml like stated in here https://stackoverflow.com/a/65792216

— Reply to this email directly, view it on GitHub https://github.com/springfox/springfox/issues/2396#issuecomment-1336542878, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABMOTX6TVR6QSIKJIZZSEALWLUPXFANCNFSM4E6ORN4A. You are receiving this because you were mentioned.Message ID: @.***>

Following the example from @siengsotheara from above. This is the Java version.

@Configuration
public class ApiDocsConfiguration implements 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/");
    }
}

If you have @EnableWebMvc, WebMvcConfigurer or WebMvcConfigurationSupport somewhere in your project, this should fix the problem.

I can now access the UI on this path: *base*/swagger-ui/index.html

Greetings from Munich

Had the same issue of the swagger-ui no longer showing up. Took me a while to find out where the issue was. Turned out I had a /{id} mapped to my general root which then caused /swagger-ui.html stating there was a missing ‘id’ parameter

I had the same problem. I didn’t have the swagger-ui dependency on my gradle dependencies. Added the swagger-ui dependency and it worked. Ended up with this:

dependencies {
	...
	implementation("io.springfox:springfox-swagger2:2.9.2")
	implementation("io.springfox:springfox-swagger-ui:2.9.2")
}

https://github.com/oeresundsgruppen/swaggerTest - updated and Tomcat deployment works now. dispatcher needed setup. Months of debugging over. Thanks @dilipkrish - hope others can re-use this.

Like @dilipkrish said:

@nWidart Do you have an configuration that subclasses WebMvcConfigurationSupport?

i removed the extends and its works like a charm

Thank you

In that case perhaps one of your endpoints is pointing has a request mapping of /{something}

Have you include the springfox-swagger-ui dependency?