package com.jbangit.common.utils.api; public class Response<T> { private final int code; private final String errorMessage; private final String message; private final Pagination pagination; private final T data; public Response(ErrorCode code, String message, T data) { this(code,message,data,null); } public Response(ErrorCode code, String message, T data, Pagination pagination) { this.code = code.getValue(); this.errorMessage = code.name().replaceAll("_"," "); this.message = message; this.data = data; this.pagination = pagination; } public static class Pagination { public int page; public long total; public int pageSize; } public int getCode() { return code; } public String getErrorMessage() { return errorMessage; } public String getMessage() { return message; } public Pagination getPagination() { return pagination; } public T getData() { return data; } }
package com.lskiot.cms.controller; import com.github.pagehelper.PageInfo; import com.jbangit.common.utils.RestUtils; import com.jbangit.common.utils.api.ErrorCode; import com.jbangit.common.utils.api.Response; import java.util.List; public class BaseController { public <T> Response<T> onSuccess(T data) { return RestUtils.newResponse(data); } public <T> Response<T> onSuccess(String message) { return onSuccess(null,message); } public <T> Response<T> onSuccess(T data, String message) { return RestUtils.newResponse(ErrorCode.success,message,data,null); } public <T> Response<List<T>> onSuccess(PageInfo<T> data) { Response.Pagination pagination = new Response.Pagination(); pagination.total = data.getTotal(); pagination.pageSize = data.getPageSize(); pagination.page = data.getPageNum(); return RestUtils.newResponse(ErrorCode.success,"",data.getList(),pagination); } public <T> Response<T> onFailure(ErrorCode code, String message) { return RestUtils.newResponse(code,message,null,null); } }