Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api_docs shows content but swagger-ui (2.6.1) is empty #1615

Closed
StefanSchubert opened this issue Dec 20, 2016 · 42 comments
Closed

api_docs shows content but swagger-ui (2.6.1) is empty #1615

StefanSchubert opened this issue Dec 20, 2016 · 42 comments
Assignees
Labels
Milestone

Comments

@StefanSchubert
Copy link

StefanSchubert commented Dec 20, 2016

Hi this is a question on configuration. I was reading for hours today but did not get my mistake.

Symptom:

http://localhost:8080/sabi/api/v2/api-docs/
shows me that the annotations have been scanned and assembled. I get a detailed json containing my content.

However when calling the swagger-ui
http://localhost:8080/sabi/swagger-ui.html
The ui renders, but I can see non of my API. Even by pasting the api-docs url from above and pressing enter did not lead to any result.

Here's my setup:

Im using:

           <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.6.1</version>
                <scope>compile</scope>
            </dependency>

            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.6.1</version>
                <scope>compile</scope>
            </dependency>

with spring-webmvc (4.2.1-RELEASE) and using xml-free annotation based config:

@Configuration
@EnableWebMvc
@EnableSwagger2
@ComponentScan(basePackages = "de.bluewhale.sabi")
@PropertySource("classpath:server.properties")
public class AppConfig {

    @Autowired
    Environment env;

    @Bean
    public EncryptionService encryptionService() {
        return new EncryptionService(env.getProperty("accessToken.salt"), env.getProperty("accessToken.password"));
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer properties() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

and


@Configuration
public class ApiDocumentationConfiguration {

    @Bean
    public Docket documentation() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                    .apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.any())
                    // .paths(regex("/api/*"))
                    .build()
                .pathMapping("/api")
                .apiInfo(metadata());
    }

    private ApiInfo metadata() {
        return new ApiInfoBuilder()
                .title("sabi's REST API documentation")
                .description("see https://github.com/StefanSchubert/sabi")
                .version("1.0")
                .license("MIT Licence (MIT)")
                .licenseUrl("https://github.com/StefanSchubert/sabi/blob/master/LICENSE")
                .contact("Stefan.Schubert@bluewhale.de")
                .build();
    }
}

What am I doing wrong?

@dilipkrish
Copy link
Member

Could you try seeing the javascript console to see why its not loading the UI?

@dilipkrish dilipkrish added this to the 2.7.0 milestone Dec 29, 2016
@StefanSchubert
Copy link
Author

StefanSchubert commented Jan 1, 2017

When hitting enter I can see an HTTP-404 on the developer console of firefox:
http://localhost:8080/sabi/swagger-resources/configuration/ui

with this information I found: #983

and tried to access: http://localhost:8080/sabi/api/swagger-resources/configuration/ui
which results in some JSON and a HTTP-200.

@dilipkrish
Copy link
Member

In that case try accessing the swagger-ui here http://localhost:8080/sabi/api/swagger-ui.html

@StefanSchubert
Copy link
Author

StefanSchubert commented Jan 2, 2017 via email

@dilipkrish
Copy link
Member

Try adding a pathMapping("/api") to your docket

@StefanSchubert
Copy link
Author

Hm... the docket already has this mapping as you can see in the 3rd code snippet of my first posting.

If it helps:

