spring-boot集成swagger及相关注解

maven依赖

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

 

创建swagger的配置类

@Configuration//配置类
@EnableSwagger2 //swagger注解
public class SwaggerConfig {
    @Bean
    public Docket webApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                .paths(Predicates.not(PathSelectors.regex("/admin/.*")))
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build();

    }

    private ApiInfo webApiInfo(){
        return new ApiInfoBuilder()
                .title("API文档")
                .description("项目的API文档,以便查看、测试")
                .version("1.0")
                .contact(new Contact("java", "http://swimming.com", "2222888444@qq.com"))
                .build();
    }
}

打开网址查看API文档  http://ip:端口/swagger-ui.html

 @注解

  @Api   --- 用于类,表示这个类使swagger的资源

@Api(tags = {"教师操作接口"}, value = "TeacherApi")
@CrossOrigin
@RestController
@RequestMapping("/serviceedu/teacher")
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;
}

 

   @ApiOperation  ---  用于方法,表示一个http请求

    @ApiOperation(value = "获取所有教师信息", notes = "注意:")
    @GetMapping("/findAll")
    public R getAll(){
        return R.ok().data("result",eduTeacherService.list(null));
    }

 

 

  @ApiParam --- 用于方法参数,对参数进行说明

    name  --  参数名

    value  --  参数说明

    required  -- 是否必填

    @ApiOperation(value = "添加一条教师记录", notes = "注意:")
    @PostMapping("/saveTeacher")
    public R saveTeacher(
            @ApiParam(name="教师对象", value="传入json格式", required = true) 
            @RequestBody EduTeacher teacher){
        boolean res = eduTeacherService.save(teacher);
        if (res){
            return R.ok().message("插入成功");
        }else {
            return R.error().message("插入失败");
        }
    }

 

 

 

 

@ApiModel  ---  用于实体类,用于参数用实体类接受

  value -- 表示对象名

  description  -- 描述

@ApiModelProperty  --- 用于实体类中的字段方法;表示对model属性的说明或数据操作更改

  value --- 字段说明

  name --- 属性名字

  dataType  ---  属性类型

  required --- 是否必填

  example -- 举例说明

  hidden -- 是否隐藏

@Data
@ApiModel(value="EduTeacher对象", description="讲师")
public class EduTeacher implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "讲师ID")
    @TableId(value = "id", type = IdType.ID_WORKER_STR)
    private String id;

}

 

posted @ 2021-03-03 15:24  樱花葬礼  阅读(154)  评论(0)    收藏  举报