——若发现文章内容有误,敬请指正,望不吝赐教,感谢!
以往记录
Web项目实战 | 购物系统v2.0 | 开发记录(一)需求分析 | 技术选型 | 系统设计 | 数据表设计 | SpringBoot、SSM、Thymeleaf、Bootstrap…
Web项目实战 | 购物系统v2.0 | 开发记录(二)搭建SpringBoot+SSM框架环境 | 配置Druid+MyBatis | 基于Bootstrap实现登陆页面| 图片验证码接口
Web项目实战 | 购物系统v2.0 | 开发记录(四)单个页面单个请求解决根据商品类型进行分页查询 | 使用省市区三级联动 | 使用JQuery 插件实现图片上传
Web项目实战 | 购物系统v2.0 | 开发记录(五)| 使用base64编码实现头像修改 | 用户个人信息修改 | JQuery动态提示
Web项目实战 | 购物系统v2.0 | 开发记录(六)商品详情页面 | 游客访问主页 | 运用Bootstrap4轻量级弹窗实现提示
Web项目实战 | 购物系统v2.0 | 开发记录(七)SpringBoot整合Shiro框架进行身份认证 | Shiro 加盐(MD5+Salt)验证登陆 | 数据表结构优化避免外键+设置中间表
Web项目实战 | 购物系统v2.0 | 开发记录(八)前后端分离初步思路 | JS处理URL参数实用函数 | AJAX 向后台传递Map类型数据 | MyBatis多表查询优化
运行环境
windows10
IDEA 2021.3 专业版
JDK8
SpringBoot2.6.x
JQuery 3.x
Druid 1.2.5
Bootstrap 4.6.0
MySQL 8
Navicat 15
一、设计Bean用于Controller层返回数据
在之前的学习中用到了伪前后端分离的思路,前端通过像后端AJAX请求获取数据,然后再渲染数据到页面,在数据之间传递时通常需要对数据类型进行序列化,为了方便Controller层返回数据,这里使用泛型进行设计,设计实体类RestResponse,用于返回结果,同时设置一个支持返回JSON字符串的方法,toJSONString()
package com.uni.base;
import com.alibaba.fastjson.JSON;
/*
* @Description: 自定义响应数据结构
*/
public class RestResponse<T> {
private int code;
private String message;
private T response;
public RestResponse(int code, String message) {
this.code = code;
this.message = message;
}
public RestResponse(int code, String message, T response) {
this.code = code;
this.message = message;
this.response = response;
}
public static RestResponse fail(Integer code, String msg) {
return new RestResponse<>(code, msg);
}
public static RestResponse fail(String msg) {
return new RestResponse<>(-1, msg);
}
public static RestResponse ok() {
SystemCodeEnum systemCodeEnum = SystemCodeEnum.OK;
return new RestResponse<>(systemCodeEnum.getCode(), systemCodeEnum.getMessage());
}
public static RestResponse ok(String message) {
SystemCodeEnum systemCodeEnum = SystemCodeEnum.OK;
return new RestResponse<>(systemCodeEnum.getCode(), message);
}
/**
* @param <F> the type parameter
* @param response the response
* @return the rest response
*/
public static <F> RestResponse<F> ok(F response) {
SystemCodeEnum systemCodeEnum = SystemCodeEnum.OK;
return new RestResponse<>(systemCodeEnum.getCode(), systemCodeEnum.getMessage(), response);
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getResponse() {
return response;
}
public void setResponse(T response) {
this.response = response;
}
@Override
public String toString() {
return "RestResponse{" +
"code=" + code +
", message='" + message + '\'' +
", response=" + response +
'}';
}
public String toJSONString(){
return JSON.toJSONString(this);
}
}
在返回消息时,有许多是比较关键的信息,比如返回的状态、返回的信息等,所以可以设计一个枚举类,来表示返回的状态,以及对应的信息
package com.uni.base;
public enum SystemCodeEnum {
OK(1, "成功"),
AccessTokenError(400, "用户登录令牌失效"),
UNAUTHORIZED(401, "用户未登录"),
AuthError(402, "用户名或密码错误"),
InnerError(500, "系统内部错误"),
ParameterValidError(501, "参数验证错误"),
AccessDenied(502, "用户没有权限访问");
int code;
String message;
SystemCodeEnum(