I have configured another module with swagger with the same setup, but which can be taken to reproduce without the need of setting up a database. So here are the steps to reproduce:

  1. clone https://github.com/StefanSchubert/sabi.git
  2. Go to the captcha subdir and build with maven install
  3. deploy the build war file into a tomcat (using my IDE for this I set the root context to "robot"

Now you can see:

http://localhost:8080/robot/swagger-ui.html
shows the GUI without content, but seems to ignore dockets path mapping of "api" (provided in class "configs/ApiDocumentationConfiguration.java"

while
http://localhost:8080/robot/api/v2/api-docs/
as well as
http://localhost:8080/robot/api/swagger-resources/configuration/ui

recognize the pathMapping and provide content.

what am I doing wrong?

Notice: the dispatcher servlet (see configs/WebAppInitalizer.java) is also configured for the path "api". You may check the dispatcher is working by probing, i.e.:
http://localhost:8080/robot/api/captcha/challenge

I think that the dispatcher may interfere with the view resolver I added for swagger
(see configs/RESTConfiguration.java) because as soon as I provide a different path mapping for the dispatcher all swagger URIs which contain the /api are no longer working.

I'm wondering if I'm so far from standard, with providing swagger doc to a simple rest application.
If this is not working, what would have been the "proper" setup?

@Kusig
Copy link

Kusig commented Feb 1, 2017

Any info about this one? I've got exactly the same problem on a more or less simple Sprint Boot 1.4.1 application.

api-docs is generated well but the call from swagger to /swagger-resources fails with 500

http://localhost:8080/swagger-resources 500

Strange enough, if I exclude the SwaggerConfig from the basebackage scan of Spring, the swagger view is shown well but of course without taking the configuration in account.

@configuration
public class SwaggerConfig
{
@bean
public Docket api()
{
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.regex("/io.*"))
.build();
}
}

Summary:

  1. If the SwaggerConfig is part of the Spring ComponentScan, the api-doc is generated but the swagger-resource fetch doesnt work.

  2. If the SwaggerConfig is excluded from the Spring ComponentScan, the api-doc is generated as well (all apis are then included as without any configuration) and the swagger resource fetch on it is working.

Therefore, the generation of the interface spec works always but only the access via Swagger is somehow destroyed if the config is part of the Spring component scan ....

@Kusig
Copy link

Kusig commented Feb 1, 2017

One step further:
I have as well JavaMelody dependency on the project. Somehow this conflicts with sprinfox. As soon as I remove the following interceptor from the JavaMelody configuration, springfox works as expected.

@bean
public MonitoringSpringAdvisor springControllerMonitoringAdvisor() {
final MonitoringSpringAdvisor interceptor = new MonitoringSpringAdvisor();
interceptor.setPointcut(new AnnotationMatchingPointcut(Controller.class));
return interceptor;
}

Surfing more around, I think this is similar as this problem here.

#1328

Any idea how this could be fixed or any workaround?

@dilipkrish
Copy link
Member

Are you getting a null pointer too? Really not sure whats going on.

@dilipkrish
Copy link
Member

perhaps because its not a public controller?

@Kusig
Copy link

Kusig commented Feb 5, 2017

I thin the exception is the same as in #1328 and the symptoms as well excatly as this issue here:

java.lang.NullPointerException: null
at springfox.documentation.swagger.web.ApiResourceController.swaggerResources(ApiResourceController.java:67) ~[springfox-swagger-common-2.6.1.jar:2.6.1]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_91]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_91]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_91]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_91]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114) ~[spring-webmvc-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) ~[spring-webmvc-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) ~[spring-webmvc-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) ~[spring-webmvc-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.5.jar:8.5.5]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55) ~[spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:201) ~[javamelody-core-1.60.0.jar:1.60.0]
at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:178) ~[javamelody-core-1.60.0.jar:1.60.0]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:105) ~[spring-boot-actuator-1.4.1.RELEASE.jar:1.4.1.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:89) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:107) ~[spring-boot-actuator-1.4.1.RELEASE.jar:1.4.1.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108) [tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) [tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349) [tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:784) [tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:802) [tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1410) [tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.5.jar:8.5.5]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_91]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_91]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.5.jar:8.5.5]
at java.lang.Thread.run(Unknown Source) [na:1.8.0_91]

@Kusig
Copy link

Kusig commented Feb 5, 2017

I don't understand the question about the public controller above really, all controllers are public I think?

The pointcut defined in JavaMelody looks straight forward. The only speciality is that it sets itself to the top position (setOrder).

