java 基础响应体定义 - 通用

时间:2021-12-29 08:31:40
package com.teewon.viewservice.entitiy.base;

import java.io.Serializable;
import java.util.List; public class Response<T> implements Serializable
{
private static final long serialVersionUID = 6766428592214605847L; private T responseEntity; private PageInfo<T> pageInfo; private ServerResult serverResult; public Response()
{
this.serverResult = getServerResult();
} public Response(T t)
{
this.responseEntity = t;
this.serverResult = getServerResult();
} public Response(List<T> list)
{
if (list instanceof Page)
{
this.pageInfo = new PageInfo<T>(list);
}
else
{
this.pageInfo = new PageInfo<T>();
this.pageInfo.setList(list);
this.pageInfo.setTotal(null == list ? 0 : list.size());
}
this.serverResult = getServerResult();
} public Response(String resultCode,String resultMessage,String internalMessage)
{
this.serverResult = new ServerResult();
serverResult.setResultCode(resultCode);
serverResult.setResultMessage(resultMessage);
} public Response(String resultCode,String resultMessage)
{
this(resultCode,resultMessage,resultMessage);
} public ServerResult getServerResult()
{
if (serverResult == null)
{
serverResult = new ServerResult();
serverResult.setResultCode("200");
serverResult.setResultMessage("请求成功");
}
return serverResult;
} public void setServerResult(ServerResult serverResult)
{
this.serverResult = serverResult;
} public T getResponseEntity()
{
return responseEntity;
} public void setResponseEntity(T responseEntity)
{
this.responseEntity = responseEntity;
} public PageInfo<T> getPageInfo()
{
return pageInfo;
} public void setPageInfo(PageInfo<T> pageInfo)
{
this.pageInfo = pageInfo;
}
}
package com.teewon.viewservice.entitiy.base;

import java.io.Serializable;

public class ServerResult implements Serializable
{
private static final long serialVersionUID = -7556295999658379023L; /**
* 返回码
*/
private String resultCode; /**
* 返回描述信息
*/
private String resultMessage; public ServerResult()
{ } public ServerResult(String resultCode)
{
this(resultCode, "Success");
} public ServerResult(String resultCode, String resultMessage)
{
super();
this.resultCode = resultCode;
this.resultMessage = resultMessage;
} public String getResultMessage()
{
return resultMessage;
} public void setResultMessage(String resultMessage)
{
this.resultMessage = resultMessage;
} public String getResultCode()
{
return resultCode;
} public void setResultCode(String resultCode)
{
this.resultCode = resultCode;
}
}
package com.teewon.viewservice.entitiy.base;

