springboot中swagger的使用

好好看,好好学

  • 导入依赖
<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

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值