public class MonitoringSpringAdvisor extends DefaultPointcutAdvisor {
private static final long serialVersionUID = 1241977525834332907L;

/**
 * Constructeur.
 */
public MonitoringSpringAdvisor() {
	super();
	setAdvice(new MonitoringSpringInterceptor());
	// ordre par rapport aux autres advisors/aspects (issue 32)
	setOrder(0);
}

}

If this Advisor is executed then springfox throws the null ptr exception.

@dilipkrish
Copy link
Member

@Kusig in particular I was referring to ApiResourceController.swaggerResources not being public.

@Kusig
Copy link

Kusig commented Feb 12, 2017

The only thing which could be null on this line 67 of ApiResourceController is swaggerResources I think. What would mean that it isn't injected before properly with the constructor of the ApiResourceController.

All this happens only if there is registered from another library (javamelody) the mentioned Interceptor above. As seen on other threads, there was as well the problem once such a similar interceptor was installed.

I just am unsure about the swaggerResources beeing injected with the Constructor. Why not having it injected independent of the Constructor?

@Kusig
Copy link

Kusig commented Feb 12, 2017

Some debugging proofed what I wrote before. The swaggerResources is null if such a interceptor is registered which causes Springs cglib stuff to handle this.
The constructor is called with valid values, however, it looks as if the Interceptor handler deals with another instance of this class somehow where this variable isn't assigned.
Probably it would be wise to have a default constructor for this class and to inject the swaggerResources "normally" and not in the constructor. Looks as Spring has problems with this if there is no default construtor around or it finds strangely another one in this case.

Might as well be a sideeffect of the variable beeing defined as final. Is this really needed?

@dilipkrish
Copy link
Member

@StefanSchubert Thanks for reporting. The demo repo was helpful in finding the problem.

@ghost ghost assigned dilipkrish Mar 8, 2017
@ghost ghost removed the in progress label Mar 8, 2017
@dilipkrish
Copy link
Member

@StefanSchubert Hope this helps!
swagger_ui

@StefanSchubert
Copy link
Author

Long time not worked on my project. I will check this out this week. Do I have to switch to another swagger version?

@dilipkrish
Copy link
Member

You'd just have to try the 2.7.0-SNAPSHOT. See the docs on how to set that up

@StefanSchubert
Copy link
Author

Tried to fetch the 2.7.0 Snapshot but maven couldn't resolve it.
I looked at mavencentral: https://mvnrepository.com/artifact/io.springfox/springfox-swagger2
were 2.6.1 is the latest.
Do I need to add another maven repository for the snapshots?

@florianrusch
Copy link
Contributor

You can find the documentation for 2.7.0 under this link: https://springfox.github.io/springfox/docs/snapshot/#snapshot-3

@StefanSchubert
Copy link
Author

StefanSchubert commented Mar 29, 2017

Hm, OK I added the snapshop maven repro. Could compile and used the configuration as shown on the docu link above.

All mentions URLS by spring-boot are accessable:
2017-03-29 22:26:58.379 INFO 2874 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/v2/api-docs],methods=[GET],produces=[application/json || application/hal+json]}" onto public org.springframework.http.ResponseEntity<springfox.documentation.spring.web.json.Json> springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest) 2017-03-29 22:26:58.382 INFO 2874 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/swagger-resources]}" onto public org.springframework.http.ResponseEntity<java.util.List<springfox.documentation.swagger.web.SwaggerResource>> springfox.documentation.swagger.web.ApiResourceController.swaggerResources() 2017-03-29 22:26:58.382 INFO 2874 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/swagger-resources/configuration/ui]}" onto public org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.UiConfiguration> springfox.documentation.swagger.web.ApiResourceController.uiConfiguration() 2017-03-29 22:26:58.382 INFO 2874 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/swagger-resources/configuration/security]}" onto public org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.SecurityConfiguration> springfox.documentation.swagger.web.ApiResourceController.securityConfiguration() 2017-03-29 22:26:58.386 INFO 2874 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2017-03-29 22:26:58.386 INFO 2874 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet

However I cannot access /swagger-ui.html which results in a 404

It's not listed above but should be, or?

I used the following maven deps for swagger:

