spring boot集成swagger
程序员文章站
2022-05-03 17:38:47
...
依赖:
<!--swagger依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
swagger配置:
package com.china.great.system.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
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.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
//swagger2的配置文件,在项目的启动类的同级文件建立
@Configuration
@EnableSwagger2
//是否开启swagger,正式环境一般是需要关闭的(避免不必要的漏洞暴露!),可根据springboot的多环境配置进行设置
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class SwaggerConfig {
// swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
// 为当前包路径
.apis(RequestHandlerSelectors.basePackage("com.china.great.system.controller")).paths(PathSelectors.any())
.build();
}
// 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 页面标题
.title("Spring Boot 测试使用 Swagger2 构建RESTful API")
// 创建人信息
.contact(new Contact("Liu HongYang", "https://blog.csdn.net/qq_35101267", "aaa@qq.com"))
// 版本号
.version("1.0")
// 描述
.description("API 描述")
.build();
}
}
application.properties开关配置:
controller调用:
package com.china.great.system.controller;
import com.china.great.system.common.CommonResultUtil;
import com.china.great.system.config.RedisConfig;
import com.china.great.system.entity.SystemUser;
import com.china.great.system.service.SystemUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@Api("用户相关的api")
public class SystemUserController {
private static final Logger LOGGER = LoggerFactory.getLogger(SystemUserController.class);
@Autowired
private SystemUserService systemUserService;
@Autowired
private RedisConfig redisConfig;
@RequestMapping(value = "/getlist", method = RequestMethod.GET)
@ApiOperation(value="获取列表")
public Object getList(){
List<SystemUser> list = systemUserService.getList();
LOGGER.info(list.toString());
redisConfig.set("aa","bb");
return CommonResultUtil.success(list);
}
}
最后:
访问:http://localhost:8080/swagger-ui.html
完成
推荐阅读
-
分享一个集成.NET Core+Swagger+Consul+Polly+Ocelot+IdentityServer4+Exceptionless+Apollo的入门级微服务开发框架
-
说说在 Spring Boot 中如何配置数据源
-
spring boot Invalid bound statement (not found)
-
一篇超详细的Spring Boot整合Mybatis文章
-
spring boot项目使用@JsonFormat失效问题的解决
-
java-使用war将spring-boot和angular 7应用程序部署到tomcat 8.5中
-
(转)Spring boot 切换配置文件到yaml
-
将Spring Boot JAR应用程序转换为WAR
-
将Spring Boot应用程序绑定到Cloud Foundry中的服务的方法
-
Spring Boot应用程序创建可部署的war文件到tomcat