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

Swagger support for Spring Boot 2 / Spring 5? #2155

Closed
springframeworkguru opened this issue Dec 17, 2017 · 23 comments
Closed

Swagger support for Spring Boot 2 / Spring 5? #2155

springframeworkguru opened this issue Dec 17, 2017 · 23 comments
Labels
Milestone

Comments

@springframeworkguru
Copy link

I'm getting a 404 on the Swagger UI url with a Spring Boot 2 (M7) project. Is there any config I need to do for Spring Boot 2 / Spring 5 (Spring MVC. not using Reactive).

Here's my project: https://github.com/springframeworkguru/spring5-mvc-rest/tree/vendor-api

You'll see its a pretty simple project (no Spring Sec).

I added deps for the 2.7.0 version and the following config:

@EnableSwagger2
@Configuration
public class SwaggerConfig {

    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors
                        .basePackage("guru.springframework"))
                .paths(regex("/api/v1/*"))
                .build();
    }
}

The JSON endpoint works fine. Only the Swagger UI endpoint is returning a 404.

Thanks!

@dilipkrish
Copy link
Member

@springframeworkguru spring boot 2 and spring fx 5 aren't really supported as of now. Trying to get the next release (2.8.0) in before starting to work on it. Let me look at your repo and see what the issue might be.

@springframeworkguru
Copy link
Author

Thanks @dilipkrish! Appreciate the help.

I just committed my changes on this branch.

@springframeworkguru
Copy link
Author

@dilipkrish - Got it working. Something broke the config in Spring 5 / Spring Boot. I had to add config for resource handlers.

For others hitting this issue:

Note: WebMvcConfigurerAdapter has been deprecated in Spring 5.

@EnableSwagger2
@Configuration
public class SwaggerConfig extends WebMvcConfigurationSupport {

