本文最后更新于 2025-01-10,转载请标明原作者!

结构如下:

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {
    private Boolean success;
    private String errorMsg;
    private T data;
    private Long total;

    public static <U> Result<U> ok(){
        return new Result<U>(true, null, null, null);
    }
    public static <U> Result<U> ok(U data){
        return new Result<U>(true, null, data, null);
    }
    public static Result<List<?>> ok(List<?> data, Long total){
        return new Result<List<?>>(true, null, data, total);
    }
    public static <U> Result<U> fail(String errorMsg){
        return new Result<U>(false, errorMsg, null, null);
    }
}

Controller类方法上返回值示例:

    /**
     * 查看某人发布的电影信息(分页)
     */
    @GetMapping("/getUserPublish")
    public Result<Page<Movie>> getUserPublish(
            @RequestParam("userId") Integer userId,
            @RequestParam(value = "current",defaultValue="1")Integer current){
        return movieService.getUserPublish(userId,current);
    }

Knife4j文档响应结果如下: