Java框架之swagger学习及使用

Swagger简介

前后端分离

Vue + SpringBoot

后端时代:前端只用管理静态页面;html==> 后端。模板引擎 JSP =>后端是主力

前后端分离式时代:

  • 后端 :后端控制层,服务层,数据访问层 【后端团队】
  • 前端 :前端控制层,视图层 【前端团队】
    • 伪造后端数据,json。已经存在了,不需要后端,前端工程依旧能够跑起来
  • 前端后如何交互 ? ===> API
  • 前后端相对独立,松耦合;
  • 前后端甚至可以部署在不同的服务器上;

产生一个问题:

  • 前后端集成联调,前端人员和后端人员无法做到 “即使协商, 尽早解决”,最终导致问题集中爆发;

解决方案:

  • 首先指定schema[计划的提纲],实时更新最新API,降低集成的风险;
  • 早些年:指定word计划文档;
  • 前后端分离:
    • 前端测试后端接口:postman
    • 后端提供接口,需要实时更新最新的消息及改动!

Swagger特点

  • 号称世界上最流行的Api框架;
  • RestFul Api 文档在线自动生成工具=>Api文档与API定义同步更新
  • 直接运行,可以在线测试API接口
  • 支持多种语言:(Java,Php…)

官网:[https://swagger.io/

SpringBoot 集成Swagger的使用

在项目使用Swagger需要 springbox;

  • swagger2
  • ui
    项目结构
    在这里插入图片描述
  1. 新建一个SpringBoot 的web项目

  2. 导入相关依赖

   <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
   <dependency>
       <groupId>io.springfox</groupId>
       <artifactId>springfox-swagger2</artifactId>
       <version>2.9.2</version>
   </dependency>
   <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
   <dependency>
       <groupId>io.springfox</groupId>
       <artifactId>springfox-swagger-ui</artifactId>
       <version>2.9.2</version>
   </dependency>
   
  1. 编写一个Hello工程
    helloController文件编写:
package com.kuang.swagger.controller;


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

import java.sql.SQLOutput;

@RestController

public class helloController {
    @RequestMapping("/hello")
    public  String hello(){
        return "hello";
    }



}


  1. 配置Swagger ==
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java使用Swagger可以通过以下步骤来实现: 1. 在你的Maven或Gradle项目中添加Swagger的依赖项。例如,对于Maven项目,可以在`pom.xml`文件中添加以下内容: ```xml <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. 创建一个配置类来启用Swagger。在该类上使用`@EnableSwagger2`注解,如下所示: ```java import org.springframework.context.annotation.Configuration; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class SwaggerConfig { } ``` 3. 创建一个API控制器类,并使用Swagger的注解来描述API接口。例如: ```java import org.springframework.web.bind.annotation.*; import io.swagger.annotations.*; @RestController @RequestMapping("/api") @Api(tags = "示例API") public class SampleController { @GetMapping("/hello") @ApiOperation("获取欢迎信息") public String hello() { return "Hello, Swagger!"; } @PostMapping("/user") @ApiOperation("创建用户") public User createUser(@RequestBody User user) { // 处理创建用户的逻辑 return user; } // 其他API接口... } ``` 4. 启动你的应用程序,并访问Swagger UI界面。Swagger UI界面默认可以通过`http://localhost:8080/swagger-ui.html`进行访问。你将能够看到由Swagger自动生成的API文档,并可以在界面上进行交互测试和调试。 请注意,上述示例中使用了Spring Boot和Springfox Swagger来演示使用Swagger的过程。你可以根据你的项目框架和需要进行相应的调整。另外,还可以通过注解来进一步定制API文档的展示效果和描述信息。 希望这些步骤能够帮助你在Java项目中成功使用Swagger
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值