🌵1.1 添加web依赖、lombok依赖、devtool依赖 🌵1.2 启动应用 🌵1.3 修改启动端口使用80端口
server.port=80 server.servlet.context-path=/🌵1.4 自定义banner http://www.network-science.de/ascii
_______ ______ _______ _______ _________ ( )|\ /|( ___ \ ( ___ )( ___ )\__ __/ | () () |( \ / )| ( ) )| ( ) || ( ) | ) ( | || || | \ (_) / | (__/ / | | | || | | | | | | |(_)| | \ / | __ ( | | | || | | | | | | | | | ) ( | ( \ \ | | | || | | | | | | ) ( | | | | )___) )| (___) || (___) | | | |/ \| \_/ |/ \___/ (_______)(_______) )_(🌵 1.5 接口测试 添加控制器HelloController 给定访问路径/hello 返回字符串Hello boot
2、集成swagger文档
支持多种注解,自动生成接口文档,支持界面测试api接口功能 及时更新代码注释提现在接口文档中 整合简单,添加依赖和简单配置,内嵌于应用中就可以同时发布api的接口文档界面,不需要部署独立服务 官网:http://swagger.io 文档:http://swagger.io/doc
🌵2.1 添加依赖
<!--swagger 依赖--> <!-- swagger2 --> <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> 🌵2.2 配置类
@Configuration //spring 的配置 @EnableSwagger2 //支持swagger2 public class SwaggerConfig { //创建api的对象 @Bean public Docket createRestApi(){ return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select() .apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo(){ //这里可以配置一些基本的页面描述信息,具体查看官网api return new ApiInfoBuilder().build(); } } 🌵2.3 页面测试 重启服务,访问http://localhost/swagger-ui.html# 查看接口是否生成
有些小伙伴可能还是打不开这个网页😢 原因可能是静态资源被拦截了,配置特定资源访问便可,因为swagger-ui.html放在了swagger-ui的jar包resource文件夹。
registry.addResourceHandler("/swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/");可以直接用来测试啦!😎