栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Swagger简单入门

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Swagger简单入门

Swagger简单入门

学习目标:

  • 了解swagger的作用和概念
  • 了解前后端分离
  • 在springboot中集成swagger
swagger简介 前后端分离

主流的解决方案:Vue+spring boot

前后端分离时代:

  • 后端:后端控制层、服务层、数据访问层
  • 前端:前端控制层、视图层
  • 前后端如何交互:->通过API来来访问
  • 前后端相互独立,耦合度低,前后端也可以部署在不同的服务器上

产生的问题:

  • 前后端集成联调苦难,前端后端人员无法“及时沟通,尽早解决”,会导致问题集中爆发

解决方案:

  • 首先指定schema【计划的提纲】,实时更新最新API,降低集成风险
  • 前后端分离需要提供一个实时的接口工具(早期测试接口postman)
Swagger
  • 号称世界上最流行的API框架
  • RestFul API 文档在线自动生成工具->API文档和API的定义同步更新
  • 直接运行,可以在线测试API接口
  • 支持多种语言(Java、PHP等)

官网:https://swagger.io/

在项目中使用swagger

需要导入Java包

  • swagger2
  • ui
springboot集成swagger

1、新建springboot项目

2、导入相关依赖



    io.springfox
    springfox-swagger2
    2.9.2




    io.springfox
    springfox-swagger-ui
    2.9.2

3、编写helloworld进行测试

package edu.kuquan.swagger.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    // /error 默认的请求

    @RequestMapping("/hello")
    public String hello() {
        return "Hello world!";
    }
}

4、配置swagger(编写配置类)

package edu.kuquan.swagger.config;

import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2 // 开启swagger2
public class SwaggerConfig {
    
}

springboot2.6以上需要额外配置properties

spring.mvc.pathmatch.matching-strategy=ant_path_matcher

5、访问swagger

链接:http://localhost:8080/swagger-ui.html


根据填写的映射方式,显示对应的请求方式

springboot配置swagger swagger信息模块配置
package edu.kuquan.swagger.config;

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

import java.util.ArrayList;

@Configuration
@EnableSwagger2 // 开启swagger2
public class SwaggerConfig {

    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
    }

    // swagger配置信息
    private ApiInfo apiInfo() {
        return new ApiInfo("swagger-demo 工程 Api Documentation", "穷且益坚,不坠青云之志", "1.0", "urn:tos", new Contact("苦泉", "", ""), "Apache 2.0", "https://space.bilibili.com/24354849", new ArrayList());

    }
}
swagger配置扫描接口
@Bean
public Docket docket() {
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        .select()
        .apis(RequestHandlerSelectors.basePackage("edu.kuquan.swagger.config")) // 指定要扫描的包
        .build();
}
配置扫描的路径
@Bean
public Docket docket() {
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        .select()
        .apis(RequestHandlerSelectors.basePackage("edu.kuquan.swagger.config")) // 指定要扫描的包
        .paths(PathSelectors.ant("/*")) // 确定要扫描的路径
        .build();
}
配置是否启动swagger
@Bean
public Docket docket() {
    return new Docket(DocumentationType.SWAGGER_2)
    .apiInfo(apiInfo())
    .enable(true); // true:启动swagger false:关闭swagger
}

如何确定swagger的使用环境,只在生产和测试环境使用,发布的时候不适用

  • 判断是不是生产环境
  • 注入enable
@Bean
public Docket docket(Environment environment) {

    // 获取项目环境,设置swagger的显示环境
    Profiles profiles = Profiles.of("dev", "test");

    // 通过profiles判断当前所在环境
    boolean b = environment.acceptsProfiles(profiles);


    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        .enable(b);
}
配置swagger的API文档分组
@Bean
public Docket docket() {
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        .groupName("ku");
}

如何配置多个分组?

配置多个Docket即可

@Bean
public Docket docket() {
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        .groupName("ku");
}

@Bean
public Docket docket2() {
    return new Docket(DocumentationType.SWAGGER_2).groupName("test");
}
在swagger中显示对象的结构
// 只要我们的接口中返回值中存在实体类,就会被swagger给扫描到
@PostMapping("/user")
public User getuser(String name, Integer age) {
    return new User(name, age);
}

结果:


给对象属性添加注解

@ApiModel("用户实体类")
public class User {
    @ApiModelProperty("姓名")
    public String name;
    @ApiModelProperty("年龄")
    public Integer age;

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public User() {}
}

结果:


给接口增加注解

@ApiOperation("你好,世界!")
@GetMapping("/hello")
public String hello() {
    return "Hello world!";
}

结果:


swagger接口测试

无参数测试

直接执行便可


有参数测试

赋值后执行即可

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/956474.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号