`
io.springfox
springfox-swagger-ui
compile

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>spring-beans</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
            <exclusion>
                <artifactId>spring-context</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
            <exclusion>
                <artifactId>spring-aop</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
            <exclusion>
                <artifactId>slf4j-api</artifactId>
                <groupId>org.slf4j</groupId>
            </exclusion>
            <exclusion>
                <artifactId>jackson-annotations</artifactId>
                <groupId>com.fasterxml.jackson.core</groupId>
            </exclusion>
        </exclusions>
    </dependency>

`

@StefanSchubert
Copy link
Author

Finally it's working for me too.
I needed to throw "api" as pathMapping of the docket config:

    @Bean
    public Docket documentation() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                    .apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.any())
                    // .paths(regex("/api/*"))
                    .build()
                .pathMapping("/")
                .apiInfo(metadata());
    }

Also I have api as part of the base path of my controllers:

@RestController
@RequestMapping(value = "api/user")
public class AuthenticationController {
...
}

But the most important thing to do was this in my app-config:

@Configuration
// @EnableWebMvc  IF ENABLED SWAGGER-UI WON'T WORK
@EnableSwagger2
@ComponentScan(basePackages = "de.bluewhale.sabi")
@PropertySource("classpath:application.properties")
public class AppConfig { 

@chrisinmtown
Copy link

chrisinmtown commented Jul 19, 2017

Our Spring (but not spring-boot) app uses the @ annotation EnableWebMvc. To make swagger work we add a config class:

@Configuration
@EnableWebMvc
public class SwaggerMVCConfig extends WebMvcConfigurerAdapter {

    @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/");
    }

}

@sinboun
Copy link

sinboun commented Aug 23, 2017

image

io.springfox springfox-swagger2 2.7.0 io.springfox springfox-swagger-ui 2.7.0

but response is empty!

@dilipkrish
Copy link
Member

@TiltCitySong may be refresh your browser cache

@chiyanram
Copy link

chiyanram commented Sep 25, 2017

@dilipkrish Hi , i am also facing the same problem(/v2/api-docs shows all the json but swagger-ui.html is empty) . Project is Spring MVC (4.3.3.RELEASE) swagger(2.7.0) .

My Docket Configuration is,

@configuration
@EnableSwagger2
public class TravelSwaggerConfig {

@Bean
public Docket swaggerApi() {

    return
        new Docket(DocumentationType.SWAGGER_2)
            .select()
            .paths(PathSelectors.any())
            .apis(RequestHandlerSelectors.withClassAnnotation(TravelApi.class))
            .build();
}

}

My Spring configuration is,

@OverRide
public void addResourceHandlers(final ResourceHandlerRegistry registry) {

    registry.addResourceHandler("swagger-ui.html")
        .addResourceLocations("classpath:/META-INF/resources/");

    registry.addResourceHandler("/webjars/**")
        .addResourceLocations("classpath:/META-INF/resources/webjars/");

}

@OverRide
public void configureDefaultServletHandling(final DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}

http://localhost:8080/travel/swagger-resources/configuration/ui gives :
{"docExpansion":"none","apisSorter":"alpha","defaultModelRendering":"schema","supportedSubmitMethods":["get","post","put","delete","patch"],"jsonEditor":false,"showRequestHeaders":true}

http://localhost:8080/travel/swagger-resources:

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

Maven:

io.springfox springfox-swagger-ui 2.7.0 io.springfox springfox-swagger2 2.7.0

web.xml:

travel /

Please let me now the issue.

Thanks,
Ram

@srikanthps
Copy link

I ran into similar situation - looks like 2.7.0 has some issues, I had to use 2.6.1 for both swagger-ui and swagger2.

In gradle,

compile("io.springfox:springfox-swagger2:2.6.1")
compile("io.springfox:springfox-swagger-ui:2.6.1")

@dilipkrish
Copy link
Member

@srikanthps please try with 2.7.0

@mneverov
Copy link

mneverov commented Nov 2, 2017

TL;DR Clear browser cache

