swagger注释使用(基于springboot框架)

swagger注释使用

一、导入swagger所需依赖

<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

二、swagger基本配置config

三、基于springboot开启使用

@EnableAsync
@SpringBootApplication
@MapperScan("com.zqs.dao")
@EnableSwagger2 //开启swagger2支持
public class SpringbootApplication {
          
   

    public static void main(String[] args) {
          
   
        // SpringApplication.run(Springboot01Application.class, args);
        SpringApplication springApplication = new SpringApplication(SpringbootApplication.class);
        //springApplication.setBannerMode(Banner.Mode.OFF);

        springApplication.run(args);

    }

}

四、访问swagger(没有使用注释)

http://localhost/swagger-ui.html

可以看出来将所有的控制层都展示出来了

四、注释使用与效果(userController为例)

注释 1、@Api(value = “用户接口”,tags = “对用户的注册登录增删改查等操作的接口”)

用来改变接口名称

进入控制层查看方法(3个方法)

@Autowired
    private UserService userService;

    @GetMapping("queryAll")
    public ResultCommon queryAll(){
          
   
        return userService.queryAll();
    }

    @DeleteMapping("user/{id}")
    public String deleteStudent(@PathVariable("id") Integer id){
          
   
        log .info("根据id删除学生的方法执行了");
        return "delete success";
    }
    @GetMapping("student/{id}")
    public String getStudentById(@PathVariable("id") Integer id){
          
   
        log .info("根据id查询学生的方法执行了");
        return "get success";
    }

注释 2、@ApiOperation()

@ApiOperation(value = “删除学生”,notes=“restfull风格接口,根据id删除学生”,httpMethod = “DELETE”)

注释 3、@ApiImplicitParams()

对多个参数的注释
@ApiOperation(value = "删除学生",notes="restfull风格接口,根据id删除学生",httpMethod = "DELETE")
     @ApiImplicitParams({
          
   
            @ApiImplicitParam(value = "学生id",dataType = "int"
            ,example = "1")
    })

注释 4、@ApiParm()

@ApiParam(value = “学生id”,defaultValue = “1” ,type = “int”,required = true)
public String deleteStudent(@ApiParam(value = "学生id",defaultValue = "1" ,type = "int",required = true) @PathVariable("id") int id){
          
   
        log .info("根据id删除学生的方法执行了");
        return "delete success";
    }

注释 5、接受的参数是实体类的 Json对象

实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "学生json对象描述",description="天青色等烟雨")
public class StudentSwagger {
          
   
    @ApiModelProperty(name="id",value = "学生id建议输入1-2000",example = "1",dataType = "int")
    private int id;
    @ApiModelProperty(name="name",value = "学生姓名,姓名长度再100字以内",example = "赵青松",dataType = "string")
    private String name;
}

控制层方法

@ApiOperation("添加学生的接口,接受的参数为学生json对象")
    @PostMapping("student")
    public ResultCommon addStudentById(  @ApiParam(name = "studentSwagger",required = true) @RequestBody StudentSwagger studentSwagger ){
          
   
        log .info("根据id查询学生的方法执行了");
        return ResultCommon.success("添加学生成功");
    }

swagger测试

经验分享 程序员 微信小程序 职场和发展