import java.io.Serializable;
import java.util.List; public class PageInfo<T> implements Serializable {
private static final long serialVersionUID = 1L;
private int pageNum;
private int pageSize;
private int size; private int startRow;
private int endRow;
//鎬昏褰曟暟
private long total;
//鎬婚〉鏁�
private int pages;
//缁撴灉闆�
private List<T> list; //绗竴椤�
private int firstPage;
//鍓嶄竴椤�
private int prePage;
//涓嬩竴椤�
private int nextPage;
//鏈�鍚庝竴椤�
private int lastPage; //鏄惁涓虹涓�椤�
private boolean isFirstPage = false;
//鏄惁涓烘渶鍚庝竴椤�
private boolean isLastPage = false;
//鏄惁鏈夊墠涓�椤�
private boolean hasPreviousPage = false;
//鏄惁鏈変笅涓�椤�
private boolean hasNextPage = false;
//瀵艰埅椤电爜鏁�
private int navigatePages;
//鎵�鏈夊鑸〉鍙�
private int[] navigatepageNums; public PageInfo() {
} /**
* 鍖呰Page瀵硅薄
*
* @param list
*/
public PageInfo(List<T> list) {
this(list, 8);
} /**
* 鍖呰Page瀵硅薄
*
* @param list page缁撴灉
* @param navigatePages 椤电爜鏁伴噺
*/
public PageInfo(List<T> list, int navigatePages) {
if (list instanceof Page) {
Page page = (Page) list;
this.pageNum = page.getPageNum();
this.pageSize = page.getPageSize(); this.total = page.getTotal();
this.pages = page.getPages();
this.list = page;
this.size = page.size();
//鐢变簬缁撴灉鏄�>startRow鐨勶紝鎵�浠ュ疄闄呯殑闇�瑕�+1
if (this.size == 0) {
this.startRow = 0;
this.endRow = 0;
} else {
this.startRow = page.getStartRow() + 1;
//璁$畻瀹為檯鐨別ndRow锛堟渶鍚庝竴椤电殑鏃跺�欑壒娈婏級
this.endRow = this.startRow - 1 + this.size;
}
this.navigatePages = navigatePages;
//璁$畻瀵艰埅椤�
calcNavigatepageNums();
//璁$畻鍓嶅悗椤碉紝绗竴椤碉紝鏈�鍚庝竴椤�
calcPage();
//鍒ゆ柇椤甸潰杈圭晫
judgePageBoudary();
}
} /**
* 璁$畻瀵艰埅椤�
*/
private void calcNavigatepageNums() {
//褰撴�婚〉鏁板皬浜庢垨绛変簬瀵艰埅椤电爜鏁版椂
if (pages <= navigatePages) {
navigatepageNums = new int[pages];
for (int i = 0; i < pages; i++) {
navigatepageNums[i] = i + 1;
}
} else { //褰撴�婚〉鏁板ぇ浜庡鑸〉鐮佹暟鏃�
navigatepageNums = new int[navigatePages];
int startNum = pageNum - navigatePages / 2;
int endNum = pageNum + navigatePages / 2; if (startNum < 1) {
startNum = 1;
//(鏈�鍓峮avigatePages椤�
for (int i = 0; i < navigatePages; i++) {
navigatepageNums[i] = startNum++;
}
} else if (endNum > pages) {
endNum = pages;
//鏈�鍚巒avigatePages椤�
for (int i = navigatePages - 1; i >= 0; i--) {
navigatepageNums[i] = endNum--;
}
} else {
//鎵�鏈変腑闂撮〉
for (int i = 0; i < navigatePages; i++) {
navigatepageNums[i] = startNum++;
}
}
}
} /**
* 璁$畻鍓嶅悗椤碉紝绗竴椤碉紝鏈�鍚庝竴椤�
*/
private void calcPage() {
if (navigatepageNums != null && navigatepageNums.length > 0) {
firstPage = navigatepageNums[0];
lastPage = navigatepageNums[navigatepageNums.length - 1];
if (pageNum > 1) {
prePage = pageNum - 1;
}
if (pageNum < pages) {
nextPage = pageNum + 1;
}
}
} /**
* 鍒ゅ畾椤甸潰杈圭晫
*/
private void judgePageBoudary() {
isFirstPage = pageNum == 1;
isLastPage = pageNum == pages;
hasPreviousPage = pageNum > 1;
hasNextPage = pageNum < pages;
} public int getPageNum() {
return pageNum;
} public void setPageNum(int pageNum) {
this.pageNum = pageNum;
} public int getPageSize() {
return pageSize;
} public void setPageSize(int pageSize) {
this.pageSize = pageSize;
} public int getSize() {
return size;
} public void setSize(int size) {
this.size = size;
} public int getStartRow() {
return startRow;
} public void setStartRow(int startRow) {
this.startRow = startRow;
} public int getEndRow() {
return endRow;
} public void setEndRow(int endRow) {
this.endRow = endRow;
} public long getTotal() {
return total;
} public void setTotal(long total) {
this.total = total;
} public int getPages() {
return pages;
} public void setPages(int pages) {
this.pages = pages;
} public List<T> getList() {
return list;
} public void setList(List<T> list) {
this.list = list;
} public int getFirstPage() {
return firstPage;
} public void setFirstPage(int firstPage) {
this.firstPage = firstPage;
} public int getPrePage() {
return prePage;
} public void setPrePage(int prePage) {
this.prePage = prePage;
} public int getNextPage() {
return nextPage;
} public void setNextPage(int nextPage) {
this.nextPage = nextPage;
} public int getLastPage() {
return lastPage;
} public void setLastPage(int lastPage) {
this.lastPage = lastPage;
} public boolean isIsFirstPage() {
return isFirstPage;
} public void setIsFirstPage(boolean isFirstPage) {
this.isFirstPage = isFirstPage;
} public boolean isIsLastPage() {
return isLastPage;
} public void setIsLastPage(boolean isLastPage) {
this.isLastPage = isLastPage;
} public boolean isHasPreviousPage() {
return hasPreviousPage;
} public void setHasPreviousPage(boolean hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
} public boolean isHasNextPage() {
return hasNextPage;
} public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
} public int getNavigatePages() {
return navigatePages;
} public void setNavigatePages(int navigatePages) {
this.navigatePages = navigatePages;
} public int[] getNavigatepageNums() {
return navigatepageNums;
} public void setNavigatepageNums(int[] navigatepageNums) {
this.navigatepageNums = navigatepageNums;
} @Override
public String toString() {
final StringBuffer sb = new StringBuffer("PageInfo{");
sb.append("pageNum=").append(pageNum);
sb.append(", pageSize=").append(pageSize);
sb.append(", size=").append(size);
sb.append(", startRow=").append(startRow);
sb.append(", endRow=").append(endRow);
sb.append(", total=").append(total);
sb.append(", pages=").append(pages);
sb.append(", list=").append(list);
sb.append(", firstPage=").append(firstPage);
sb.append(", prePage=").append(prePage);
sb.append(", nextPage=").append(nextPage);
sb.append(", lastPage=").append(lastPage);
sb.append(", isFirstPage=").append(isFirstPage);
sb.append(", isLastPage=").append(isLastPage);
sb.append(", hasPreviousPage=").append(hasPreviousPage);
sb.append(", hasNextPage=").append(hasNextPage);
sb.append(", navigatePages=").append(navigatePages);
sb.append(", navigatepageNums=");
if (navigatepageNums == null) sb.append("null");
else {
sb.append('[');
for (int i = 0; i < navigatepageNums.length; ++i)
sb.append(i == 0 ? "" : ", ").append(navigatepageNums[i]);
sb.append(']');
}
sb.append('}');
return sb.toString();
}
}
package com.teewon.viewservice.entitiy.base;

