Getting unexpected results when configuring Swagger using Spring Boot

I have a lot of new things for Swagger, and I began to document a very simple my web service that I created using Spring Boot.

The problem is that after setting up swagger in the browser, when I type localhost: 8080 / swagger-ui.html , I get the following screen with some kind of weird pop-up message that says: β€œUnable to infer the base url. This is often occurs when using dynamic servlet registration or when the API is behind an API gateway. " I know this may seem like a repeated question, but I could not solve it at all with all these answers. Following this, I posted a screenshot and full code where I did nothing wrong. Please help me understand if I did wrong.

Screenshot: enter image description here

the 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, SwaggerConfig.java and TestApplication.java own the same package, i.e. com.test.config and TestController.java belongs to com.test.controllers

This is all the code I have, and in pom.xml I have the following two 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> 
+7
spring spring-boot swagger swagger-ui
source share
4 answers

I ran into the same problem when deploying a Spring-boot application as an artifact of war. When launching an application with a built-in tomcat (as a stand-alone Spring-boot application), it worked great where, when deploying a war file for a remote tomcat, facing the above problem.

At that time, I found that for my application for the military archive, I lacked a servlet initializer.

After me they 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); } } 
+4
source share

I decided that adding the @ EnableSwagger2 annotation to the Application class.

 @SpringBootApplication @EnableSwagger2 //this public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 
+3
source share

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

0
source share

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

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

Then you get access to the Swagger UI Documentation

0
source share

All Articles