In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to configure Swagger2 and how to solve the problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
Swagger2 configuration
Swagger2 is configured in the spring boot project, but access to the injection method in 404 Swagger Config is also performed or the page is not accessible. The reason is that MVC did not find the swagger-ui.html file in the swagger-ui package.
The configuration steps for Swagger2 are as follows:
I. introduction of dependency
Pom.wml
Io.springfox springfox-swagger2 2.9.2 io.springfox springfox-swagger-ui 2.9.2. Write the configuration file package io.github.talelin.latticy.config;import com.google.common.base.Function;import com.google.common.base.Optional;import com.google.common.base.Predicate;import org.springframework.context.annotation.Bean Import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import springfox.documentation.RequestHandler;import springfox.documentation.builders.ApiInfoBuilder;import springfox.documentation.builders.PathSelectors;import springfox.documentation.service.ApiInfo;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;import springfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration@EnableSwagger2public class SwaggerConfig {/ / defines the delimiter private static final String splitor = ";" @ Bean Docket docket () {System.out.println ("Swagger===") Return new Docket (DocumentationType.SWAGGER_2) .apiInfo (apiInfo ()) .select () .apis (basePackage ("io.github.talelin.latticy.controller.v1")) / / here packet scanning is used to determine the interface / / .apis (RequestHandlerSelectors.withMethodAnnotation (ApiOperation.class) to be displayed. )) / / the interface to be displayed is determined by including annotations. Interfaces (PathSelectors.any ()) .build () } private ApiInfo apiInfo () {return new ApiInfoBuilder () .title ("CMS") .description ("e-commerce Mini Program CMS Api document") .termsOfServiceUrl ("https://blog.csdn.net/xfx_1994") .version (" 1.0") .build () } public static Predicate basePackage (final String basePackage) {return input-> declaringClass (input) .transform (handlerPackage (basePackage)) .or (true);} private static Function > declaringClass (RequestHandler input) {return Optional.fromNullable (input.declaringClass ());}}
Now that the configuration is complete, start the project to visit http://localhost: port/ {port} / port/ {context-path} / swagger-ui.html
If the access is successful, you do not need to continue with the following configuration. If there is a 404 error when the access fails, do the following configuration.
Third, solve the error report package io.github.talelin.latticy.config;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configurationpublic class WebMvcConfig implements WebMvcConfigurer {@ Override public void addResourceHandlers (ResourceHandlerRegistry registry) {registry.addResourceHandler ("/ *") .addResourceLocations ("classpath:/static/") Registry.addResourceHandler ("swagger-ui.html") .addResourceLocations ("classpath:/META-INF/resources/"); registry.addResourceHandler ("/ webjars/**") .addResourceLocations ("classpath:/META-INF/resources/webjars/");}}
The principle is to help MVC find the files corresponding to swagger-ui.html and its CSS,JS.
There are still 404 problems after swagger is configured.
Record the problems encountered in learning spring boot
Swagger2
@ Overridepublic void addResourceHandlers (ResourceHandlerRegistry registry) {registry.addResourceHandler ("swagger-ui.html") .addResourceLocations ("classpath:/META-INF/resources/"); registry.addResourceHandler ("/ webjars/**") .addResourceLocations ("classpath:/META-INF/resources/webjars/");}
Swagger still 404 after adding this configuration
1. It is possible that some other class implements WebMvcConfigurer or inherits WebMvcConfigurationSupport
The resulting WebMvcConfigurationSupport does not override the addResourceHandlers when inheriting
There are three 2.spring boot startup modes. If there is no change by default, it should be SERVLET.
NONE
SERVLET
REACTIVE
Notice that only SERVLET loads the webmvc configuration
This is the answer to the problem of how to configure Swagger2 and how to solve the error problem. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.