个人周记丨2019-12-01 swagger注解,优雅的使用ApiImplicitParams生成复杂对象

概述

在使用swagger编写文档时,我们可能需要接收一个复杂对象,这时我们便会使用ApiImplicitParams注解来完成我们的需求。

需求

现在我要接受一个医院机构对象,如何使用ApiImplicitParams注解来优雅的展示呢。

实现

  • 1.项目搭建与配置
    此处略,请参考网上资料
  • 2.dbo医院机构对象
    dbo对象可以使用mybatis-plus代码生成器完成
    dbo对象需要为其加上ApiModel注解,并使用value方法赋值
@ApiModel(value = "MedicalInstitution",description = "医院机构")
@TableName("MEDICAL_INSTITUTION")
public class MedicalInstitution extends Model<MedicalInstitution> {

    private static final long serialVersionUID = 1L;

    @TableId(value = "MEDI_INST_CODE", type = IdType.UUID)
    @ApiModelProperty("机构码")
    private String mediInstCode;

    @ApiModelProperty("机构名称")
    private String mediInstName;

    @ApiModelProperty("机构的库名")
    private String dbName;

    @ApiModelProperty("历史机构号")
    @TableField(exist = false)
    private String oldMediInstCode;

    public String getOldMediInstCode() {
        return oldMediInstCode;
    }

    public void setOldMediInstCode(String oldMediInstCode) {
        this.oldMediInstCode = oldMediInstCode;
    }

    public String getDbName() {
        return dbName;
    }

    public void setDbName(String dbName) {
        this.dbName = dbName;
    }

    public String getMediInstCode() {
        return mediInstCode;
    }

    public void setMediInstCode(String mediInstCode) {
        this.mediInstCode = mediInstCode;
    }

    public String getMediInstName() {
        return mediInstName;
    }

    public void setMediInstName(String mediInstName) {
        this.mediInstName = mediInstName;
    }

    @Override
    protected Serializable pkVal() {
        return this.mediInstCode;
    }

    @Override
    public String toString() {
        return "MedicalInstitution{" +
        "mediInstCode=" + mediInstCode +
        ", mediInstName=" + mediInstName +
        "}";
    }
}
  • 3.在接口上使用机构对象
    使用ApiImplicitParams注解
    并在该注解体内添加成员ApiImplicitParam,使用dataType方法,为该方法赋值,值为dbo机构对象ApiModel注解value所对应的值(ApiImplicitParams注解体内可以使用多个ApiImplicitParam,如果只有一个ApiImplicitParam,建议直接使用ApiImplicitParam)。例如本文赋值的为MedicalInstitution
@Controller
@RequestMapping("hospital")
@Api(value = "医院模块",description = "医院管理相关的接口")
public class HospitalController {

    @Autowired
    MedicalInstitutionService medicalInstitutionService;


    @ApiOperation("新增医院")
    @RequestMapping(value = "add",method = RequestMethod.POST)
    @ResponseBody
    @ApiImplicitParams(
            @ApiImplicitParam(dataType = "MedicalInstitution",name = "medicalInstitution")
    )
    public JsonRet add(@RequestBody MedicalInstitution medicalInstitution){

        MedicalInstitution dbData = medicalInstitutionService.getById(medicalInstitution.getMediInstCode());
        if(dbData != null){
            return JsonRet.buildFailRet("该机构号已经存在");
        }
        medicalInstitutionService.save(medicalInstitution);
        return JsonRet.buildSuccRet(null);
    }
}

总结

最后看看效果,这就是ApiImplicitParams的优雅使用啦。
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Swagger Maven Plugin是一个用于生成Swagger接口文档的Maven插件。它可以帮助开发人员在构建项目时自动生成Swagger规范的JSON或YAML文件,以便于API文档的管理和使用使用Swagger Maven Plugin生成接口文档swagger.json或swagger.yaml的步骤如下: 1. 在项目的pom.xml文件中添加Swagger Maven Plugin的依赖配置: ```xml <build> <plugins> <plugin> <groupId>com.github.kongchen</groupId> <artifactId>swagger-maven-plugin</artifactId> <version>3.1.8</version> <configuration> <!-- 配置Swagger文档的基本信息 --> <apiSources> <apiSource> <springmvc>true</springmvc> <locations>com.example.controller</locations> <basePath>/api</basePath> <info> <title>API文档</title> <version>1.0.0</version> <description>API接口文档</description> <termsOfServiceUrl>http://example.com/terms-of-service</termsOfServiceUrl> <contact> <email>contact@example.com</email> </contact> <license> <name>Apache 2.0</name> <url>http://www.apache.org/licenses/LICENSE-2.0.html</url> </license> </info> </apiSource> </apiSources> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>generate</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` 2. 在项目根目录下执行以下命令生成Swagger接口文档: ``` mvn compile swagger:generate ``` 3. 执行完上述命令后,Swagger Maven Plugin会根据配置的信息扫描项目中的接口,并生成Swagger规范的JSON或YAML文件。生成的文件默认保存在项目的target目录下的swagger目录中。 生成Swagger接口文档可以通过访问http://localhost:8080/api/swagger-ui.html(假设项目部署在本地的8080端口)来查看和测试API接口。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值