spring boot 启用 swagger2——迹忆客-ag捕鱼王app官网
swagger2 是一个开源项目,用于为 restful web 服务生成 rest api 文档。 它提供了一个用户界面来通过 web 浏览器访问我们的 restful web 服务。
要在 spring boot 应用程序中启用 swagger2,我们需要在我们的构建配置文件中添加以下依赖项。
io.springfox
springfox-swagger2
2.7.0
io.springfox
springfox-swagger-ui
2.7.0
对于 gradle 用户,在 build.gradle 文件中添加以下依赖项。
compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.7.0'
compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.7.0'
现在,在主 spring boot 应用程序中添加 @enableswagger2
注解。 @enableswagger2
注解用于为我们的 spring boot 应用程序启用 swagger2。
主 spring boot 应用程序的代码如下所示
package com.jiyik;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import springfox.documentation.swagger2.annotations.enableswagger2;
@springbootapplication
@enableswagger2
public class swaggerapplication {
public static void main(string[] args) {
springapplication.run(swaggerapplication.class, args);
}
}
接下来,创建 docket bean 来为我们的 spring boot 应用程序配置 swagger2。 我们需要定义基本包来为 swagger2 配置 rest api。
@bean
public docket productapi() {
return new docket(documentationtype.swagger_2).select()
.apis(requesthandlerselectors.basepackage("com.jiyik")).build();
}
现在,将此 bean 添加到主 spring boot 应用程序类文件本身中,我们的主 spring boot 应用程序类将如下所示
package com.jiyik;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.context.annotation.bean;
import springfox.documentation.builders.requesthandlerselectors;
import springfox.documentation.spi.documentationtype;
import springfox.documentation.spring.web.plugins.docket;
import springfox.documentation.swagger2.annotations.enableswagger2;
@springbootapplication
@enableswagger2
public class swaggerapplication {
public static void main(string[] args) {
springapplication.run(swaggerapplication.class, args);
}
@bean
public docket productapi() {
return new docket(documentationtype.swagger_2).select()
.apis(requesthandlerselectors.basepackage("com.jiyik")).build();
}
}
现在,在构建配置文件中添加以下 spring boot starter web 依赖项来编写 rest endpoints,如下所示
maven 用户可以在我们的 pom.xml 文件中添加以下依赖项
org.springframework.boot
spring-boot-starter-web
gradle 用户可以在 build.gradle 文件中添加以下依赖项
compile('org.springframework.boot:spring-boot-starter-web')
现在,这里显示了在 rest controller 文件中构建两个简单的 restful web 服务 get 和 post 的代码
package com.jiyik.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;
import org.springframework.web.bind.annotation.restcontroller;
import java.util.arraylist;
import java.util.list;
@restcontroller
public class swaggerapicontroller {
@requestmapping(value = "/products", method = requestmethod.get)
public list getproducts() {
list productslist = new arraylist();
productslist.add("honey");
productslist.add("almond");
return productslist;
}
@requestmapping(value = "/products", method = requestmethod.post)
public string createproduct() {
return "商品创建成功";
}
}
完整的构建配置文件如下
maven – pom.xml
4.0.0 com.tutorialspoint swagger-demo 0.0.1-snapshot jar swagger-demo demo project for spring boot org.springframework.boot spring-boot-starter-parent 1.5.9.release utf-8 utf-8 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test io.springfox springfox-swagger2 2.7.0 io.springfox springfox-swagger-ui 2.7.0 org.springframework.boot spring-boot-maven-plugin
gradle – build.gradle
buildscript { ext { springbootversion = '1.5.9.release' } repositories { mavencentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springbootversion}") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot' group = 'com.jiyik' version = '0.0.1-snapshot' sourcecompatibility = 1.8 repositories { mavencentral() } dependencies { compile('org.springframework.boot:spring-boot-starter-web') testcompile('org.springframework.boot:spring-boot-starter-test') compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.7.0' compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.7.0' }
现在,我们可以创建一个可执行的 jar 文件,并使用以下 maven 或 gradle 命令运行 spring boot 应用程序。
对于 maven,使用下面给出的命令
$ mvn clean install
成功之后,我们可以在 target 目录下找到 jar 文件。
对于 gradle,使用下面给出的命令
$ gradle clean build
这里我们使用 idea 来启动服务(读者也可以使用上面两种方式中的一种生成可执行 jar 包)
现在,应用程序将在 tomcat 端口 8080 上启动,如图所示
现在,在 web 浏览器中点击 url 并查看 swagger api 功能。
http://localhost:8080/swagger-ui.html