Springboot swagger3 update configuration details

FHAdmin 2022-06-24 07:38:16 阅读数:893

springbootswagger3swaggerupdateconfiguration

1. Introduce dependencies , edition 3.0.0 Just introduce one

 <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>

2. Configuration class SwaggerConfig

package org.fh.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
/**
* explain :Swagger Interface API Generate
* author :FH Admin
* from fhadmin.cn
*/
@Configuration
@EnableOpenApi
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("org.fh.controller")) // For the current package path
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("FH Admin Swagger3 RESTful API") // The page title
.version("3.0") // Version number
.description("fhadmin.org") // describe
.build();
}
}

3.Swagger Intercept configuration

package org.fh.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* explain :Swagger Intercept configuration
* author :FH Admin
* from fhadmin.cn
*/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.
addResourceHandler("/swagger-ui/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
.resourceChain(false);
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/swagger-ui/")
.setViewName("forward:/swagger-ui/index.html");
}
}

4. visit 127.0.0.1:8081/swagger-ui/index.html

5. Interface description case

 Annotate the processing class , such as
@Api(" User registration login interface ")
Annotate the method , such as
@ApiOperation(value = " Sign in ", notes=" Verify whether the login is successful ")
@ApiImplicitParam(name = "KEYDATA", value = " User name password confusion code combination ", paramType = "query", required = true, dataType = "String")
copyright:author[FHAdmin],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/175/20210630154917221r.html