I had the same situation (spring-boot 1.5.8.RELEASE, springfox-swagger-ui 2.7.0).
Opened the same page http://localhost:8080/swagger-ui.html in firefox - and it worked.
Console in chrome shows that there is an error:

"Template was precompiled with an older version of Handlebars than the current runtime. Please update your precompiler to a newer version (>= 4.0.0) or downgrade your runtime to an older version (>= 2.0.0-beta.1)."

Error at Object.<anonymous> (http://localhost:8080/webjars/springfox-swagger-ui/lib/handlebars-4.0.5.js:527:24) at __webpack_require__ (http://localhost:8080/webjars/springfox-swagger-ui/lib/handlebars-4.0.5.js:56:30) at Object.exports.__esModule (http://localhost:8080/webjars/springfox-swagger-ui/lib/handlebars-4.0.5.js:266:19) at __webpack_require__ (http://localhost:8080/webjars/springfox-swagger-ui/lib/handlebars-4.0.5.js:56:30) at Object.exports.default.newObj (http://localhost:8080/webjars/springfox-swagger-ui/lib/handlebars-4.0.5.js:173:24) at __webpack_require__ (http://localhost:8080/webjars/springfox-swagger-ui/lib/handlebars-4.0.5.js:56:30) at Object.exports.default.obj.__esModule.default (http://localhost:8080/webjars/springfox-swagger-ui/lib/handlebars-4.0.5.js:89:27) at __webpack_require__ (http://localhost:8080/webjars/springfox-swagger-ui/lib/handlebars-4.0.5.js:56:30) at http://localhost:8080/webjars/springfox-swagger-ui/lib/handlebars-4.0.5.js:76:18 at http://localhost:8080/webjars/springfox-swagger-ui/lib/handlebars-4.0.5.js:79:10

@yogeshmsharma
Copy link

yogeshmsharma commented Feb 5, 2018

What is correct fix for this? I do get similar error. Updated to 2.8 but problem is not solved

@dilipkrish
Copy link
Member

Error similar to what? @yogeshmsharma

@yogeshmsharma
Copy link

Unnable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway. The base url is the root of where all the swagger resources are served. For e.g. if the api is available at http://example.org/api/v2/api-docs then the base url is http://example.org/api/.

@dilipkrish
Copy link
Member

You need to add docket.pathMapping('/api') to account for the dynamic servlet

@yogeshmsharma
Copy link

Dilip Thanks for reply but it didn't worked
also I have no spring security in place

@bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.pathMapping("/api")
.apiInfo(apiInfo());

@dilipkrish
Copy link
Member

Are u using an API Gateway?

@yogeshmsharma
Copy link

No, it is simple Spring application

@yogeshmsharma
Copy link

I have also provided mapping in spring context xml

<resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
<resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>
<resources mapping="/configuration/**" location="classpath:/META-INF/resources/webjars/"/>
<resources mapping="/configuration/ui/**" location="classpath:/META-INF/resources/webjars/"/>

@StefanSchubert
Copy link
Author

StefanSchubert commented Feb 6, 2018

Hm...it's a while ago. but just in case you wish to compare the config setting, as meanwhile my setup is working, you may have a look at here: https://github.com/StefanSchubert/sabi
Hope it helps ;-)

Ah and reviewing the comments, see my posting from 16 Jun 2017 here? This was what finally helped in my case.

@yogeshmsharma
Copy link

Stefan thanks for your code base, actually its working for one version of java config where I have used annotation. But it is not working for XML based spring configuration for spring 5.0 .
I need to re-write complete configuration to check what is wrong

@cpinkerton
Copy link

In case this help anyone - I encountered this problem after refactoring a Spring Boot project to deploy a WAR to a standalone Tomcat (rather than use the default embedded servlet container). The issue was my @SpringBootApplication class wasn't extending SpringBootServletInitializer

@AdamDudley
Copy link

Content Security Policy - Just in case this helps, I had this. Check your CSP, I disabled 'unsafe-eval' or similar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests