统一返回结果
找到模块 trip-common-core,创建包com.swx.common.core.utils
,创建R
R@Setter @Getter @NoArgsConstructor public class R<T> { public static final int CODE_SUCCESS = 200; public static final String MSG_SUCCESS = "操作成功"; public static final int CODE_NOLOGIN = 401; public static final String MSG_NOLOGIN = "请先登陆"; public static final int CODE_ERROR = 500; public static final int CODE_REGISTER_ERROR = 500; public static final String MSG_ERROR = "系统异常,请联系管理员"; public static final int CODE_ERROR_PARAM = 501;
private int code; private String msg; private T data;
public R(int code, String msg, T data) { this.code = code; this.msg = msg; this.data = data; }
public static <T> R<T> ok(T data) { return new R<>(CODE_SUCCESS, MSG_SUCCESS, data); }
public static <T> R<T> ok() { return new R<>(CODE_SUCCESS, MSG_SUCCESS, null); }
public static <T> R<T> error(int code, String msg, T data) { return new R<>(code, msg, data); }
public static <T> R<T> error(int code, String msg) { return new R<>(code, msg, null); }
public static <T> R<T> defaultError() { return new R<>(CODE_ERROR, MSG_ERROR, null); }
public static <T> R<T> noLogin() { return new R<>(CODE_NOLOGIN, MSG_NOLOGIN, null); }
public static <T> R<T> noPermission() { return new R<>(403, "非法访问", null); } public T checkAndGet() { if (this.code != CODE_SUCCESS) { throw new BizException(code, msg); } return data; } }
|
自定义异常
创建包com.swx.common.core.exception
,新建 BizException
BizException
@Getter public class BizException extends RuntimeException {
private Integer code = R.CODE_ERROR;
public BizException() { super(R.MSG_ERROR); }
public BizException(String message) { super(message); }
public BizException(Integer code, String message) { super(message); this.code = code; } }
|
Md5Utils
包com.swx.common.core.utils
,创建 Md5Utils
Md5Utils
public class Md5Utils {
public static String getMD5(String message) { String md5 = ""; try { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] messageByte = message.getBytes("UTF-8"); byte[] md5Byte = md.digest(messageByte); md5 = bytesToHex(md5Byte); } catch (Exception e) { e.printStackTrace(); } return md5; }
private static String bytesToHex(byte[] bytes) { StringBuffer hexStr = new StringBuffer(); int num; for (int i = 0; i < bytes.length; i++) { num = bytes[i]; if (num < 0) { num += 256; } if (num < 16) { hexStr.append("0"); } hexStr.append(Integer.toHexString(num)); } return hexStr.toString().toUpperCase(); }
public static String signatures(Map<String, Object> params){ String signatures = ""; try { List<String> paramsStr = new ArrayList<String>(); for (String key1 : params.keySet()) { if(null != key1 && !"".equals(key1)){ paramsStr.add(key1); } } Collections.sort(paramsStr); StringBuilder sbff = new StringBuilder(); for (String kk : paramsStr) { String value = params.get(kk).toString(); if ("".equals(sbff.toString())) { sbff.append(kk + "=" + value); } else { sbff.append("&" + kk + "=" + value); } } signatures = getMD5(sbff.toString()).toUpperCase(); }catch(Exception e) { e.printStackTrace(); } return signatures; }
public static void main(String[] args) { String str = "12345618888888888"; String pass = Md5Utils.getMD5(str); System.out.println(pass); } }
|