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

swagger2 注解详解+使用案例

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

swagger2 注解详解+使用案例

API注解说明:
注解使用位置注意事项
@Api用于controller类上基于类的整体描述
@ApiOperation用于controller类内部的方法上基于方法功能的描述
@ApiImplicitParams用于controller类内部的方法上非Model对象的参数描述,包含多个@ApiImplicitParam
@ApiImplicitParam用于@ApiImplicitParams内单个参数的描述
@ApiModel用于参数为Model对象(自定义的实体类)的类上多个参数封装在一个实体类内
@ApiModelProperty用于@ApiModel对应实体类内属性上与@ApiModel联合使用,用于对类内属性的描述
@ApiResponses用于controller类内部的方法上对Response描述,内部包含多个@ApiResponse (类似@ApiImplicitParams)
@ApiResponse用于@ApiResponses内单个Response code的相关描述
使用案例

@Api(tags = "同步权限",produces = "application/json; charset=UTF-8",protocols = "https")
@RestController
@Slf4j
@Validated
public class PermissionController {
    @Value("${timeout}")
    private long deferTimeout;

    @Resource
    PermissionService permissionService;

    
    @ApiOperation("查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "role", value = "角色", dataType = "String"),
            @ApiImplicitParam(name = "table", value = "表名", dataType = "String")
    })
    @RequestMapping(method = {RequestMethod.GET,RequestMethod.POST},value = "/permission/search",produces = "application/json; charset=UTF-8")
    public Object search(@RequestParam String role,@RequestParam String table){
        return Mono.create(sink->{
            try {
                sink.success(permissionService.search(role,table));
            } catch (Exception e) {
                sink.success(WebResult.getServerErrorResult(e.toString()));
                log.error(LogUtil.getServerErrorLog("search error,",e));
            }
        }).timeout(Duration.ofMillis(deferTimeout),Mono.just(WebResult.getServerTimeOutResult()));
    }
    
    @ApiOperation("新增")
    @PostMapping(value = "/permission/add",produces = "application/json; charset=UTF-8")
    public Object add(@RequestBody PermissionEntity entity){
        return Mono.create(sink->{
            try {
                sink.success(permissionService.add(entity));
            } catch (Exception e) {
                sink.success(WebResult.getServerErrorResult(e.toString()));
                log.error(LogUtil.getServerErrorLog("add error,",e));
            }
        }).timeout(Duration.ofMillis(deferTimeout),Mono.just(WebResult.getServerTimeOutResult()));
    }
    
    @ApiOperation("更新")
    @PostMapping(value = "/permission/update",produces = "application/json; charset=UTF-8")
    public Object update(@RequestBody PermissionEntity entity){
        return Mono.create(sink->{
            try {
                sink.success(permissionService.update(entity));
            } catch (Exception e) {
                sink.success(WebResult.getServerErrorResult(e.toString()));
                log.error(LogUtil.getServerErrorLog("update error,",e));
            }
        }).timeout(Duration.ofMillis(deferTimeout),Mono.just(WebResult.getServerTimeOutResult()));
    }
    
    @ApiOperation("删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "数据唯一id", dataType = "String")
    })
    @PostMapping(value = "/permission/del",produces = "application/json; charset=UTF-8")
    public Object del(@RequestParam String id){
        return Mono.create(sink->{
            try {
                sink.success(permissionService.del(id));
            } catch (Exception e) {
                sink.success(WebResult.getServerErrorResult(e.toString()));
                log.error(LogUtil.getServerErrorLog("update error,",e));
            }
        }).timeout(Duration.ofMillis(deferTimeout),Mono.just(WebResult.getServerTimeOutResult()));
    }

}

@Data
@ApiModel(description = "权限配置信息")
public class PermissionEntity {
    
    @ApiModelProperty(value = "唯一id")
    @Null(groups = Crud.Create.class,message = "_id必须为null")
    @Field(value = "_id")
    private String id;
    
    @ApiModelProperty(value = "角色")
    @NotBlank(message = "role不能为空")
    private String role;
    
    @ApiModelProperty(value = "权限")
    @NotNull(message = "permission不能为空")
    private Map permission;
    
    @ApiModelProperty(value = "更新的用户名")
    private String updateUser;
    
    @ApiModelProperty(value = "更新时间:yyyy-MM-dd HH:mm:ss",hidden = true)
    @Null(message = "updateTime必须为null,由系统自动生成")
    private String updateTime;
}

注意:

案例中的代码引用有自己封装的类,如LogUtil,使用时忽略即可,不影响正常功能。

效果:
@Api
@ApiOperation

@ApiModel
@ApiModelProperty

@ApiImplicitParams
@ApiImplicitParam

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

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

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