JSON(JavaScript Object Notation, JS对象简谱)是一种轻量级的数据交换格式。它基于 ECMAScript(European Computer Manufacturers Association, 欧洲计算机协会制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。
package com.attendance.common;
import com.github.pagehelper.PageInfo;
import lombok.Data;
import java.util.List;
@Data
public class CommonPage<T> {
//当前页
private int pageNum;
//每页的数量
private int pageSize;
//当前页的数量
private int size;
//总记录数
private long total;
//总页数
private int pages;
//是否为第一页
private boolean isFirstPage = false;
//是否为最后一页
private boolean isLastPage = false;
//是否有前一页
private boolean hasPreviousPage = false;
//是否有下一页
private boolean hasNextPage = false;
//结果集
private List<T> list;
public static <T> CommonPage<T> restPage(List<T> list) {
CommonPage<T> result = new CommonPage();
PageInfo<T> pageInfo = new PageInfo(list);
result.setPages(pageInfo.getPages());
result.setPageNum(pageInfo.getPageNum());
result.setPageSize(pageInfo.getPageSize());
result.setSize(pageInfo.getSize());
result.setTotal(pageInfo.getTotal());
result.setFirstPage(pageInfo.isIsFirstPage());
result.setHasNextPage(pageInfo.isHasNextPage());
result.setHasPreviousPage(pageInfo.isHasPreviousPage());
result.setLastPage(pageInfo.isIsLastPage());
result.setList(pageInfo.getList());
return result;
}
}
// 根据条件查学生信息
@Override
public CommonResult selectStudent(Student student, Pages pages) {
PageHelper.startPage(pages.getPage(),pages.getNum());
return CommonResult.success(CommonPage.restPage(studentMapper.selectStudent(student)));
}
package com.attendance.pojo;
import lombok.Data;
@Data
public class Pages {
private int page = 1;//第几页
private int num = 5;//每页几条数据
}
package com.attendance.common;
import lombok.Data;
@Data
public class CommonResult<T> {
private long code;
private String message;
private boolean flag;
private T data;
protected CommonResult() {
}
protected CommonResult(long code, boolean flag, String message, T data) {
this.code = code;
this.flag = flag;
this.message = message;
this.data = data;
}
public CommonResult(long code, boolean flag, String message) {
this.code = code;
this.flag = flag;
this.message = message;
}
public static <T> CommonResult<T> success(T data) {
return new CommonResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getFlag(), ResultCode.SUCCESS.getMessage(), data);
}
public static <T> CommonResult<T> success(T data, String message) {
return new CommonResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getFlag(), message, data);
}
public static <T> CommonResult<T> success(String message) {
return new CommonResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getFlag(), message);
}
public static <T> CommonResult<T> failed(IErrorCode errorCode) {
return new CommonResult(errorCode.getCode(), errorCode.getFlag(), errorCode.getMessage(), (Object)null);
}
public static <T> CommonResult<T> failed(IErrorCode errorCode, String message) {
return new CommonResult(errorCode.getCode(), errorCode.getFlag(), message, (Object)null);
}
public static <T> CommonResult<T> failed(String message) {
return new CommonResult(ResultCode.FAILED.getCode(), ResultCode.FAILED.getFlag(), message, (Object)null);
}
public static <T> CommonResult<T> failed() {
return failed((IErrorCode)ResultCode.FAILED);
}
public static <T> CommonResult<T> validateFailed() {
return failed((IErrorCode)ResultCode.VALIDATE_FAILED);
}
public static <T> CommonResult<T> validateFailed(String message) {
return new CommonResult(ResultCode.VALIDATE_FAILED.getCode(), ResultCode.VALIDATE_FAILED.getFlag(), message, (Object)null);
}
public static <T> CommonResult<T> unauthorized(T data) {
return new CommonResult(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getFlag(), ResultCode.UNAUTHORIZED.getMessage(), data);
}
public static <T> CommonResult<T> forbidden(T data) {
return new CommonResult(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getFlag(), ResultCode.FORBIDDEN.getMessage(), data);
}
}
package com.attendance.common;
public interface IErrorCode {
long getCode();
boolean getFlag();
String getMessage();
}
package com.attendance.common;
public enum ResultCode implements IErrorCode {
SUCCESS(200L, true, "操作成功"),
FAILED(500L, false, "操作失败"),
VALIDATE_FAILED(404L, false, "参数检验失败"),
UNAUTHORIZED(401L, false, "暂未登录或token已经过期"),
FORBIDDEN(403L, false, "没有相关权限");
private long code;
private boolean flag;
private String message;
private ResultCode(long code, boolean flag, String message) {
this.code = code;
this.flag = flag;
this.message = message;
}
public long getCode() {
return this.code;
}
public boolean getFlag() {
return this.flag;
}
public String getMessage() {
return this.message;
}
}
就是让方法的返回值变成CommonResult
// 新增学生信息
@Override
public CommonResult selectStudentById(int id) {
Student student = studentMapper.selectStudentById(id);
return CommonResult.success(student,"查询成功!");
}
// 更改学生状态
@Override
public CommonResult updateStudentInfo(Student student) {
int i = studentMapper.updateStudentInfo(student);
if (i==1){
return CommonResult.success("修改信息成功!");
}else {
return CommonResult.failed("修改数据失败!");
}
}
// 更新学生基本信息
@PostMapping("/updateStudentInfo")
public CommonResult updateStudentInfo(@RequestBody Student student){
return studentService.updateStudentInfo(student);
}
// 更改学生状态
@PostMapping("/modifyStudentStatus")
public CommonResult modifyStudentStatus(@RequestBody Student student){
return studentService.modifyStudentStatus(student);
}
{
"code": 0,
"message": "查询成功!",
"flag": true,
"data": [
{
"id": 1,
"stuName": "苏文绩",
"stuNative": "重庆市铜梁区",
"stuSex": "男",
"stuPhone1": "17782299402",
"stuPhone2": "19112799705",
"classType": "文科",
"stuIdentity": "500224200012050053",
"stuAddress": "重庆市铜梁区东城街道玉泉花苑5-12-3",
"specialType": "单亲家庭",
"stuScore": "502",
"stuMajor1": "软件工程",
"stuMajor2": "计算机科学与技术",
"status": 0,
"contactContent": "",
"version": 1,
"createTime": "2022-06-23T09:50:26.000+00:00",
"updateTime": "2022-06-27T03:55:51.000+00:00",
"deleted": false
},
{
"id": 2,
"stuName": "谢汝雄",
"stuNative": "重庆市江津区",
"stuSex": "男",
"stuPhone1": "17353192303",
"stuPhone2": "11651561651",
"classType": "文科",
"stuIdentity": "500244200007031125",
"stuAddress": "重庆市江津区",
"specialType": "孤儿",
"stuScore": "600",
"stuMajor1": "自动化",
"stuMajor2": "艺术与科技",
"status": 1,
"contactContent": "adasd",
"version": 1,
"createTime": "2022-06-23T09:52:18.000+00:00",
"updateTime": "2022-06-27T04:42:21.000+00:00",
"deleted": false
},
{
"id": 3,
"stuName": "张幸",
"stuNative": "重庆市万州区",
"stuSex": "男",
"stuPhone1": "173151515",
"stuPhone2": "12.1.21.1.15.15",
"classType": "理科",
"stuIdentity": "56156165165",
"stuAddress": "重庆市万州区",
"specialType": "孤儿,优扶家庭子女,家庭突发重大变故",
"stuScore": "504",
"stuMajor1": "计算机科学与技术",
"stuMajor2": "艺术与科技",
"status": 0,
"contactContent": "",
"version": 1,
"createTime": "2022-06-23T09:53:06.000+00:00",
"updateTime": "2022-06-26T08:47:18.000+00:00",
"deleted": false
},
{
"id": 4,
"stuName": "赵云",
"stuNative": "124124",
"stuSex": "男",
"stuPhone1": "12124124",
"stuPhone2": "12512512",
"classType": "1",
"stuIdentity": "124124",
"stuAddress": "123123123",
"specialType": "孤儿,烈士子女,优扶家庭子女,建档立卡贫困家庭学生",
"stuScore": "124124",
"stuMajor1": "通信工程",
"stuMajor2": "自动化",
"status": 0,
"contactContent": "",
"version": 1,
"createTime": "2022-06-23T10:29:57.000+00:00",
"updateTime": "2022-06-26T08:52:32.000+00:00",
"deleted": false
},
{
"id": 5,
"stuName": "张三",
"stuNative": "重庆",
"stuSex": "男",
"stuPhone1": "12124",
"stuPhone2": "12412",
"classType": "1",
"stuIdentity": "500224",
"stuAddress": "124125125",
"specialType": "孤儿,烈士子女,优扶家庭子女,建档立卡贫困家庭学生",
"stuScore": "654",
"stuMajor1": "市场营销",
"stuMajor2": "信息工程",
"status": 0,
"contactContent": "",
"version": 1,
"createTime": "2022-06-23T11:02:43.000+00:00",
"updateTime": "2022-06-26T09:18:58.000+00:00",
"deleted": false
}
],
"count": 21
}
package com.zdsoft.datafactory.common;
import com.zdsoft.datafactory.common.enums.ResultCodeEnum;
/**
* 接口通用的返回对象。
* @author 李川东
* */
public final class R<T> {
private int code;
private String msg ;
private T data;
public R(){
}
public R(int code){
this.code = code;
this.msg = "";
this.data = null;
}
public R(int code,String msg){
this.code = code;
this.msg = msg;
this.data = null;
}
public R(int code,String msg,T data){
this.code = code;
this.msg = msg;
this.data = data;
}
public void setCode(int code) {
this.code = code;
}
public void setMsg(String msg) {
this.msg = msg;
}
public void setData(T data) {
this.data = data;
}
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
public T getData() {
return data;
}
public static R Success(Object data){
return new R(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage(),data);
}
public static R Success(String message,Object data){
return new R(ResultCodeEnum.SUCCESS.getCode(), message,data);
}
public static R Success(){
return Success("");
}
public static R Failed(String msg){
return new R(ResultCodeEnum.SYSTEM_EXCEPTION.getCode(),msg);
}
public static R Failed(){
return Failed("Failed");
}
public static R Failed(int code,String msg){
return new R(code,msg);
}
public boolean succeeded(){
return getCode() == ResultCodeEnum.SUCCESS.getCode();
}
public boolean failed(){
return getCode() != ResultCodeEnum.SUCCESS.getCode();
}
}
package com.zdsoft.datafactory.common.enums;
import com.zdsoft.datafactory.common.R;
import java.util.ArrayList;
import java.util.List;
/**
* @Author lichuandong
* @Date 2021/1/5
*/
@SuppressWarnings("AlibabaEnumConstantsMustHaveComment")
public enum ResultCodeEnum {
SUCCESS(100200, "返回成功"),
SYSTEM_EXCEPTION(100500, "系统异常"),
REQUEST_PARAM_ERROR(100401,"请求参数错误"),
REQUEST_OUT_OVERTIME(100408,"请求超时"),
REQUEST_NOT_FOUND(100404,"请求的资源或服务未找到"),
REQUEST_LENGTH_LIMIT(100414,"请求URI太长"),
REQUEST_Format_NOT_SUPPORTED(100415,"请求的格式不支持"),
TOKEN_EXPIRED_REDIRECT(100302,"token过期,请重新登录"),
INTERFACE_BASIC_INFORMATION_NULL(100001, "接口基本信息为空"),
INTERFACE_NAME_NULL(100002, "接口名称为空"),
INTERFACE_SOURCE_NULL(100005, "接口来源为空"),
INTERFACE_PROTOCOL_NULL(100003, "接口协议为空"),
INTERFACE_NOT_ENABLED(100123, "接口未启用"),
INTERFACE_IP_PORT_NULL(100012, "ip和端口为空"),
INTERFACE_PATH_NULL(100004, "接口路径为空"),
INTERFACE_REQUEST_METHOD_NULL(100008, "接口请求方式"),
REQUEST_TIMEOUT(100014, "超时时限为空"),
REQUEST_PARAM_NULL(100012, "请求参数为空"),
INTERFACE_BASE_NOT_FOUND(100013, "未找到该条接口基本信息"),
INTERFACE_SETTING_NOT_FOUND(100015, "未找到该条接口配置信息"),
INTERFACE_BASE_EXIST(100026, "接口基本信息已存在"),
INTERFACE_BASE_NOT_EXIST(100025, "接口基本信息不存在"),
INTERFACE_SETTING_EXIST(100027, "接口配置信息已存在"),
INTERFACE_SETTING_NOT_EXIST(100028, "接口配置信息不存在"),
INTERFACE_PATH_ILLEGAL(100009, "接口路径不符合规范"),
REQUIRED_FIELD_IS_BLANK(100007, "入参必填字段为空: "),
REQUEST_OUT_API_FAIL(100018, "请求外部接口失败,返回状态码:"),
REQUEST_OUT_GET_OVERTIME(100019, "GET请求外部接口失败,请求超时"),
REQUEST_OUT_POST_OVERTIME(100020, "POST请求外部接口失败,请求超时"),
REQUEST_OUT_PUT_OVERTIME(100021, "PUT请求外部接口失败,请求超时"),
REQUEST_OUT_DELETE_OVERTIME(100022, "DELETE请求外部接口失败,请求超时"),
OTHER_METHODS_NOT_SUPPORTED(100023, "现阶段不支持其他请求方式"),
PARAM_BODY_CONTENT_ERROR(100024, "请求body不是json格式"),
SPLICING_PARAM_IS_FAIL(100031, "拼接GET请求入参出错,请检查参数"),
;
/**
* 枚举值
*/
private final Integer code;
/**
* 枚举描述
*/
private final String message;
/**
* 构造一个LocalCacheEnum
枚举对象
*
* @param code 枚举值
* @param message 枚举描述
*/
ResultCodeEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
public Integer getCode() {
return code;
}
public String getMessage() {
return message;
}
public <T> R<T> result(){
return new R(getCode(),getMessage());
}
public <T> R<T> result(String message){
return new R(getCode(),message);
}
/**将返回码标准的信息 填充到message里面,message必须包含一个%s*/
public <T> R<T> resultFillingMessage(String message){
return new R(getCode(),String.format(message,getMessage()));
}
/**将message添加到返回码标准的信息后面 再返回{@link R}*/
public <T> R<T> resultAppendMessage(String message){
return new R(getCode(),getMessage()+message);
}
/**
* 获取全部枚举
*
* @return List
*/
public static List<ResultCodeEnum> getAllEnum() {
List<ResultCodeEnum> list = new ArrayList<>();
for (ResultCodeEnum each : values()) {
list.add(each);
}
return list;
}
/**
* 获取全部枚举值
*
* @return List
*/
public static List<Integer> getAllEnumCode() {
List<Integer> list = new ArrayList<>();
for (ResultCodeEnum each : values()) {
list.add(each.code);
}
return list;
}
}
public R addstopwatch(@RequestBody AddStopWatchVo addStopWatchVo){
try {
if (!IsChineseUtil.isLetterChinese(addStopWatchVo.getsName())){
return R.Failed("码表名称只能为中文或者英文");
}
Boolean flag = iStopWatchValueService.JudgeStopWatchValue(addStopWatchVo.getStopWatchValues());
if (flag){
StopWatch stopWatch = iStopWatchService.AddStopWatch(addStopWatchVo.getsName(), addStopWatchVo.getsComment());
Long id = stopWatch.getsId();
iStopWatchValueService.AddStopWatchValue(addStopWatchVo.getStopWatchValues(),id);
return R.Success();
}else return R.Failed("码值不能重复");
}catch (Exception e){
return R.Failed("码表名称不能重复");
}
}
{
"code": 100200,
"msg": "返回成功",
"data": ""
}