Tag Archives: Swagger

[Solved] Swagger Error: Failed to start bean ‘documentationPluginsBootstrapper’

Error cause: it may be caused by version mismatch. My spring boot is 2.6.0, but the spring cloud version needs to be compared in this way and cannot be degraded, so it is configured in this way

POM dependency

 <parent>
	 <groupId>org.springframework.boot</groupId>
	 <artifactId>spring-boot-starter-parent</artifactId>
	 <version>2.6.0</version>
	 <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
	<dependency>
		<groupId>io.springfox</groupId>
		<artifactId>springfox-swagger2</artifactId>
		<version>2.9.2</version>
	</dependency>
	<dependency>
		<groupId>io.springfox</groupId>
		<artifactId>springfox-swagger-ui</artifactId>
		<version>2.9.2</version>
	</dependency>
	<dependency>
		<groupId>com.google.guava</groupId>
		<artifactId>guava</artifactId>
		<version>25.1-jre</version>
	</dependency>
</dependencies>

Startup class:

@SpringBootApplication
@EnableDiscoveryClient
@EnableWebMvc // Comment out
public class UploadServiceApplication {

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

}

This configuration may cause the UI page of swagger to fail to open, so the following configuration is required:

import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {

    /**
     * Accessing static resources
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /**
         * SpringBoot autoconfiguration itself does not map the /swagger-ui.html
         * This path is mapped to the corresponding directory META-INF/resources/
         * Use WebMvcConfigurerAdapter to publish the static files of swagger;
         */
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        //Map all /static/** accesses to the classpath:/static/ directory
        registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX +"/static/");
        super.addResourceHandlers(registry);
    }
}

[Solved] Springboot integrate swagger Error: failed to start bean ‘documentationpluginsboot

First, paste my jar package version

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/>
    </parent>


 <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>25.1-jre</version>
        </dependency>

Some people failed to start the bean ‘documentationpluginsboot’ because the guava version is too low. The online strategy is also to modify the guava version

Finally, it is found that the springboot version is too high and incompatible. Just change the springboot version to 2.2.5

An error is reported when swagger is used: failed to load API definition

NuGet add Swashbuckle.AspNetCore, add and enable middleware Swagger in Startup.cs

public void ConfigureServices(IServiceCollection services)
        {
            #region Swagger
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info
                {
                    Version = "v1.0.0",
                    Title = "My Web API",
                    Description = " Description Document " ,
                    TermsOfService = "None",
                    Contact = new Swashbuckle.AspNetCore.Swagger.Contact { Name = "My.Web", Email = "[email protected]", Url = "https://www.cnblogs.com/Zev_Fung/" }
                });
            });
            #endregion
        }
       
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            #region Swagger
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My Web API V1");
            });
            #endregion 
        }

Use the built-in Kestrel web debugging, enter the address: http://localhost:<port>/swagger, jump to https://localhost:<port>/swagger/index.html by default

 

Swagger prompts an error:

Failed to load API definition.
Errors
Fetch errorInternal Server Error /swagger/v1/swagger.json

Open http://localhost:<port>/swagger/v1/swagger.json, prompt error

An unhandled exception occurred while processing the request.
NotSupportedException: Ambiguous HTTP method for action - xxxxx.Controllers.BooksController.Post (xxxxx). Actions require an explicit HttpMethod binding for Swagger 2.0
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreatePathItem(IEnumerable<ApiDescription> apiDescriptions, ISchemaRegistry schemaRegistry)

Stack Query Cookies Headers
NotSupportedException: Ambiguous HTTP method for action - xxxxx.Controllers.BooksController.Post (xxxxx). Actions require an explicit HttpMethod binding for Swagger 2.0
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreatePathItem(IEnumerable<ApiDescription> apiDescriptions, ISchemaRegistry schemaRegistry)
System.Linq.Enumerable.ToDictionary<TSource, TKey, TElement>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreatePathItems(IEnumerable<ApiDescription> apiDescriptions, ISchemaRegistry schemaRegistry)
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwagger(string documentName, string host, string basePath, string[] schemes)
Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Show raw exception details

 

 

Or check the output error in the console

 

 

Probably means: Unsupported exception: The HTTP method of the operation is not clear, and the method needs to specify the request method

Add [HttpGet], [HttpPost], etc. to the method, you can view the API through Swagger UI

 

 

The above is just one of the causes of Fetch error Internal Server Error /swagger/v1/swagger.json, and the others are also solved by viewing the error message