微服务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端口号

相关推荐
Jason-河山4 分钟前
利用 Python 爬虫采集 1688商品详情
java·http
计算机源码社4 分钟前
分享一个餐饮连锁店点餐系统 餐馆食材采购系统Java、python、php三个版本(源码、调试、LW、开题、PPT)
java·python·php·毕业设计项目·计算机课程设计·计算机毕业设计源码·计算机毕业设计选题
Zww08918 分钟前
idea插件市场安装没反应
java·ide·intellij-idea
夜雨翦春韭9 分钟前
【代码随想录Day31】贪心算法Part05
java·数据结构·算法·leetcode·贪心算法
计算机学姐9 分钟前
基于微信小程序的调查问卷管理系统
java·vue.js·spring boot·mysql·微信小程序·小程序·mybatis
problc20 分钟前
Android 组件化利器:WMRouter 与 DRouter 的选择与实践
android·java
程序员南飞2 小时前
ps aux | grep smart_webrtc这条指令代表什么意思
java·linux·ubuntu·webrtc
一颗花生米。3 小时前
深入理解JavaScript 的原型继承
java·开发语言·javascript·原型模式
问道飞鱼3 小时前
Java基础-单例模式的实现
java·开发语言·单例模式