springfox: swagger-ui endpoint doesn't seem to work
I’m trying to figure out how to get at the Swagger UI using the Springfox artifacts in a Spring Boot MVC application. Unfortunately the reference documentation is rather misleading, since the neither documented endpoint or the (different) endpoint in the screenshot exist in my application.
I’m using the following dependencies in Maven:
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jaxrs</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.0.1</version>
</dependency>
My Docket Bean is very simple for now:
@Bean
public Docket myAPI() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.paths(paths())
.build();
}
Looking at /mappings in Spring Boot, I have /swagger-resources and /v2/api-docs, but can’t find an endpoint for the UI.
Am I doing something wrong, experiencing a bug, or suffering from bad documentation?
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 15
- Comments: 57 (18 by maintainers)
I have added following in the class where i put @EnableWebMvc annotation. It works fine for me.
I have removed the extra swagger 1.5.0 dependency and ensured that I am using the wordnik package names for the API annotations. This appears to make no difference to the resulting behaviour.
My application is defined with the following annotations:
I did have an
@Configurationclass with@EnableWebMvcon it. Removing the@EnableWebMvcannotation seems to fix the missing/swagger-ui.htmlendpoint. The documentation implies the opposite is true, stating that adding the annotation may fix a NullPointerException.I had to disable security configurations for various swagger-ui related endpoints to get the UI to load properly, but I got there in the end. Thanks for your help.
Ok figured it out. The dispatcherServlet is suppose to map /** to to ResourceHttpRequestHandler, but if you have a request mapping that takes one parameter in my case (
@RequestMapping("/{range}")) then it will override it.I thought this would be ok, because the @RestController class had @RequestMapping(“/query”) annotation (thus the full path being /query/{range}) but apparently not.
So there you go.
You also have to be aware that Swagger 3 changed the URL of the UI from /swagger-ui.html to /swagger-ui/index.html.
I was surprised by seeing the Whitelabel Error Page (404: NotFound) after upgrading from 2.8.0 to Swagger 3.
Thanks to @hala3k for the mentioned link that helped me clarify this.
for me after removing @EnableWebMvc it worked.
@NetRob I just added, for 3.0.0:
Do you by any chance have a custom webconfigurer adapter or perhaps a
@EnableWebMvc?Also 2.0.1 will not work with swagger 1.5.0 ( depends on milestone). The package names have changed between the milestone and release builds.
@igilham mappings will only list
RequestMapping’s.<HOST:PORT/CONTEXT_PATH>/swagger-ui.htmlshould reveal the ui. Not sure whats misleading about this?I had a similar issue and found the reason of what was wrong with my code. I had a class marked by @Configuration derived from WebMvcConfigurationSupport.
Unfortunately, WebMvcConfigurationSupport has an empry method configureDefaultServletHandling() which has to be overriden like this:
@Override protected void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); }
The solution, that has helped me, was to include the aforementioned resource handlers in the class, that enables @EnableWebMvc via a WebMvcConfigurerAdapter:
My class where I needed to activate @EnableWebMvc was the SecurityConfiguration in order to set up Cors mappings. So I moved the swagger resource configuration there: (The solution is ugly, because swagger does not belong into security. But then, spring itself is ugly.)
Context path is dependent on your application. As a library, there is no way to know what your applications context path is. May be we should clarify that in the docs. Usually when you’re testing locally that context path is
/The screenshot is a little misleading as you said because its based on the demo the home forwards to
/swagger-ui.html.Thanks for your feedback! 👍
solved for me by using swagger 3 instead of 2 as swagger 2 might not integrate well with spring-boot 2. this link might be useful.
It was working till yesterday, but now it is not working
I have one @GetMapping(value = “/fallBack”) in that service
the context path is blank port = 9208
localhost:9208/swagger-resources loading localhost:9208/swagger-ui.html not loading
Now, I changed V to 2.10.5, & it is working fine.
Working example with complete code and dependencies using spring boot 2.0.3-RELEASE https://gist.github.com/biniama/ba3e0e1df6fd7d320d1bf242de9d469e
It is a compilation of all the recommended options from the above comments.
I had a similar issue to the one @ndtreviv was experiencing on 9 Mar 2016, however the request mapping for my controller was for “/”. I was porting a legacy application so couldn’t change the URLs of the main application and none of the suggestions above worked for me.
I worked around this issue by adding support for a different path for accessing swagger (http://localhost:8000/swagger/ui/swagger-ui.html). This required the following resource handlers to be set up:
I then needed to introduce Tuckey for URL rewriting with the following configuration:
Spring Boot: 1.5.4.RELEASE Swagger: 2.8.0
@raviksoni25 well, that’s interesting: I cloned your project into Spring Tools Suite and it does not work. Afterwards, I did exactly the same on another machine, and there it worked.
Unfortunately, I do not really have any clue what’s going on there. There are just some really minor differences in the logs. I’m attaching them, though. failing.txt working.txt
/edit: okay. that was a nasty one: after cleaning my ~/.m2 it does work on the first machine, too.
@shafsongithub I have pushed sample Spring boot with Swagger application. If you want you can use that. https://github.com/raviksoni25/swagger-sample.git
I found the really reason, because of @ComponentScan, and u must to annotate it , for example: //@ComponentScan(basePackages = {“com.haizhi.dao”,“com.haizhi.controll”,“com.haizhi.service”})
and then , it will be ok, u can open the swagger-tui.html and /swagger-resources/configuration/ui
@dapregala It sure does support shorthand request mappings
https://github.com/springfox/springfox/blob/5c8fa1b5fdd91df64c2f9fdebf25e3d1ba45192b/swagger-contract-tests/src/test/resources/contract/swagger2/declaration-bugs-service.json#L782-L821
Also you should be using 2.8.0 which is the latest release at this point in time.
For anyone still encountering this problem, I tried almost everything from above but still can’t fix it. So, this is how I resolved mine.
Root cause for my case: swagger-ui doesn’t show up because it doesn’t recognize any of my APIs.
I don’t know the way how it recognizes APIs but I’m 100% sure that it doesn’t recognize the controllers and methods I annotated with Spring shorthand annotations, annotations such as
@RestController,@PostMapping,@GetMapping, etc.When I changed it to the traditional
@RequestMapping(value = "/foo", method = RequestMethod.POST), the API is now recognized and swagger-ui now shows.Versions used:
springfox-swagger2:2.7.0springfox-swagger-ui:2.7.0spring-boot:2.0.0.RELEASEHope this helps anyone.
@ndtreviv You saved my day! My configuration is all right, but I just can’t see swagger-ui.html and it always returns “Method not allowed”. After reading your post, I checked all my controllers and find one with
to handle all OPTIONS request. I deleted this controller and it just works!
In my case the issues was with version number. Changed from compile ‘io.springfox:springfox-swagger-ui:2.4.0’ to compile ‘io.springfox:springfox-swagger-ui:2.6.1’ in build.gradle file.
Unbelievable, clearing …m2 folder and rebuilding helped.