java 基础响应体定义 - 通用
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;
}
java 基础响应体定义 - 通用的更多相关文章
- Java基础-变量的定义以及作用域详解
Java基础-变量的定义以及作用域详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.字面量 常量(字面量)表示不能改变的数值(程序中直接出现的值).字面量有时也称为直接量,包 ...
- 《Java基础——数组的定义与使用》
Java基础--数组的定义与使用 一. 一维数组: 格式一: 数组类型 数组变量[]=new 数据类型[长度]; //需要后续赋值,且后续赋值时只能为单个元素赋值. 或 数组类型 数组变量 ...
- java基础:方法的定义和调用详细介绍,方法同时获取数组最大值和最小值,比较两个数组,数组交换最大最小值,附练习案列
1. 方法概述 1.1 方法的概念 方法(method)是将具有独立功能的代码块组织成为一个整体,使其具有特殊功能的代码集 注意: 方法必须先创建才可以使用,该过程成为方法定义 方法创建后并不是直接可 ...
- 2.Java基础:方法定义和调用
一.方法的定义 1.类的方法代表的是实力的某种行为(或功能) 方法的定义:方法类型 . 方法签名 . 方法体 2.定义类的方法 访问修饰符 类型 方法名(参数列表){ ///方法体 } 3.把方法当作 ...
- Java基础--数组的定义
1.数组的定义 数组:一组能够储存相同数据类型值的变量的集合. 2.数组的赋值方式 (1)使用默认的初始值来初始化数组中的每一个元素 语法:数组元素类型[]数组名 = new数组元素类型[数组中元素的 ...
- Java基础 - 变量的定义和使用
变量定义 public class Main { public static void main(String[] args) { // 定义byte类型的变量 byte b = 10; System ...
- Java基础--方法的定义
1.为什么要有方法? 方法(又叫函数)就是一段特定功能的代码块.方法提高程序的复用性和可读性. 比如,有了方法,我们可以把要重复使用的一段代码提炼出来,然后在每个需要执行这段代码的地方去调用即可. 2 ...
- java基础-2
java基础-2 面向对象 定义 面向对象是一种思维方式,相对于面向过程面向过程注重流程中的每一步,清楚流程中的每一个细节面向对象注重的是对象,有了对象就有对象的一届自己动手做--面向过程,找其 ...
- JAVA基础之HttpServletResponse响应
用户在客户端输入网址(虚拟路径)时,开始发送一个HTTP请求(请求行.请求头.请求体)至服务器.服务器内的Tomcat引擎会解析请求的地址,去找XML文件,然后根据虚拟路径找Servlet的真实路径, ...
随机推荐
- 三、HTTP基础路由详解
1.一次请求的完整实现过程 2.基础路由 Route::get($uri,$callback); Route::post($uri,$callback); Route::put($uri,$callb ...
- Docker compose 调用外部文件及指定hosts 例子
cat docker-compose.yml version: '3.4' services: klvchen: image: ${IMAGE_NAME} restart: always # dock ...
- 小tips:JS之break,continue和return这三个语句的用法
break语句 break语句会使运行的程序立刻退出包含在最内层的循环或者退出一个switch语句.由于它是用来退出循环或者switch语句,所以只有当它出现在这些语句时,这种形式的break语句才是 ...
- 洛谷P2503 [HAOI2006]均分数据(模拟退火)
题目描述 已知N个正整数:A1.A2.…….An .今要将它们分成M组,使得各组数据的数值和最平均,即各组的均方差最小.均方差公式如下: 输入输出格式 输入格式: 输入文件data.in包括: 第一行 ...
- css不受高度限制实现文本超出隐藏并以省略号结束
文本超出省略号显示代码: overflow: hidden; text-overflow:ellipsis; white-space: nowrap;width: 100px; /*宽度做好限制*/ ...
- html之input标签(11)
1.输入框 type=“text” 就是一个简单的输入框 <body> <input type="text"> </body> 2.密码输入框 ...
- Salesforce 导入导出数据简介
导入数据的方式 有两种方式可以将数据导入Salesforce: 数据导入向导 Data Loader工具 Salesforce支持将csv文件中的数据导入系统. 数据导入向导 数据导入向导可以从设置界 ...
- <1>Python生成高质量Html文件:Pyh模块+Bootstrap框架
一,介绍 QQ交流群:585499566 本文的目的是怎么使用Pyh+Bootstrap快速生成简约,大方,清新的Html页面,涉及到的技能:Python小白技能,Pyh会阅读中文文档,Html基础, ...
- JHipster生成单体架构的应用示例
本文演示如何用JHipster生成一个单体架构风格的应用. 环境需求:安装好JHipster开发环境的CentOS 7.4(参考这里) 应用名:app1 实体名:role 主机IP:192.168.2 ...
- Cas 服务器 为Service定义UI
Cas服务器允许用户为每个Service自定义登陆等UI外观,但需要尊着Cas定于的规则: 一.为Service配置theme(主题) 在<Cas 服务器 Service(Cas客户端)注册信息 ...