SpringBoot整合Swagger与统一返回结果问题

        写此博客,以此留念,解决了因使用统一返回结果对象导致,swagger-ui看不到返回对象属性注释的原因。(没有使用泛型导致)

Springboot统一结果返回对象

@Data
@ApiModel("统一API响应结果封装") 
public class DataResult {

    /**操作结果 0为成功*/
    @ApiModelProperty("操作结果 0为成功")
    private int status = 200;
    /**错误code*/
    @ApiModelProperty("错误code")
    private String statusCode = "0";
    /**操作结果描述信息*/
    @ApiModelProperty("操作结果描述信息")
    private String statusInfo = "SUCCESS";
    @ApiModelProperty("list时,总数量")
    private Integer totalCount = 0;
    @ApiModelProperty("结果对象/集合")
    private T data;

    public DataResult(){
        this(200,"0","SUCCESS",0,null);
    }
    public DataResult(T data){
        this(200,"0","SUCCESS",0,data);
    }
    public DataResult(String statusInfo){
        this(200,"0",statusInfo,0,null);
    }
    public DataResult(String statusInfo,T data){
        this(200,"0",statusInfo,0,data);
    }
    public DataResult(int status,String statusCode,String statusInfo){
        this.status = status;
        this.statusCode = statusCode;
        this.statusInfo = statusInfo;
    }

    public DataResult(int status,String statusCode,String statusInfo,Integer totalCount,T data){
        this.status = status;
        this.statusCode = statusCode;
        this.statusInfo = statusInfo;
        this.totalCount = totalCount;
        this.data = data;
    }

    public static  DataResult success(T data){
        return new DataResult(data);
    }

    public static  DataResult success(String statusInfo){
        return new DataResult(statusInfo);
    }
    public static  DataResult success(){
        return new DataResult();
    }

    public static  DataResult fail(String statusInfo){
        DataResult result = new DataResult<>();
        result.setStatus(new Integer(ApiErrorCode.FAIL.getCode()));
        result.setStatusCode(ApiErrorCode.FAIL.getDesc());
        result.setStatusInfo(statusInfo);
        return result;
    }

    public static  DataResult fail(String statusCode,String statusInfo){
        DataResult result = new DataResult<>();
        result.setStatus(new Integer(ApiErrorCode.FAIL.getCode()));
        result.setStatusCode(statusCode);
        result.setStatusInfo(statusInfo);
        return result;
    }
    public static  DataResult result(int status,String statusCode,String statusInfo,Integer totalCount,T data){
        return new DataResult(status,statusCode,statusInfo,totalCount,data);
    }

    public static  DataResult result(int status,String statusCode,String statusInfo){
        return new DataResult(status,statusCode,statusInfo);
    }

泛型对象

@Data
@ApiModel("适老化摸排详情")
public class Xxx{
    /** 主键id */
    @ApiModelProperty("主键id")
    private Integer id;
    /** 姓名 */
    @ApiModelProperty("姓名")
    private String xm;
}

Swagger常用注解

@Api() :用于类;表示标识这个类(往往是controller)是swagger的资源,tags–表示说明
    @Api(tags="用户模块")
    public class UserController { }

@ApiOperation:用于对一个操作或HTTP方法进行描述
    @ApiOperation(notes = "查询用户",value = "查询用户")
    public User getUser(){}

@ApiParam:用于请求方法上,定义api参数的注解
    public User getUser(@ApiParam("用户id")Integer id){}

@ApiModel():用于类 ;表示对类进行说明,用于参数用实体类接收 
    @ApiModel("用户对象user")
    public class User{}

@ApiModelProperty:用来描述一个Model的属性
    @ApiModelProperty(value = "提示信息")
	private String message;

你可能感兴趣的:(SpringBoot,JAVA,java,spring)