8

I'm pretty much new to Swagger and I started Documenting very simple my web service that I've build using Spring Boot.

The problem is, After I configure swagger, in the browser when I type localhost:8080/swagger-ui.html I get this following screen with some weird popup message that says "Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway".
I do know it may seems repeated question, but I couldn't resolve this at all with all those answers given. Following, I've posted the screenshot and complete code where I didn't what went wrong. Please make me understand If I went wrong.

Screen shot:enter image description here

Code
SwaggerConfig.java

package com.test.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import static springfox.documentation.builders.PathSelectors.regex;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .paths(regex("/greet.*"))
                .build();
    }
}

TestApplication.java

package com.test.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages="com.test.controllers") 
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
}

TestController.java

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/greet")
public class TestController {

    @RequestMapping
        public String getGreeting() {
        return "Hello There";
    }
}

In the above code both SwaggerConfig.java and TestApplication.java belongs to same package i.e com.test.config and TestController.java belongs to com.test.controllers

This is all the code I've and in the pom.xml I've two following dependencies

<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>
Kishore Kumar Korada
  • 1,204
  • 6
  • 22
  • 47
  • It works for me. Sometimes these things pop up when you turn on some browser extensions. Try with incognito window or some other browser – Barath Oct 02 '17 at 06:24
  • In some browsers like Mozilla, There is no alert screen getting displayed, but still Documentation isn't appearing at all. In the TestController, for the getGreeting() method, A documentation should be visible right? – Kishore Kumar Korada Oct 02 '17 at 06:36
  • @Barath Are you sure, you are able to see the docs? – Kishore Kumar Korada Oct 02 '17 at 06:36
  • yes I was able to see the swagger docs with your configuration. I faced similar issue earlier, it is due to browser, try clearing your cache. – Barath Oct 02 '17 at 06:46
  • double check your @ComponentScan it looks like your SwaggerConfig doesnt get scanned at all. Add a sysout and check it out. Because com.test.controllers is getting scanned and com.test.config is under different pkg. – Barath Oct 02 '17 at 06:47

6 Answers6

6

I solved it adding the annotation @EnableSwagger2 on Application class.

@SpringBootApplication
@EnableSwagger2 //this
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
6

I was facing the same issue when deploying the Spring-boot application as a war artifact. While running the application with the embedded tomcat(as an standalone spring-boot app) it was working fine where as while deploying the war file to remote tomcat facing the above issue.

On bit of dig around, I found that I was missing the Servlet Initializer for my application for the war archive.

Following worked for me like a charm.

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

   @Override
   protected SpringApplicationBuilder configure(SpringApplicationBuilder application){ 
   return application.sources(TestApplication.class); 
   }
}
Amit Agarwal
  • 248
  • 3
  • 7
1

if Swagger is behind any auth, you need to do following in SpringBoot Security

http.authorizeRequests().antMatchers("/swagger-resources/**").permitAll().anyRequest().fullyAuthenticated();

Then you access Swagger UI Documentation

Anand
  • 1,845
  • 2
  • 20
  • 25
1

just add these three URLs in your configure method of ResourceServerConfiguration class "/swagger-ui" , "/swagger-ui.html" and "/swagger-resources/**". it can resolve your problem.

Akash jain
  • 11
  • 1
0

hmmm, try changing .paths to .apis(RequestHandlerSelectors.basePackage("package where your controller is com.demo.example.controller")).build(); give this a shot and if it doesn't work please let me know.

SkiiNet
  • 67
  • 1
  • 8
  • No. Just In browser, I'm not able to view the documentation. To this url "http://localhost:8080/swagger-ui.html", It's just displaying the popup as it exactly showing in the screen shot that I've specified in the question. – Kishore Kumar Korada Oct 02 '17 at 06:26
  • sorry, so you are trying to access the documentation through localhost:8080/greet/swagger-ui.html or just localhost:8080/swagger-ui.html? – SkiiNet Oct 02 '17 at 06:54
  • try accessing the greet one, i think that should be the actual path of your service – SkiiNet Oct 02 '17 at 12:58
0

In my case this bean configuration was a problem maker.

@Bean
public CsvMapper csvSerializerMapper() {
    stuff()
}

Spring used it to initialize jackson objectMapper for Spring, which was failing silently. Fixed it by adding bean with object mapper.

@Autowired
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
    return builder.createXmlMapper(false).build();
}
Elas
  • 212
  • 2
  • 16