SpringMVC使用Swagger

Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。
我们这里使用SpringBoot来快速搭建一个MVC,同时使用Swagger插件。
pom.xml,主要是引用swagger2

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath />
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

Swagger2配置类

package cn.duanjt.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.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration
@EnableSwagger2
public class Swagger2 {
/**
* 创建API应用
* apiInfo() 增加API相关信息
* 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
* 本例采用指定扫描的包路径来定义指定要建立API的目录。
*
* @return
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("cn.duanjt.controller"))//指定要扫描的包名
.paths(PathSelectors.any())
.build();
} /**
* 创建该API的基本信息(这些基本信息会展现在文档页面中)
* 访问地址:http://项目实际地址/swagger-ui.html
* @return
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建RESTful APIs")
.description("更多请关注http://www.baidu.com")
.termsOfServiceUrl("http://www.baidu.com")
.contact("段江涛")
.version("1.0")
.build();
}
}

控制器StudentController

package cn.duanjt.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import cn.duanjt.pojo.Student;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation; @RestController
@RequestMapping("/student")
@Api(description = "学生信息")
public class StudentController { @RequestMapping(path = "/getAll", method = RequestMethod.GET)
@ApiOperation(value = "获取所有用户信息", notes = "备注:获取所有用户信息")
public Student getAll() {
Student stu = new Student(1, "zhangsan", 20);
return stu;
} @RequestMapping(path = "/getById", method = RequestMethod.GET)
@ApiOperation(value = "根据Id查询用户信息", notes = "备注:根据Id查询用户信息")
@ApiImplicitParam(paramType = "query", name = "id", value = "用户id", required = true, dataType = "Integer")
public Student getById(int id) {
Student stu = new Student(id, "zhangsan", 20);
return stu;
} @RequestMapping(path = "/insert", method = RequestMethod.POST)
@ApiOperation(value = "新增用户信息", notes = "备注:新增用户信息")
public String insert(Student student){
System.out.println(student.toString());
return "成功";
}
}

说明:

1.需要在配置类Swagger2中指定需要扫描的包名
2.@Api标记在类上面,用于表示该类需要被Swagger扫描
3.@ApiOperation标记方法
4.@ApiImplicitParam标记方法,描述参数信息

运行之后,在浏览器输入:http://localhost:8080/swagger-ui.html

SpringMVC使用Swagger

原文参考:https://blog.csdn.net/sanyaoxu_2/article/details/80555328

上一篇:codevs 1052 地鼠游戏 优先队列


下一篇:CODEVS——T 1269 匈牙利游戏 2012年CCC加拿大高中生信息学奥赛