Swagger是为了方便进行测试后台的restful形式的接口,实现动态的更新,当我们在后台的接口修改了后,swagger可以实现自动的更新,而不需要人为的维护这个接口进行测试。
1、新建SpringBoot项目结构
2、在pom.xml中引入依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
3、在项目根目录下,新建Swagger2.java类。
package testswagger.testswagger;
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 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("testswagger.testswagger.controller"))
.paths(PathSelectors.any())
.build();
}
@SuppressWarnings("deprecation")
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot 测试使用 Swagger2 构建RESTful API")
.description("个人测试用api")
.contact("测试")
.version("1.0")
.build();
}
}
4、新建控制器StudentController.java类
package testswagger.testswagger.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/student")
@Api(value = "测试StudentController接口")
public class StudentController {
@RequestMapping(value = "/add/{id}/{name}",method = RequestMethod.GET)
@ApiOperation(value = "增加学生信息接口",httpMethod = "GET")
@ApiImplicitParams({
@ApiImplicitParam(name="id",value = "学生id",dataType = "int",required = true),
@ApiImplicitParam(name="name",value = "学生姓名",dataType = "String",required = true)
})
public Object addStudent(@PathVariable int id, @PathVariable String name){
System.out.println(id+","+name);
return "success";
}
@RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
@ApiOperation(value = "删除学生信息接口",httpMethod = "GET")
@ApiImplicitParams({
@ApiImplicitParam(name="id",value = "学生id",dataType = "int",required = true),
})
public Object deleteStudent(@PathVariable int id){
System.out.println(id);
return "success";
}
@RequestMapping(value = "/update/{id}/{name}",method = RequestMethod.GET)
@ApiOperation(value = "更新学生信息接口",httpMethod = "GET")
@ApiImplicitParams({
@ApiImplicitParam(name="id",value = "学生id",dataType = "int",required = true),
@ApiImplicitParam(name="name",value = "学生姓名",dataType = "String",required = true)
})
public Object updateStudent(@PathVariable int id, @PathVariable String name){
System.out.println(id+","+name);
return "success";
}
@RequestMapping(value = "/select/{id}",method = RequestMethod.GET)
@ApiOperation(value = "查询学生信息接口",httpMethod = "GET")
@ApiImplicitParams({
@ApiImplicitParam(name="id",value = "学生id",dataType = "int",required = true)
})
public Object addStudent(@PathVariable int id){
System.out.println(id);
return "success";
}
}
5、测试
在网址栏访问:http://localhost:8080/swagger-ui.html