    public Docket api(){

        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors
                        .basePackage("guru.springframework"))
                .paths(regex("/api/v1/*"))
                .build();

    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

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

@dilipkrish
Copy link
Member

Oh cool! So in your experience everything related to traditional mvc works as expected?

@springframeworkguru
Copy link
Author

I think so. Seems only Swagger UI was broken. With the above config, Swagger UI is looking good.

I'm developing a section on Swagger for my Spring 5 course. So, I'll be getting into the various config options over the next few days. Fingers crossed!

@dilipkrish
Copy link
Member

Thats encouraging! So the new functional endpoints are working as well? I'll update this post when 2.8.0 is released (hoping soon enough!)

@springframeworkguru
Copy link
Author

Have not tested functional endpoints - not yet!

@dilipkrish
Copy link
Member

For a moment there I was excited! sigh!

@Aloren
Copy link

Aloren commented Dec 22, 2017

In our project Swagger 2.7.0 works correctly with Spring Boot 2.0.0.M3 without adding resource handlers.

@springframeworkguru
Copy link
Author

I saw that yesterday - I ran through it again, I wasn't able to recreate it. I reviewed the code changes & could not spot the delta. No idea what I was doing to cause it... I was working with M7. Going to close this issue. Thanks!

@ghost ghost removed the current label Dec 22, 2017
@dilipkrish dilipkrish added this to the 2.8.0 milestone Dec 28, 2017
@armdev
Copy link

armdev commented Mar 2, 2018

Swagger2 does not working with Spring Boot 2 when we add embedded reactive connectors(netty). Because Swagger has dependency from Spring web MVC, and if you exclude tomcat, swagger will fail. Just tested with Spring Boot 2.

@dilipkrish
Copy link
Member

@armdev thanks for letting us know!

@adriens
Copy link

adriens commented Mar 21, 2018

Hi @springframeworkguru , i'm running Spring Boot 2, and trying to make run Swagger 2 on top of it.
Thanks to your tutorial, i'm able to get the json up anr running, when, even with the code you posted in this issue, i'm not able to get the html up, i get the following log telling me th htm is not found :

`2018-03-22 07:05:12.280 WARN 1928 --- [nio-8080-exec-2] o.s.web.servlet.PageNotFound : No mapping found for HTTP request with URI [/v2/api-docs/swagger-ui.html] in DispatcherServlet with name 'dispatcherServlet'

To make short :

http://localhost:8080/v2/api-docs -> OK, looks perfect !
http://localhost:8080/v2/api-docs/swagger-ui.html -> 404

I've used the code provided by @springframework guru in this ticket.

Any idea what i should to to make it up and running please ?...it's so frustrating not to see the html ;-p

Thanks in advance for any help. I'm on the 2.8.0

@adriens
Copy link

adriens commented Mar 21, 2018

I was initially using the following tutorial (bfefore to switch on this issue) : https://springframework.guru/spring-boot-restful-api-documentation-with-swagger-2/

@armdev
Copy link

armdev commented Mar 21, 2018

I pushed project with Spring boot 2 and swagger https://github.com/armdev/springboot2-swagger , but application configured as a "servlet", not a "reactive",
https://github.com/armdev/springboot2-swagger/blob/master/src/main/java/io/project/app/Application.java
but reactive stack is working.

@adriens
Copy link

adriens commented Mar 21, 2018

Thanks a lot @armdev , i'm taking a look at it, i'll let you know as soon as it works 👍

@adriens
Copy link

adriens commented Mar 21, 2018

I've just run your code locally without any problem.

just

git clone https://github.com/armdev/springboot2-swagger.git
cd springboot2-swagger.git
mvn spring-boot:run
firefox localhost:4545/swagger-ui.html

and swagger-ui is up an running. Ill use htis as a tutorial now.

Thanks a lot @armdev for great help.

@adriens
Copy link

adriens commented Mar 29, 2018

@armdev : your code allowed me to make swagger-ui work with only few lines of code on the main app 👍 💯
Thanks a lot !

@AdityaJaiswal7
Copy link

Hi @springframeworkguru/ @armdev, I am running a Spring boot 2.2.0 with swagger version 2.9.2 and in swagger configuration file if I am using @configuration and @EnableSwagger2, getting the following error:


APPLICATION FAILED TO START


Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

springfox.documentation.spring.web.plugins.DocumentationPluginsManager.createContextBuilder(DocumentationPluginsManager.java:152)

The following method did not exist:

org.springframework.plugin.core.PluginRegistry.getPluginFor(Ljava/lang/Object;Lorg/springframework/plugin/core/Plugin;)Lorg/springframework/plugin/core/Plugin;

The method's class, org.springframework.plugin.core.PluginRegistry, is available from the following locations:

jar:file:/Users/SystemName/.m2/repository/org/springframework/plugin/spring-plugin-core/2.0.0.M1/spring-plugin-core-2.0.0.M1.jar!/org/springframework/plugin/core/PluginRegistry.class

It was loaded from the following location:

file:/Users/SystemName/.m2/repository/org/springframework/plugin/spring-plugin-core/2.0.0.M1/spring-plugin-core-2.0.0.M1.jar

Action:

Correct the classpath of your application so that it contains a single, compatible version of org.springframework.plugin.core.PluginRegistry

Any help would be appreciated.
Thanks!

@jaumzors
Copy link

@AdityaJaiswal7 could you please provide a more detailed answer for someone who is new to Java? :)
How would you go about correcting the project's classpath? ty

@dvsingh9
Copy link

dvsingh9 commented Jun 9, 2019

I am using Spring Boot 2.1.5 and swagger 2.9.2 now http://localhost:8080/v2/api-docs is working fine and I am able to get json for my api documentation, where as http://localhost:8080/swagger-ui.html gives me whitelabel page. I added

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

for WebMvcConfigurationSupport.

Any suggestions please

@Splash34
Copy link

I got it working with the following setup:

#pom.xml

      <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.2.RELEASE</version>
		<relativePath />
       </parent>
         <dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-rest</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>	
	        
		<!-- io.springfox setup  -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>3.0.0-SNAPSHOT</version>
		</dependency>
                <dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>3.0.0-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-data-rest</artifactId>
			<version>3.0.0-SNAPSHOT</version>
		</dependency>
	</dependencies>
       <repositories>
		<repository>
			<id>jcenter-snapshots</id>
			<name>jcenter</name>
			<url>http://oss.jfrog.org/artifactory/oss-snapshot-local/</url>
		</repository>
	</repositories>

#SpringFoxConfig.java

@Configuration
@EnableSwagger2WebMvc
@Import(SpringDataRestConfiguration.class)
public class SpringFoxConfig {
	@Bean
	public Docket api() {
		return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
				.paths(PathSelectors.any()).build();
	}
}

#URL:
http://localhost:8080/swagger-ui.html#/

I know it's just a snapshot version at the moment, but for my purpose it's totally fine for now.
Nevertheless, they have to release a new major version.

@rameshkumar2410
Copy link

rameshkumar2410 commented Jun 4, 2020

Hi,

I am using Spring below version

<java.version>1.8</java.version>
<org.springframework.version>5.2.2.RELEASE</org.springframework.version>
<org.springsecurity.version>5.2.1.RELEASE</org.springsecurity.version>

I have added swagger dependency

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

Config class:

package com.merck.midas.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableWebMvc
@componentscan
@EnableSwagger2
@configuration
public class SwaggerConfiguration extends WebMvcConfigurationSupport {
@bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any()).build();
}

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

public void configure(HttpSecurity http) throws Exception {
	http.authorizeRequests().antMatchers("/v2/api-docs", "/swagger-resources",
			"/swagger-resources/configuration/ui", "/swagger-resources/configuration/security").permitAll();
}

}

In API class added

@EnableSwagger2
@controller
@path(value = "/")
@Api(tags = "Demo", description = " DemoAPI Specification")
public class DemoController {

@ApiOperation(value="View filter", notes = "viewFilter")
public final Response viewFilter(){

.. some business logic
}

When i launch my application i get stuck with

Unable 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/. Please enter the location manually:

I tried with http://localhost:8080/v2/api-docs- got 404 error.

Any help would be appreciated.

Have any one tried using spring 5.X version instead of spring boot.

Is swagger support spring 5.2.2

Thanks in advance.

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

10 participants