微服务swagger解析部署使用全流程

1、介绍

swagger是一个在线接口说明文档,在代码中通过注解的方式将说明问题集成到项目,代码发生修改,说明文档同步修改,前后台联调可以快速同步数据。

2、应用

1、引入依赖

复制代码
<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、编写配置类

复制代码
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
​
/**
 * @author szsw
 * @date 2023/3/1 18:12:17
 */
@Configuration
public class SwaggerConfig {
​
    @Bean
    public Docket docket() {
        /*采用swagger2版本*/
        return new Docket(DocumentationType.SWAGGER_2)
                /*生成api相关信息*/
                .apiInfo(createApiInfo())
                /*返回一个apiSelectorBuilder对象,用来控制接口生成在线文档*/
                .select()
                /*扫描某个包下的接口*/
                .apis(RequestHandlerSelectors.basePackage("org.jsoft.demo.controller"))
                /*生成所有的api*/
                .paths(PathSelectors.any())
                .build();
    }
​
    /**
     * 创建api相关信息
     *
     * @return ApiInfo
     */
    private ApiInfo createApiInfo() {
        return new ApiInfoBuilder()
                /*自定义title*/
                .title("swagger demo")
                /*描述*/
                .description("swagger示例")
                /*定义服务域名*/
                .termsOfServiceUrl("")
                /*自定义版本号*/
                .version("0.0.1")
                .build();
    }
​
}
​

3、开启Swagger

复制代码
@EnableSwagger2                 启动器

4、编写示例

1、实体类

复制代码
package org.jsoft.demo.dto;
​
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
​
import java.util.Date;
​
​
@Data
@ApiModel
public class UserUpdateDto {
​
    @ApiModelProperty(value = "主键", required = true, example = "asdfa54s56d")
    private String id;
    @ApiModelProperty(value = "名字", required = true, example = "张三")
    private String name;
    @ApiModelProperty(value = "密码", required = true, example = "123456")
    private String password;
    @ApiModelProperty(value = "年龄", required = true, example = "20")
    private Integer age;
    @ApiModelProperty(value = "生日", required = true, example = "1998-07-07")
    private Date birthday;
    @ApiModelProperty(value = "分数", example = "100.5")
    private Double score;
​
}
/*
value 描述
required 必填项
example 举例
*/
​
复制代码
package org.jsoft.demo.dto;
​
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
​
import java.util.Date;
​
​
@Data
@ApiModel
public class UserAddDto {
​
    @ApiModelProperty(value = "名字", required = true, example = "张三")
    private String name;
    @ApiModelProperty(value = "密码", required = true, example = "123456")
    private String password;
    @ApiModelProperty(value = "年龄", required = true, example = "20")
    private Integer age;
    @ApiModelProperty(value = "生日", required = true, example = "1998-07-07")
    private Date birthday;
    @ApiModelProperty(value = "分数", example = "100.5")
    private Double score;
​
}
​

2、Controller

复制代码
package org.jsoft.demo.controller;
​
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.jsoft.demo.dto.UserAddDto;
import org.jsoft.demo.dto.UserUpdateDto;
import org.jsoft.demo.utils.Result;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
​
import java.util.Map;
​
​
@Api(tags = "用户")
@RestController
@RequestMapping("/user")
public class UserController {
​
    @PostMapping
    @ApiOperation("用户新增")
    public Result add(@RequestBody UserAddDto dto) {
        return Result.ok();
    }
​
    @DeleteMapping("/{id}")
    @ApiOperation("用户删除")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "删除的用户主键", name = "id", paramType = "path")
    })
    public Result delete(@PathVariable String id) {
        return Result.ok();
    }
​
    @PutMapping
    @ApiOperation("用户修改")
    public Result update(@RequestBody UserUpdateDto dto) {
        return Result.ok();
    }
​
    @GetMapping("/{id}")
    @ApiOperation("用户详情")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户主键", paramType = "path")  //path是指地址传参。
    })
    public Result info(@PathVariable String id) {
        return Result.ok();
    }
​
    @GetMapping
    @ApiOperation("用户查询")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "姓名", name = "name", paramType = "query"),
            @ApiImplicitParam(value = "年龄", name = "age", paramType = "query"),
            @ApiImplicitParam(value = "分数", name = "score", paramType = "query")  // get地址栏传参
    })
    public Result query(@ApiIgnore @RequestParam Map<String, String> params) {
        return Result.ok();
    }
​
}
​

3、访问地址

http:localhost:100/swagger-ui.html 100端口号

相关推荐
不早睡不改名@12 分钟前
Netty源码解析---FastThreadLocal-addToVariablesToRemove方法详解
java·网络·笔记·学习·netty
小信丶18 分钟前
Spring MVC @SessionAttributes 注解详解:用法、场景与实战示例
java·spring boot·后端·spring·mvc
no245441021 分钟前
深度解析:WebP会在几年内取代JPG吗?
java·大数据·人工智能·科技·ai
William Dawson28 分钟前
【Java Stream 流:高效、优雅的集合操作 ✨】
java·windows·python
疯狂成瘾者29 分钟前
SseEmitter
java
一只专注api接口开发的技术猿33 分钟前
商品详情API的SLA保障体系:监控告警、异常检测与自动化修复
运维·数据库·架构·自动化
Vic1010139 分钟前
Java深度分页性能优化:从问题本质到生产实践
java·adb·性能优化
爱丽_39 分钟前
Redis 持久化与高可用:RDB/AOF、主从复制、哨兵与一致性取舍
java·后端·spring
伯远医学41 分钟前
如何判断提取的RNA是否可用?
java·开发语言·前端·javascript·人工智能·eclipse·创业创新
盐水冰1 小时前
【烘焙坊项目】补充完善(1)- SpringAI大模型接入
java·后端·大模型