Springboot配置Swagger展示API文档并进行接口测试(doc.html、swagger-ui.html)
一、pom.xml中添加相关的依赖项
<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.github.xiaoymin</groupId><artifactId>swagger-bootstrap-ui</artifactId><version>1.9.6</version></dependency>
二、创建一个Swagger配置类
packagecom.example.demo.config;importlombok.extern.slf4j.Slf4j;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;importorg.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;importorg.springframework.web.servlet.config.annotation.WebMvcConfigurer;importspringfox.documentation.builders.ApiInfoBuilder;importspringfox.documentation.builders.ParameterBuilder;importspringfox.documentation.builders.PathSelectors;importspringfox.documentation.builders.RequestHandlerSelectors;importspringfox.documentation.schema.ModelRef;importspringfox.documentation.service.ApiInfo;importspringfox.documentation.service.Parameter;importspringfox.documentation.spi.DocumentationType;importspringfox.documentation.spring.web.plugins.Docket;importspringfox.documentation.swagger2.annotations.EnableSwagger2;importjava.util.ArrayList;importjava.util.List;@Configuration@EnableSwagger2@Slf4jpublicclassSwaggerConfigextendsWebMvcConfigurationSupport{@OverridepublicvoidaddResourceHandlers(ResourceHandlerRegistry registry){
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");@BeanpublicDocketcreateRestApi(){returnnewDocket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().paths(PathSelectors.any()).build();}privateApiInfoapiInfo(){returnnewApiInfoBuilder().title("swagger2在线文档测试").description("系统描述").termsOfServiceUrl("服务URL").contact("作者").version("1.0").build();}}
三、创建一个测试接口
packagecom.example.demo.controller;importio.swagger.annotations.Api;importio.swagger.annotations.ApiOperation;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/api")@Api(tags ="你好")publicclassSampleController{@ApiOperation("你好")@GetMapping("/hello")publicStringsayHello(){return"你好";}}
四、访问测试
http://localhost:8080/doc.html
http://localhost:8080/swagger-ui.html
本文转载自: https://blog.csdn.net/LSW1737554365/article/details/131478434
版权归原作者 信息化战略 所有, 如有侵权,请联系我们删除。
版权归原作者 信息化战略 所有, 如有侵权,请联系我们删除。