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 基础响应体定义 - 通用的更多相关文章

  1. Java基础-变量的定义以及作用域详解

    Java基础-变量的定义以及作用域详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.字面量 常量(字面量)表示不能改变的数值(程序中直接出现的值).字面量有时也称为直接量,包 ...

  2. 《Java基础——数组的定义与使用》

    Java基础--数组的定义与使用       一. 一维数组: 格式一: 数组类型 数组变量[]=new 数据类型[长度]; //需要后续赋值,且后续赋值时只能为单个元素赋值. 或 数组类型 数组变量 ...

  3. java基础:方法的定义和调用详细介绍,方法同时获取数组最大值和最小值,比较两个数组,数组交换最大最小值,附练习案列

    1. 方法概述 1.1 方法的概念 方法(method)是将具有独立功能的代码块组织成为一个整体,使其具有特殊功能的代码集 注意: 方法必须先创建才可以使用,该过程成为方法定义 方法创建后并不是直接可 ...

  4. 2.Java基础:方法定义和调用

    一.方法的定义 1.类的方法代表的是实力的某种行为(或功能) 方法的定义:方法类型 . 方法签名 . 方法体 2.定义类的方法 访问修饰符 类型 方法名(参数列表){ ///方法体 } 3.把方法当作 ...

  5. Java基础--数组的定义

    1.数组的定义 数组:一组能够储存相同数据类型值的变量的集合. 2.数组的赋值方式 (1)使用默认的初始值来初始化数组中的每一个元素 语法:数组元素类型[]数组名 = new数组元素类型[数组中元素的 ...

  6. Java基础 - 变量的定义和使用

    变量定义 public class Main { public static void main(String[] args) { // 定义byte类型的变量 byte b = 10; System ...

  7. Java基础--方法的定义

    1.为什么要有方法? 方法(又叫函数)就是一段特定功能的代码块.方法提高程序的复用性和可读性. 比如,有了方法,我们可以把要重复使用的一段代码提炼出来,然后在每个需要执行这段代码的地方去调用即可. 2 ...

  8. java基础-2

    java基础-2 面向对象 定义 面向对象是一种思维方式,相对于面向过程​面向过程注重流程中的每一步,清楚流程中的每一个细节​面向对象注重的是对象,有了对象就有对象的一届​自己动手做--面向过程,找其 ...

  9. JAVA基础之HttpServletResponse响应

    用户在客户端输入网址(虚拟路径)时,开始发送一个HTTP请求(请求行.请求头.请求体)至服务器.服务器内的Tomcat引擎会解析请求的地址,去找XML文件,然后根据虚拟路径找Servlet的真实路径, ...

随机推荐

  1. Netty 系列四(ChannelHandler 和 ChannelPipeline).

    一.概念 先来整体的介绍一下这篇博文要介绍的几个概念(Channel.ChannelHandler.ChannelPipeline.ChannelHandlerContext.ChannelPromi ...

  2. 新浪微博登陆以及发送微博(附python源码)

    原文链接(本人):https://blog.csdn.net/A5878989/article/details/76275855 说明 本文主要记录分析新浪微博登陆以及发送文字和图片微博的详细过程 分 ...

  3. jquery网页日历显示控件calendar3.1使用详解

    关于日历插件,我做了好多次尝试,一直致力于开发一款简单易用的日历控件.我的想法是争取在引用这个控件后,用一行js代码就能做出一个日历,若在加点参数,就能自定义外观和功能丰富多彩的日历.Calendar ...

  4. CSS border-collapse 属性

    表格边框合并: table {  border-collapse:collapse; 边框会合并为一个单一的边框 } ----------------------------------------- ...

  5. 2018-08-13 中文编程讨论组(GitHub)社区守则一周年修订

    原址在此 社区守则 大原则 求同存异 就事论事 己所不欲勿施于人 注: 在讨论组成立一周年之际, 对行为规范进行一些细化 内 这部分所有内容同样适用于对外 回归技术 所有与中文编程没有直接关系的话题都 ...

  6. MongoDB 通过配置文件启动及注册服务

    1.配置mongodb环境变量,配置完成之后就可以直接执行mong.mongod等常用命令,不用每次都到mongodb安装目录bin下去执行: 2.通过命令启动mongo服务 mongod --dbp ...

  7. html之input标签(11)

    1.输入框 type=“text” 就是一个简单的输入框 <body> <input type="text"> </body> 2.密码输入框 ...

  8. iOS----------导航栏的正确隐藏方式

    第一种做法 -注意这里一定要用动画的方式隐藏导航栏,这样在使用滑动返回手势的时候效果最好,和上面动图一致.这样做有一个缺点就是在切换tabBar的时候有一个导航栏向上消失的动画. - (void)vi ...

  9. 自定义View的三个构造函数

    自定义View有三个构造方法,它们的作用是不同的. public MyView(Context context) { super(context); } public MyView(Context c ...

  10. ngx-moment汉化

    1.导入汉化文件 import '../../../node_modules/moment/locale/zh-cn.js' 2.使用汉化 <span>{{item.time|amLoca ...