Swagger+springboot(前后端分离,世界上最流行的Api框架,postman被禁用了,程序员怎么测试)

SpringBoot集成Swagger

一:项目搭建,配置信息

1.搭建项目

新建一个springboot的项目,在pom.xml文件,导入相关依赖

 <!--        swagger接口测试-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

2.编写一个hello工程

3.配置一个Swagger=>config工程


@SpringBootApplication
// MapperScan注解指定当前项目中的Mapper接口路径,在项目启动时就会自动加载所有的接口
@MapperScan("com.example.store.mapper")
@EnableOpenApi
public class StoreApplication {

    public static void main(String[] args) {
        SpringApplication.run(StoreApplication.class, args);
    }

}

4.访问http://localhost:8080/swagger-ui/

如果你的swgger-ui是3.0的版本需要加,2点多的不需要添加

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

swagger3.0迭代算是重大更新。
一个是添加了starter,依赖用springfox-boot-starter这一个就可以了。具体的可以alt+insert自己搜这个。
ui界面移动到了/swagger-ui/index.html,也就是可以只打/swagger-ui/。接口看起来也正常了点
configure的启动标签那边变成了@EnableOpenApi。
在这里插入图片描述

5.配置swagger,写出你自己的swagger-ui.html页面

在这里插入图片描述

Swagger的bean实例Docket

package com.example.swagger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;


@Configuration
@EnableOpenApi //开启swagger
public class SwaggerConfig {


    //配置了Swagger的Docket的bean实例
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());
    }
    //配置Swagger信息=apiInfo
    private ApiInfo apiInfo(){
        Contact contact = new Contact(
                "王冲",
                "https://blog.csdn.net/qq_51269815?spm=1000.2115.3001.5343",
                "wc2032764143@163.com");
        return new ApiInfo(
                "王冲的csdn博客",
                "事情已逐浮云散",
                "V1.0",
                "https://blog.csdn.net/qq_51269815?spm=1000.2115.3001.5343",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<>());
    }

}

二:Swagger高级教程

1.配置扫描接口及开关

指定扫描某些接口

//配置了Swagger的Docket的bean实例
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                // RequestHandlerSelectors,配置扫描接口的方式
                //basePackage指定要扫描的包
                // any 扫描全部
                // none 都不扫描
                // withClassAnnotation :扫描类上的注解
                // withMethodAnnotation: 扫描方法上的注解
//                .apis(RequestHandlerSelectors.withMethodAnnotation(GetMapping.class))
                .apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller"))
                // paths() 过滤什么路劲
                .paths(PathSelectors.ant("/chong/**"))
                .build();
    }

2.根据不同的环境决定是否开启swagger

application.yaml配置



server:
  port: 8080
//使用dev的生产环境
spring:
  profiles:
    active: dev

application-dev.yaml

server:
  port: 8086

 @Bean
    public Docket docket(Environment enviroment){

        //设置要使用的swagger环境
        Profiles profiles = Profiles.of("dev","test");

        //获取项目的环境 enviroment.acceptsProfiles(profiles)判断是否处在自己设定的环境当中
        boolean flag = enviroment.acceptsProfiles(profiles);
        System.out.println(flag);


        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                // enable是否启动swagger,如果为False,则swagger不能在游览器中访问
                .enable(flag)
                .select()
                // RequestHandlerSelectors,配置扫描接口的方式
                //basePackage指定要扫描的包
                // any 扫描全部
                // none 都不扫描
                // withClassAnnotation :扫描类上的注解
                // withMethodAnnotation: 扫描方法上的注解
//                .apis(RequestHandlerSelectors.withMethodAnnotation(GetMapping.class))
                .apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller"))
                // paths() 过滤什么路劲
                .paths(PathSelectors.ant("/chong/**"))
                .build();
    }

三:使用swagger进行测试和加注释

1.给实体类加注解

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

//给生成文档加注释
@ApiModel("用户实体类")
public class User {
    @ApiModelProperty("用户名")
    public String username;
    @ApiModelProperty("密码")
    public String password;
}

只有在controller层使用才能在swagger-ui/index.html页面见到
在这里插入图片描述

2.给接口增加注释,使用swagger进行接口测试

controller增加注解

package com.example.swagger.controller;

import com.example.swagger.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "你好,明天!")
public class HelloController {
    @GetMapping(value = "/hello")
    public String hello(){
        return "hello";
    }

    @PostMapping(value = "/user")
    public User user(){
        return  new User();
    }

    @ApiOperation("hello控制类")
    @GetMapping(value = "/hello2")
    public String hello2(@ApiParam("用户名") String username){
        return "hello"+username;
    }
}

效果是
在这里插入图片描述

使用swagger进行接口测试
在这里插入图片描述
如果你在实体类中增加了@Data注解,请注意user不能为空

  @ApiOperation("Post测试类")
    @PostMapping(value = "/postt")
    public User postt(@ApiParam("用户名") User user){
        return user;
    }

在这里插入图片描述

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
课程简介这是一门使用Java语言,SpringBoot框架,从0开发一个RESTful API应用,接近企业级的项目(我的云音乐),课程包含了基础内容,高级内容,项目封装,项目重构等知识,99%代码为手写;因为这是项目课程;所以不会深入到源码讲解某个知识点,以及原理,但会粗略的讲解下基础原理;主要是讲解如何使用系统功能,流行的第三方框架,第三方服务,完成接近企业级项目,目的是让大家,学到真正的企业级项目开发技术。适用人群刚刚毕业的学生想提高职场竞争力想学从零开发SpringBoot项目想提升SpringBoot项目开发技术想学习SpringBoot项目架构技术想学习企业级项目开发技术就是想学习SpringBoot开发能学到什么从0开发一个类似企业级项目学会能做出市面上90%通用API快速增加1到2年实际开发经验刚毕业学完后能找到满意的工作已经工作学完后最高涨薪30%课程信息全课程目前是82章,155小时,每节视频都经过精心剪辑。在线学习分辨率最高1080P课程知识点1~11章:学习方法,项目架构,编码规范,Postman使用方法,Git和Github版本控制12~16章:搭建开发环境,快速入门SpringBoot框架17~20章:快速入门MySQL数据库21~30章:MyBatis,登录注册,找回密码,发送短信,发送邮件,企业级接口配置31~41章:实现歌单,歌单标签,音乐,列表分页,视频,评论,好友功能42~48章:阿里云OSS,话题,MyBatis-plus,应用监控49~53章:Redis使用,集成Redis,SpringCache,HTTP缓存54~58章:Elasticsearch使用,集成Elasticsearch,使用ES搜索59~61章:商城,集成支付宝SDK,支付宝支付62~64章:常用哈希和加密算法,接口加密和签名65~67章:实时挤掉用户,企业级项目测试环境,企业级接口文档68~69章:SpringBoot全站HTTPS,自签证书,申请免费证书70~73章:云MySQL数据库,云Redis数据库使用,轻量级应用部署环境,域名解析74~80章:Docker使用,生产级Kubernetes集群,域名解析,集群全站HTTPS81~82章:增强和重构项目,课程总结,后续学习计划
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@黑夜中的一盏明灯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值