import java.util.ArrayList;
import java.util.List; import org.apache.ibatis.session.RowBounds; public class Page<E> extends ArrayList<E> {
private static final long serialVersionUID = 1L; private static final int NO_SQL_COUNT = -1;
private static final int SQL_COUNT = 0;
private int pageNum;
private int pageSize;
private int startRow;
private int endRow;
private long total;
private int pages;
private Boolean reasonable;
private Boolean pageSizeZero; public Page() {
super();
} public Page(int pageNum, int pageSize) {
this(pageNum, pageSize, SQL_COUNT, null);
} public Page(int pageNum, int pageSize, boolean count) {
this(pageNum, pageSize, count ? Page.SQL_COUNT : Page.NO_SQL_COUNT, null);
} private Page(int pageNum, int pageSize, int total, Boolean reasonable) {
super(0);
if (pageNum == 1 && pageSize == Integer.MAX_VALUE) {
pageSizeZero = true;
pageSize = 0;
}
this.pageNum = pageNum;
this.pageSize = pageSize;
this.total = total;
calculateStartAndEndRow();
setReasonable(reasonable);
} public Page(RowBounds rowBounds, boolean count) {
this(rowBounds, count ? Page.SQL_COUNT : Page.NO_SQL_COUNT);
} public Page(RowBounds rowBounds, int total) {
super(0);
if (rowBounds.getOffset() == 0 && rowBounds.getLimit() == Integer.MAX_VALUE) {
pageSizeZero = true;
this.pageSize = 0;
} else {
this.pageSize = rowBounds.getLimit();
}
this.startRow = rowBounds.getOffset();
//RowBounds鏂瑰紡榛樿涓嶆眰count鎬绘暟锛屽鏋滄兂姹俢ount,鍙互淇敼杩欓噷涓篠QL_COUNT
this.total = total;
this.endRow = this.startRow + rowBounds.getLimit();
} public List<E> getResult() {
return this;
} public int getPages() {
return pages;
} public int getEndRow() {
return endRow;
} public int getPageNum() {
return pageNum;
} public void setPageNum(int pageNum) {
//鍒嗛〉鍚堢悊鍖栵紝閽堝涓嶅悎鐞嗙殑椤电爜鑷姩澶勭悊
this.pageNum = ((reasonable != null && reasonable) && pageNum <= 0) ? 1 : pageNum;
} public int getPageSize() {
return pageSize;
} public void setPageSize(int pageSize) {
this.pageSize = pageSize;
} public int getStartRow() {
return startRow;
} public long getTotal() {
return total;
} public void setTotal(long total) {
this.total = total;
if (pageSize > 0) {
pages = (int) (total / pageSize + ((total % pageSize == 0) ? 0 : 1));
} else {
pages = 0;
}
//鍒嗛〉鍚堢悊鍖栵紝閽堝涓嶅悎鐞嗙殑椤电爜鑷姩澶勭悊
if ((reasonable != null && reasonable) && pageNum > pages) {
pageNum = pages;
calculateStartAndEndRow();
}
} public void setReasonable(Boolean reasonable) {
if (reasonable == null) {
return;
}
this.reasonable = reasonable;
//鍒嗛〉鍚堢悊鍖栵紝閽堝涓嶅悎鐞嗙殑椤电爜鑷姩澶勭悊
if (this.reasonable && this.pageNum <= 0) {
this.pageNum = 1;
calculateStartAndEndRow();
}
} public Boolean getReasonable() {
return reasonable;
} public Boolean getPageSizeZero() {
return pageSizeZero;
} public void setPageSizeZero(Boolean pageSizeZero) {
if (pageSizeZero != null) {
this.pageSizeZero = pageSizeZero;
}
} /**
* 璁$畻璧锋琛屽彿
*/
private void calculateStartAndEndRow() {
this.startRow = this.pageNum > 0 ? (this.pageNum - 1) * this.pageSize : 0;
this.endRow = this.startRow + this.pageSize * (this.pageNum > 0 ? 1 : 0);
} public boolean isCount() {
return this.total > NO_SQL_COUNT;
} //澧炲姞閾惧紡璋冪敤鏂规硶 /**
* 璁剧疆椤电爜
*
* @param pageNum
* @return
*/
public Page<E> pageNum(int pageNum) {
//鍒嗛〉鍚堢悊鍖栵紝閽堝涓嶅悎鐞嗙殑椤电爜鑷姩澶勭悊
this.pageNum = ((reasonable != null && reasonable) && pageNum <= 0) ? 1 : pageNum;
return this;
} /**
* 璁剧疆椤甸潰澶у皬
*
* @param pageSize
* @return
*/
public Page<E> pageSize(int pageSize) {
this.pageSize = pageSize;
calculateStartAndEndRow();
return this;
} /**
* 鏄惁鎵цcount鏌ヨ
*
* @param count
* @return
*/
public Page<E> count(Boolean count) {
this.total = count ? Page.SQL_COUNT : Page.NO_SQL_COUNT;
return this;
} /**
* 璁剧疆鍚堢悊鍖�
*
* @param reasonable
* @return
*/
public Page<E> reasonable(Boolean reasonable) {
setReasonable(reasonable);
return this;
} /**
* @param pageSizeZero
* @return
*/
public Page<E> pageSizeZero(Boolean pageSizeZero) {
setPageSizeZero(pageSizeZero);
return this;
} @Override
public String toString() {
final StringBuffer sb = new StringBuffer("Page{");
sb.append("pageNum=").append(pageNum);
sb.append(", pageSize=").append(pageSize);
sb.append(", startRow=").append(startRow);
sb.append(", endRow=").append(endRow);
sb.append(", total=").append(total);
sb.append(", pages=").append(pages);
sb.append(", reasonable=").append(reasonable);
sb.append(", pageSizeZero=").append(pageSizeZero);
sb.append('}');
return sb.toString();
}
}

示例:

  @Override
@ResponseBody
public Response<Behavior> getLessonInfo(@RequestBody GetBehaviorReq behavior)
{
if(null == behavior || null == behavior.getType())
{
ServerResult serverResult = new ServerResult();
serverResult.setResultCode("-1");
serverResult.setResultMessage("请求参数为空,请检查");
Response<Behavior> response = new Response<Behavior>();
response.setServerResult(serverResult);
return response;
}
List<Behavior> infoList = behaviorService.getLessonInfo(behavior);
Response<Behavior> response = new Response<Behavior>(infoList);
return response;
}