1、导入maven插件依赖爆红,解决办法:在外部依赖项导入后再放进插件依赖
2、跨域问题:前端代码加入
xhrFields:{withCredentials:true},
crossDomain: true,
后端加入:
@CrossOrigin(origins = {originPatterns = {"*"},allowCredentials = "true",allowedHeaders = "*")
3、定义CommonReturnType返回给用户有意义的错误信息
package com.yp.responseType;
/**
* @Author: yp
* @Date: 2023/3/26 16:59
* 给前端返回有意义的错误信息
*/
public class CommonReturnType {
//
/**表明对应请求的处理结果是success或fail
* 如果status=success 则data内返回前端所需的Json数据
* 如果status=fail 则data内返回通用的错误码格式
*/
private String status;
private Object data;
public static CommonReturnType create(Object result){
return CommonReturnType.create(result,"success");
}
public static CommonReturnType create(Object result,String status){
CommonReturnType type=new CommonReturnType();
type.setData(result);
type.setStatus(status);
return type;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
4、定义exception handler处理未被controller层吸收的异常
public interface CommonError {
public int getErrCode();
public String getErrMsg();
public CommonError setErrorMsg(String errMsg);
}
package com.yp.error;
/**
* @Author: yp
* @Date: 2023/3/26 17:22
*/
public enum EmBusinessErr implements CommonError{
/**
* 错误码和对应信息
* 类型是enum
*有新的错误类型只需无限向下延申即可
*/
//通用错误类型00001
parameter_validation_error(10001,"参数不合法"),
//1000开头为用户相关信息错误
user_not_exist(20001,"用户不存在"),
user_login_fail(20002,"用户手机号或密码不正确"),
;
private int errCode;
private String errMsg;
EmBusinessErr(int errCode, String errMsg) {
this.errCode = errCode;
this.errMsg = errMsg;
}
@Override
public int getErrCode() {
return this.errCode;
}
@Override
public String getErrMsg() {
return this.errMsg;
}
@Override
public CommonError setErrorMsg(String errMsg) {
this.errMsg=errMsg;
return this;
}
}
package com.yp.error;
/**
* @Author: yp
* @Date: 2023/3/26 17:35
* 包装器业务异常类实现
*/
public class BusinessException extends Exception implements CommonError{
private CommonError commonError;
//直接接收EmBusinessErr类型的传参用于构造业务异常
public BusinessException(CommonError commonError){
super();
this.commonError=commonError;
}
public BusinessException(CommonError commonError,String errMsg){
super();
this.commonError=commonError;
this.commonError.setErrorMsg(errMsg);
}
@Override
public int getErrCode() {
return commonError.getErrCode();
}
@Override
public String getErrMsg() {
return commonError.getErrMsg();
}
@Override
public CommonError setErrorMsg(String errMsg) {
commonError.setErrorMsg(errMsg);
return this;
}
}
public class BaseController {
public static final String CONTENT_TYPE_FORMED="application/x-www-form-urlencoded";
/**
* 定义exceptionhandle来处理未被controller层吸收的异常 不让用户看到恶心的报错页面
*/
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Object handlerException(HttpServletRequest request, Exception exception){
BusinessException businessException=(BusinessException) exception;
Map<String,Object> responseData=new HashMap<>();
responseData.put("errCode",businessException.getErrCode());
responseData.put("errMsg",businessException.getErrMsg());
return CommonReturnType.create(responseData,"fail");
}
}
5、针对用户模型在代码中不同的侧写面,定义userDO(对数据库user表进行操作的对应类)、userModel(比userDO更具逻辑性,具有密码等userDO中没有的属性)、userVO(前端展示类)
private UserVo convertFromModel(UserModel userModel){
if(userModel==null){
return null;
}
UserVo userVo=new UserVo();
BeanUtils.copyProperties(userModel,userVo);
return userVo;
}
6、对数据库进行操作定义userService接口,userServiceImpl实现类中定义真正对数据库执行操作的方法,userDOMapper(userPasswordMapper)类实例调用CRUD方法。
7、对数据库进行操作时抛出”该字段没有默认值“错误,数据库对应字段设置为自增问题解决
8、对传入字段需进行加密传入数据库时,设置加密函数
public String EncodeByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest md5=MessageDigest.getInstance("MD5");
Base64.Encoder encoder=Base64.getEncoder();
//加密字符串
String newstr=encoder.encodeToString(md5.digest(str.getBytes("utf-8")));
return newstr;
}
9、setAttribute和getAttribute(转载一文理清Request和Response - 知乎
//将otp验证码与用户手机相关联 使用httpsession的方式绑定otpcode和手机号
httpServletRequest.getSession().setAttribute(telphone,otpCode);
//获取对应name对应的value
String insessionOtpCode = (String) this.httpServletRequest.getSession().getAttribute(telphone);
web服务器收到客户端的HTTP请求,会针对每一次请求分别创建一个用于代表请求的request对象和代表响应的response对象。
- 要得到客户机提交过来的数据,只需要找request对象就行了。
- 要向客户机输出数据,只需要找response对象就行了。
request就是将请求报文封装而成的对象,所以通过request能获得请求文本中的所有内容,请求头、请求体、请求行 。浏览器向服务器发送请求,因为浏览器与服务器之间的通信实质上是socket流,所以要先将请求参数(字符)转换成字节,也就是编码过程,服务器接收到请求参数后进行解码(字节转字符),然后封装到request对象中。如果客户端的编码与服务器端的解码不统一,就会导致通过request获取到的请求参数的值是乱码。
所以为了解决request乱码的问题,我们只需要在服务器端设置相应的解码格式即可。
具体设置代码如下:
request.setCharacterEncoding("utf-8"); //设置request对象的解码方式