From 8034e9ab87862ccecc5bcd3e6eabc8da0c7eac18 Mon Sep 17 00:00:00 2001 From: "453530270@qq.com" Date: Tue, 5 Mar 2024 10:28:35 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 36 ++++ pom.xml | 59 ++++++ .../bc/core/bcmail/BcmailApplication.java | 12 ++ .../bcmail/controller/DebugController.java | 34 +++ .../controller/GlobalErrorController.java | 75 +++++++ .../bcmail/controller/HomeController.java | 62 ++++++ .../bcmail/controller/SendMailController.java | 14 ++ .../bcmail/exception/AccessException.java | 22 ++ .../bc/core/bcmail/exception/BcException.java | 34 +++ .../java/bc/core/bcmail/utils/AjaxResult.java | 81 +++++++ src/main/resources/application.properties | 32 +++ src/main/resources/banner.txt | 4 + src/main/resources/logback-spring.xml | 199 ++++++++++++++++++ 13 files changed, 664 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/bc/core/bcmail/BcmailApplication.java create mode 100644 src/main/java/bc/core/bcmail/controller/DebugController.java create mode 100644 src/main/java/bc/core/bcmail/controller/GlobalErrorController.java create mode 100644 src/main/java/bc/core/bcmail/controller/HomeController.java create mode 100644 src/main/java/bc/core/bcmail/controller/SendMailController.java create mode 100644 src/main/java/bc/core/bcmail/exception/AccessException.java create mode 100644 src/main/java/bc/core/bcmail/exception/BcException.java create mode 100644 src/main/java/bc/core/bcmail/utils/AjaxResult.java create mode 100644 src/main/resources/application.properties create mode 100644 src/main/resources/banner.txt create mode 100644 src/main/resources/logback-spring.xml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed57ec8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,36 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### logs ### +logs/ \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..5fad499 --- /dev/null +++ b/pom.xml @@ -0,0 +1,59 @@ + + + 4.0.0 + + bc.core + bcmail + 1.0.01 + + + + 1.8 + 2.7.15 + + + + + + org.springframework.boot + spring-boot-starter-web + ${springboot.version} + + + org.springframework.boot + spring-boot-starter-mail + + + commons-lang + commons-lang + 2.6 + + + commons-logging + commons-logging + 1.2 + + + com.alibaba + fastjson + 2.0.7 + + + org.springframework.amqp + spring-rabbit-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/src/main/java/bc/core/bcmail/BcmailApplication.java b/src/main/java/bc/core/bcmail/BcmailApplication.java new file mode 100644 index 0000000..cc93ed8 --- /dev/null +++ b/src/main/java/bc/core/bcmail/BcmailApplication.java @@ -0,0 +1,12 @@ +package bc.core.bcmail; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class BcmailApplication { + public static void main(String[] args) { + SpringApplication.run(BcmailApplication.class, args); + } + +} diff --git a/src/main/java/bc/core/bcmail/controller/DebugController.java b/src/main/java/bc/core/bcmail/controller/DebugController.java new file mode 100644 index 0000000..55c8e5e --- /dev/null +++ b/src/main/java/bc/core/bcmail/controller/DebugController.java @@ -0,0 +1,34 @@ +package bc.core.bcmail.controller; + + +import org.apache.commons.lang.StringUtils; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.text.SimpleDateFormat; +import java.util.Date; + + +@RestController +public class DebugController { + private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + /*** + * 调试接口 + * @return + */ + @RequestMapping("/test") + public String mtest(String param){ + Date now = new Date(); + String nstr = sdf.format(now); + +// long timestamp = System.currentTimeMillis(); + String ret = ""; + if(StringUtils.isEmpty(param)){ + ret = nstr; +// ret = String.valueOf(timestamp); + }else { + ret = param+"\r\n"+nstr; + } + return ret; + } +} diff --git a/src/main/java/bc/core/bcmail/controller/GlobalErrorController.java b/src/main/java/bc/core/bcmail/controller/GlobalErrorController.java new file mode 100644 index 0000000..3380b97 --- /dev/null +++ b/src/main/java/bc/core/bcmail/controller/GlobalErrorController.java @@ -0,0 +1,75 @@ +package bc.core.bcmail.controller; + + +import bc.core.bcmail.exception.AccessException; +import bc.core.bcmail.exception.BcException; +import bc.core.bcmail.utils.AjaxResult; +import com.alibaba.fastjson.JSONObject; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; + +/** + * 全局异常控制器 + */ +@ControllerAdvice +public class GlobalErrorController { + //日志初始化 + private static final Log log = LogFactory.getLog(GlobalErrorController.class); + + private JSONObject jsonObject=new JSONObject(); + /** + * 异常信息输出 + * @return + */ + @ExceptionHandler(BcException.class) + @ResponseBody + public JSONObject aisHandler(BcException chex){ + jsonObject = AjaxResult.exception(chex.getCode(),chex.getMsg()); + return jsonObject; + } + + /** + * 空指针异常 + * @param e + * @return + */ + @ExceptionHandler(value =NullPointerException.class) + @ResponseBody + public JSONObject exceptionHandler(NullPointerException e){ + log.error("空指针异常:"+e.getStackTrace()); + String msg = e.getMessage()==null?"module not found!":e.getMessage(); + jsonObject.put("status",500); + jsonObject.put("message",msg); + return jsonObject; + } + + /** + * 常规异常信息 + * @param ex + * @return + */ + @ExceptionHandler(value = Exception.class) + @ResponseBody + public JSONObject exceptionHandler(Exception ex){ + log.error("异常信息:"+ex); + return AjaxResult.exception(500,ex.getMessage()); + } + + /** + * 401 异常 + * @param ae + * @return + */ + @ResponseBody + @ResponseStatus(value = HttpStatus.UNAUTHORIZED) + @ExceptionHandler(value = AccessException.class) + public JSONObject accessHandler(AccessException ae){ + log.error("需要登录"+ae.getLocalizedMessage()); + return AjaxResult.exception(401,ae.getMessage()); + } +} diff --git a/src/main/java/bc/core/bcmail/controller/HomeController.java b/src/main/java/bc/core/bcmail/controller/HomeController.java new file mode 100644 index 0000000..d577858 --- /dev/null +++ b/src/main/java/bc/core/bcmail/controller/HomeController.java @@ -0,0 +1,62 @@ +package bc.core.bcmail.controller; + +import bc.core.bcmail.utils.AjaxResult; +import com.alibaba.fastjson.JSONObject; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.text.SimpleDateFormat; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Date; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.TimeZone; + +@RestController +public class HomeController { + /** + * homepage + * @return + */ + @RequestMapping({"/",""}) + public JSONObject home(){ + // output current time + // ask jdk gt 1.8+ +// // get localtime +// ZoneId zoneId = ZoneId.of("America/New_York"); +// // get current time +// ZonedDateTime zonedDateTime = ZonedDateTime.now(); +// // convert to US. time format +// ZonedDateTime ustime = zonedDateTime.withZoneSameInstant(zoneId); +// // +// DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); +// +// String format = zonedDateTime .format(dateFormat); +// return AjaxResult.success(format); + //************************************************************* + + // 设置时区为美国 + TimeZone timeZone = TimeZone.getTimeZone("America/New_York"); + // 创建 SimpleDateFormat 对象,指定日期格式 + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzzz"); + sdf.setTimeZone(timeZone); + + // 获取当前时间 + Date date = new Date(); + + // 将日期按照指定格式转换成字符串 + String formattedDate = sdf.format(date); + return AjaxResult.success(formattedDate); + } + + @RequestMapping("/debug/{msg}") + public JSONObject debug(@PathVariable String msg){ + LinkedHashMap rtmap = new LinkedHashMap(); + rtmap.put("time",System.currentTimeMillis()); + rtmap.put("data",msg); + return AjaxResult.success("success",rtmap); + } +} diff --git a/src/main/java/bc/core/bcmail/controller/SendMailController.java b/src/main/java/bc/core/bcmail/controller/SendMailController.java new file mode 100644 index 0000000..601115e --- /dev/null +++ b/src/main/java/bc/core/bcmail/controller/SendMailController.java @@ -0,0 +1,14 @@ +package bc.core.bcmail.controller; + +import org.springframework.web.bind.annotation.RestController; + +/** + * + * + * +获取参数,并发送邮件,将结果post 给notify_url +json 传递的值用post方式 +*/ +@RestController +public class SendMailController { +} diff --git a/src/main/java/bc/core/bcmail/exception/AccessException.java b/src/main/java/bc/core/bcmail/exception/AccessException.java new file mode 100644 index 0000000..5188393 --- /dev/null +++ b/src/main/java/bc/core/bcmail/exception/AccessException.java @@ -0,0 +1,22 @@ +package bc.core.bcmail.exception; + +/** + * 访问权限异常 + */ +public class AccessException extends RuntimeException{ + private String message; + + @Override + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + // 构造函数 + public AccessException(String message) { + this.message = message; + } +} diff --git a/src/main/java/bc/core/bcmail/exception/BcException.java b/src/main/java/bc/core/bcmail/exception/BcException.java new file mode 100644 index 0000000..ce51499 --- /dev/null +++ b/src/main/java/bc/core/bcmail/exception/BcException.java @@ -0,0 +1,34 @@ +package bc.core.bcmail.exception; + +/** + * 全局异常 + */ +public class BcException extends RuntimeException { + private String msg; + private int code; + + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public BcException(String msg, int code) { + this.msg = msg; + this.code = code; + } + + public BcException(String msg) { + this.msg = msg; + } +} diff --git a/src/main/java/bc/core/bcmail/utils/AjaxResult.java b/src/main/java/bc/core/bcmail/utils/AjaxResult.java new file mode 100644 index 0000000..b6ad3b6 --- /dev/null +++ b/src/main/java/bc/core/bcmail/utils/AjaxResult.java @@ -0,0 +1,81 @@ +package bc.core.bcmail.utils; + +import com.alibaba.fastjson.JSONObject; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + + +public class AjaxResult { + /** + * 接口返回数据 + * @param msg + * @param dataList + * @return + */ + public static JSONObject success(String msg,List dataList){ + JSONObject result = new JSONObject(); + result.put("status",200); + result.put("msg",msg); + result.put("data",dataList); + return result; + } + + public static JSONObject success(String token){ + JSONObject result= new JSONObject(); + result.put("code",200); + result.put("token",token); + return result; + } + + public static JSONObject success(String msg, Map map){ + JSONObject result = new JSONObject(); + result.put("code",200); + result.put("msg",msg); + result.put("data",map); + return result; + } + + /** + * 异常信息 + * @param msg + * @return + */ + public static JSONObject error(String msg){ + JSONObject result = new JSONObject(); + result.put("code",500); + result.put("msg",msg); + return result; + } + + /** + * 格式异常返回 + * @param code + * @param msg + * @return + */ + public static JSONObject exception(int code,String msg){ + Map rmap = new HashMap(); + rmap.put("message",msg); + rmap.put("time",System.currentTimeMillis()/1000l); + + // + JSONObject rjson = new JSONObject(); + rjson.put("status",code); + rjson.put("message",msg); +// rjson.put("data",rmap); + + return rjson; + } + + public static JSONObject error500(String msg){ + Map rmap = new HashMap(); + rmap.put("message",msg); + // + JSONObject rjson = new JSONObject(); + rjson.put("data",rmap); + rjson.put("status",500); + return rjson; + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..4dd3e83 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,32 @@ +# logback日志操作 +logging.config=classpath:logback-spring.xml +logging.path=./logs/ + +#application.properties基本配置,后面我都使用此配置来发送邮件 +## 基本配置 +### smtp服务器主机(163的) +spring.mail.host=smtp.163.com +### 连接邮件服务器端口(默认SMTP 25 POP 110) +spring.mail.port=25 +### 服务协议SMTP(代表是发送邮件) +spring.mail.protocol=smtp +### 登录服务器邮箱账号 +spring.mail.username=antladdie +### 登录服务器邮箱授权码(不是邮箱密码,这个是我们开通SMTP、POP时得到的授权码) +spring.mail.password=xxxxxxxxxxxxx +### 默认邮件的编码集(MimeMessage 编码,默认UTF-8) +spring.mail.default-encoding=UTF-8 + +# 补充配置(这里具体可以参照Jakarta Mail的扩展配置) +## 默认发送方邮箱账号(当程序未指定发件人邮箱则默认取这个) +#spring.mail.properties.mail.smtp.from=antladdie@163.com +## 开启权限认证 +spring.mail.properties.mail.smtp.auth=true +## 邮件接收时间的限制 +spring.mail.properties.mail.smtp.timeout=60000 +## 连接时间的限制 +spring.mail.properties.mail.smtp.connectiontimeout=60000 +## 邮件发送时间的限制(毫秒) +spring.mail.properties.mail.smtp.writetimeout=60000 +## 日志打印,邮件发送过程的日志会被输出 +spring.mail.properties.mail.debug=true \ No newline at end of file diff --git a/src/main/resources/banner.txt b/src/main/resources/banner.txt new file mode 100644 index 0000000..4a03049 --- /dev/null +++ b/src/main/resources/banner.txt @@ -0,0 +1,4 @@ +===== Welcome to BCMIS ===== +SPB. version:${spring-boot.version} +BCMIS version 1.0.1 +============================ \ No newline at end of file diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..ac3b809 --- /dev/null +++ b/src/main/resources/logback-spring.xml @@ -0,0 +1,199 @@ + + + + + + + + + + logback + + + + + + + + + + + + + + + + + + + + info + + + ${CONSOLE_LOG_PATTERN} + + UTF-8 + + + + + + + + + + ${LOG_PATH}/log_debug.log + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + UTF-8 + + + + + ${LOG_PATH}/debug/log-debug-%d{yyyy-MM-dd}.%i.log + + 100MB + + + 15 + + + + debug + ACCEPT + DENY + + + + + + + ${LOG_PATH}/log_info.log + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + UTF-8 + + + + + ${LOG_PATH}/info/log-info-%d{yyyy-MM-dd}.%i.log + + 100MB + + + 15 + + + + info + ACCEPT + DENY + + + + + + + ${LOG_PATH}/log_warn.log + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + UTF-8 + + + + ${LOG_PATH}/warn/log-warn-%d{yyyy-MM-dd}.%i.log + + 100MB + + + 15 + + + + warn + ACCEPT + DENY + + + + + + + + ${LOG_PATH}/log_error.log + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + UTF-8 + + + + ${LOG_PATH}/error/log-error-%d{yyyy-MM-dd}.%i.log + + 100MB + + + 15 + + + + ERROR + ACCEPT + DENY + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file