开始之前
源码已上传github:桂香驿站
版本
- springboot版本:2.1.3.RELEASE
- redis版本:3.2.1
- elasticsearch版本:6.4.3
SDK
微信小程序sdk
七牛云sdk
pom依赖
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-configuration-processor
true
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-data-elasticsearch
org.springframework.boot
spring-boot-starter-cache
org.springframework.boot
spring-boot-starter-data-redis
2.1.3.RELEASE
org.springframework.boot
spring-boot-starter-quartz
org.projectlombok
lombok
com.github.binarywang
weixin-java-miniapp
3.3.0
com.qiniu
qiniu-java-sdk
[7.2.0, 7.2.99]
org.apache.commons
commons-lang3
mysql
mysql-connector-java
commons-beanutils
commons-beanutils
1.9.3
org.springframework.boot
spring-boot-starter-test
test
lombok是方便的工具,可以帮你生成get、set方法
org.projectlombok
lombok
目录结构
东西有点多,会依次说明
一些工具类、异常类和枚举类
结果枚举类
用于返回一些错误信息,与异常类配合使用
@Getter
public enum ResultEnum {
SUCCESS(0, "成功"),
CODE_ERROR(1, "未获取到code"),
SESSION_ERROR(2, "获取sessionKey和openid失败"),
SESSION_ID_NULL(3, "sessionId不存在"),
USER_INFO_ERROR(4, "用户信息校验失败"),
PARAM_ERROR(5, "表单参数不正确"),
UPLOAD_ERROR(6, "文件上传出错"),
ARTICLE_ID_ERROR(7, "帖子id不能为空"),
COMMENT_ID_ERROR(8, "评论id不能为空"),
USER_NOT_EXIT(9, "用户不存在"),
ARTICLE_NOT_EXIT(10, "帖子不存在"),
COMMENT_NOT_EXIT(11, "评论不存在"),
REPLY_NOT_EXIT(12, "回复不存在")
;
private Integer code;
private String message;
ResultEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
}
异常类
用于抛出项目自定义的一些错误信息
@Data
public class BBSException extends RuntimeException {
private Integer code;
public BBSException(ResultEnum resultEnum) {
super(resultEnum.getMessage());
this.code = resultEnum.getCode();
}
public BBSException(Integer code, String message){
super(message);
this.code = code;
}
}
返回结果类
返回http请求的结果,code可让前端知道返回是否正常,操作是否成功
/**
* Http请求返回最外层对象
*/
@Data
public class ResultVO {
/** 错误码. */
private Integer code;
/** 提示信息. */
private String msg;
/** 具体内容. */
private T data;
}
返回结果工具类
用这个工具类可以方便的返回统一的结果
public class ResultVOUtil {
public static ResultVO success(Object object){
ResultVO resultVO = new ResultVO();
resultVO.setData(object);
resultVO.setCode(0);
resultVO.setMsg("成功");
return resultVO;
}
public static ResultVO success(){
return success(null);
}
public static ResultVO error(Integer code, String msg){
ResultVO resultVO = new ResultVO();
resultVO.setCode(code);
resultVO.setMsg(msg);
return resultVO;
}
}
生成键的工具类
一个方法用于生成主键id,另一个用与加密前后端通信的sessionId,因为微信不建议使用sessionKey作为前后端通信的token
public class KeyUtil {
/**
* 生成唯一的主键UUID
* @return
*/
public static synchronized String genUniqueKey(){
return UUID.randomUUID().toString().replace("-", "").toLowerCase();
}
/**
* MD5加密的sessionId
* @param key
* @return
*/
public static synchronized String getSessionId(String key) {
String sessionId = DigestUtils.md5Hex(key);
System.out.println("MD5加密后的sessionId为:" + sessionId);
return sessionId;
}
}
下一章会介绍springboot-jpa进行数据库增删改查~
下一篇:springboot+jpa+redis+quzartz+elasticsearch实现微信论坛小程序(二)