好好看,好好学
- 导入依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
需要注意的是:父模块或依赖模块或自身有web启动器,即:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
否则启动报错。
- swagger配置类
@Configuration //声明该类是个配置类
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("springboot.model_swagger.controller")) //这个地方是controller包所在的路径,根据自己的情况更改
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("eboot-api文档")
.description("更多信息,请访问https://www.jianshu.com/u/3979cb11f079")
.termsOfServiceUrl("https://gitee.com/QuanZhanZhiLu/easy-boot")
.version("1.0")
.build();
}
}
除此之外,还需要在应用类上添加@EnableSwagger2注解,开启swagger功能:
- 在controller包下添加controller类,并添加方法(用到的注解,稍后会有解释)
@RestController
@Api(value = "TestController")
public class TestController {
@GetMapping("print1")
@ApiOperation(value = "打印key和value",notes = "备注")
@ApiImplicitParam(paramType="query", name = "str", value = "谁便一个字符串", required = true, dataType = "String")
public Map<String, String> printOne(@RequestParam String str) {
Map<String, String> resultMap = new HashMap<>();
resultMap.put("key", "value");
return resultMap;
}
@PostMapping("print2")
@ApiOperation(value = "打印多条数据",notes = "备注")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query",name = "str1",value = "谁便一个字符串",required = true,dataType = "String"),
@ApiImplicitParam(paramType = "query",name = "str2",value = "谁便一个字符串",required = true,dataType = "String")
})
public Map<String,String> printTwo(@RequestParam String str1,@RequestParam String str2){
Map<String, String> resultMap = new HashMap<>();
resultMap.put("key1",str1);
resultMap.put("key2",str2);
return resultMap;
}
//接受对象参数
@PostMapping("saveDoctor")
@ApiOperation(value = "保存医生信息")
public Map<String,String> saveDoctor(@RequestBody Doctor doctor){
Map<String, String> resultMap = new HashMap<>();
resultMap.put("key",doctor.getName());
return resultMap;
}
@PostMapping("updateDoctor/{doctorId}")
@ApiOperation(value = "修改医生的信息")
@ApiImplicitParam(paramType = "query",name = "doctorId",value = "医生的id",required = true,dataType = "String")
public Doctor updateDoctor(@RequestParam String doctorId,@RequestBody Doctor doctor){
return doctor;
}
}
注解解析:
如果方法中用对象作为参数,如:
那么Doctor类应该是这样:
@ApiModel(value = "医生对象模型") //声明该对象是个swagger模板对象
public class Doctor {
@ApiModelProperty(value = "id",required = true) //类属性用该注解标注,用来声明该属性是个模板对象属性
private Integer id;
@ApiModelProperty(value = "医生姓名",required = true)
private String name;
public Doctor() {
}
public Doctor(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Doctor{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
- 访问swagger:localhost:8080/swagger-ui.html