企业项目项目开发提高效率技能Swagger,减少无用都交流,多一些做开发

     讲Swagger之前我们来想一哈,你作为一个后端开发者写好接口后给前端用,然后前端开发人员不知道你的字段是是什么意思,前端界面数据展示不知道用那个接口,他就会来问你,这样是不是就很耽误你的时间。第一个解决方法就是写Word简单,但是麻烦,这里就引入Swagger。

  • 什么是Swagger?

Swagger是一个规范和完整的框架,用于生成、描述、调用和可视化RESTful风格的Web服务。简单来说,Swagger是一个功能强大的接口管理工具,并且提供了多种编程语言的前后端分离解决方案,还可以用来接口测试!

使用教程:

1.建一个SpringBoot项目,再Pom下面引入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>

2.建一个SwaggerConfig.java

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket docket(Environment environment) {
        Profiles profiles = Profiles.of("dev");
        boolean flag = environment.acceptsProfiles(profiles);
        System.out.println(flag);
        return new Docket(DocumentationType.SWAGGER_2).
                apiInfo(apiInfo()).
                enable(flag).
                select().
                apis(RequestHandlerSelectors.basePackage("com.yunhuisu.controller"))
                .build();
    }
    @Bean
    public ApiInfo apiInfo() {
        Contact  contact = new Contact("大阳光", "null", "525769564@qq.com");
        return new ApiInfo(
                "平安校园平台API",
                "简便开发,减少前后端的无用吵闹!",
                "1.8",
                "null",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList()
                );
    }
}

3.建一个Student的实体类

@ApiModel("学生实体类")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    @ApiModelProperty(value = "学生学号",required = true)
    private Integer studentNumber;
    @ApiModelProperty(value = "姓名",required = true)
    private String name;
}

4.建一个Student的控制器

@Api(tags = "学生模块")
@RestController
public class UserController {
    @ApiOperation("添加学生接口")
    @ApiResponses({
            @ApiResponse(code = 200,message = "请求成功!"),
            @ApiResponse(code = 400, message = "请求参数没填好"),
            @ApiResponse(code = 401, message = "没有通过认证"),
            @ApiResponse(code = 403,message = "服务器拒绝访问"),
            @ApiResponse(code = 404, message = "请求路径不对")
    })
    @ResponseBody
    @PostMapping("/addStudent")
    public String addStudent(Student student) {
        return student.toString();
    }
}

测试:

 Swagger的注解的具体用处看Swagger,笔者也是今天才知道这个的,这里是教大家使用

Swagger网站:API Documentation & Design Tools for Teams | Swagger

但是笔者发现了一个比Swagger还好用的那就是: Apifox

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值