From 49f3e590f99631686cce673674ee6d1f94c3e9fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E6=9D=B0?= Date: Sun, 6 Jan 2019 12:51:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96=EF=BC=8C?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E6=97=A5=E5=BF=97=E4=BF=9D=E5=AD=98=E5=A0=86?= =?UTF-8?q?=E6=A0=88=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 18 ++ src/main/java/me/zhengjie/AppRun.java | 1 - .../me/zhengjie/common/aop/log/LogAspect.java | 3 +- .../handler/GlobalExceptionHandler.java | 32 +-- .../me/zhengjie/common/utils/FtlUtil.java | 27 +++ .../me/zhengjie/common/utils/StringUtils.java | 214 ++++++++++++++++++ .../zhengjie/common/utils/ThrowableUtil.java | 28 +++ .../me/zhengjie/monitor/domain/Logging.java | 4 +- .../monitor/repository/LoggingRepository.java | 4 +- .../service/impl/LoggingServiceImpl.java | 4 +- .../system/service/impl/MenuServiceImpl.java | 4 + 11 files changed, 304 insertions(+), 35 deletions(-) create mode 100644 src/main/java/me/zhengjie/common/utils/FtlUtil.java create mode 100644 src/main/java/me/zhengjie/common/utils/StringUtils.java create mode 100644 src/main/java/me/zhengjie/common/utils/ThrowableUtil.java diff --git a/pom.xml b/pom.xml index df66854c..9285505d 100644 --- a/pom.xml +++ b/pom.xml @@ -60,6 +60,12 @@ org.springframework.boot spring-boot-starter-websocket + + + + org.springframework.boot + spring-boot-starter-freemarker + @@ -175,6 +181,18 @@ 3.1.0 + + + commons-configuration + commons-configuration + 1.9 + + + + + org.quartz-scheduler + quartz + diff --git a/src/main/java/me/zhengjie/AppRun.java b/src/main/java/me/zhengjie/AppRun.java index 18f711c3..67ac0d32 100644 --- a/src/main/java/me/zhengjie/AppRun.java +++ b/src/main/java/me/zhengjie/AppRun.java @@ -11,7 +11,6 @@ import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBr * @date 2018/11/15 9:20:19 */ @SpringBootApplication -//开启定时任务 @EnableScheduling @EnableTransactionManagement @EnableWebSocketMessageBroker diff --git a/src/main/java/me/zhengjie/common/aop/log/LogAspect.java b/src/main/java/me/zhengjie/common/aop/log/LogAspect.java index 0017e744..cc70a96c 100644 --- a/src/main/java/me/zhengjie/common/aop/log/LogAspect.java +++ b/src/main/java/me/zhengjie/common/aop/log/LogAspect.java @@ -2,6 +2,7 @@ package me.zhengjie.common.aop.log; import lombok.extern.slf4j.Slf4j; import me.zhengjie.common.exception.BadRequestException; +import me.zhengjie.common.utils.ThrowableUtil; import me.zhengjie.monitor.domain.Logging; import me.zhengjie.monitor.service.LoggingService; import org.aspectj.lang.JoinPoint; @@ -66,7 +67,7 @@ public class LogAspect { @AfterThrowing(pointcut = "logPointcut()", throwing = "e") public void logAfterThrowing(JoinPoint joinPoint, Throwable e) { Logging logging = new Logging("ERROR",System.currentTimeMillis() - currentTime); - logging.setExceptionDetail(e.getMessage()); + logging.setExceptionDetail(ThrowableUtil.getStackTrace(e)); loggingService.save((ProceedingJoinPoint)joinPoint, logging); } } diff --git a/src/main/java/me/zhengjie/common/exception/handler/GlobalExceptionHandler.java b/src/main/java/me/zhengjie/common/exception/handler/GlobalExceptionHandler.java index 40262431..30d5987e 100644 --- a/src/main/java/me/zhengjie/common/exception/handler/GlobalExceptionHandler.java +++ b/src/main/java/me/zhengjie/common/exception/handler/GlobalExceptionHandler.java @@ -4,14 +4,13 @@ import lombok.extern.slf4j.Slf4j; import me.zhengjie.common.exception.BadRequestException; import me.zhengjie.common.exception.EntityExistException; import me.zhengjie.common.exception.EntityNotFoundException; +import me.zhengjie.common.utils.ThrowableUtil; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.access.AccessDeniedException; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; -import java.io.PrintWriter; -import java.io.StringWriter; import static org.springframework.http.HttpStatus.BAD_REQUEST; import static org.springframework.http.HttpStatus.FORBIDDEN; import static org.springframework.http.HttpStatus.NOT_FOUND; @@ -32,7 +31,7 @@ public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) public ResponseEntity handleException(Exception e){ // 打印堆栈信息 - log.error(getStackTrace(e)); + log.error(ThrowableUtil.getStackTrace(e)); ApiError apiError = new ApiError(BAD_REQUEST.value(),e.getMessage()); return buildResponseEntity(apiError); } @@ -45,7 +44,7 @@ public class GlobalExceptionHandler { @ExceptionHandler(AccessDeniedException.class) public ResponseEntity handleAccessDeniedException(AccessDeniedException e){ // 打印堆栈信息 - log.error(getStackTrace(e)); + log.error(ThrowableUtil.getStackTrace(e)); ApiError apiError = new ApiError(FORBIDDEN.value(),e.getMessage()); return buildResponseEntity(apiError); } @@ -58,7 +57,7 @@ public class GlobalExceptionHandler { @ExceptionHandler(value = BadRequestException.class) public ResponseEntity badRequestException(BadRequestException e) { // 打印堆栈信息 - log.error(getStackTrace(e)); + log.error(ThrowableUtil.getStackTrace(e)); ApiError apiError = new ApiError(e.getStatus(),e.getMessage()); return buildResponseEntity(apiError); } @@ -71,7 +70,7 @@ public class GlobalExceptionHandler { @ExceptionHandler(value = EntityExistException.class) public ResponseEntity entityExistException(EntityExistException e) { // 打印堆栈信息 - log.error(getStackTrace(e)); + log.error(ThrowableUtil.getStackTrace(e)); ApiError apiError = new ApiError(BAD_REQUEST.value(),e.getMessage()); return buildResponseEntity(apiError); } @@ -84,7 +83,7 @@ public class GlobalExceptionHandler { @ExceptionHandler(value = EntityNotFoundException.class) public ResponseEntity entityNotFoundException(EntityNotFoundException e) { // 打印堆栈信息 - log.error(getStackTrace(e)); + log.error(ThrowableUtil.getStackTrace(e)); ApiError apiError = new ApiError(NOT_FOUND.value(),e.getMessage()); return buildResponseEntity(apiError); } @@ -97,7 +96,7 @@ public class GlobalExceptionHandler { @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity handleMethodArgumentNotValidException(MethodArgumentNotValidException e){ // 打印堆栈信息 - log.error(getStackTrace(e)); + log.error(ThrowableUtil.getStackTrace(e)); String[] str = e.getBindingResult().getAllErrors().get(0).getCodes()[1].split("\\."); StringBuffer msg = new StringBuffer(str[1]+":"); msg.append(e.getBindingResult().getAllErrors().get(0).getDefaultMessage()); @@ -113,21 +112,4 @@ public class GlobalExceptionHandler { private ResponseEntity buildResponseEntity(ApiError apiError) { return new ResponseEntity(apiError, HttpStatus.valueOf(apiError.getStatus())); } - - /** - * 获取堆栈信息 - * @param throwable - * @return - */ - private String getStackTrace(Throwable throwable) - { - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - try { - throwable.printStackTrace(pw); - return "\n"+sw.toString(); - } finally { - pw.close(); - } - } } diff --git a/src/main/java/me/zhengjie/common/utils/FtlUtil.java b/src/main/java/me/zhengjie/common/utils/FtlUtil.java new file mode 100644 index 00000000..067ed55e --- /dev/null +++ b/src/main/java/me/zhengjie/common/utils/FtlUtil.java @@ -0,0 +1,27 @@ +package me.zhengjie.common.utils; + +import freemarker.template.Configuration; +import java.io.FileOutputStream; + +/** + * @author jie + * @date 2019-01-03 + */ +public class FtlUtil { + + private static Configuration configuration; + private static FileOutputStream fileOut = null; + + static { + configuration=new Configuration(Configuration.VERSION_2_3_28); + configuration.setDefaultEncoding("UTF-8"); + configuration.setClassForTemplateLoading(FtlUtil.class, "/template/generator"); + configuration.setClassForTemplateLoading(FtlUtil.class, "/template/email"); + } + + public static Configuration getConfig(){ + return configuration; + } + + +} diff --git a/src/main/java/me/zhengjie/common/utils/StringUtils.java b/src/main/java/me/zhengjie/common/utils/StringUtils.java new file mode 100644 index 00000000..42126c9a --- /dev/null +++ b/src/main/java/me/zhengjie/common/utils/StringUtils.java @@ -0,0 +1,214 @@ +package me.zhengjie.common.utils; + +import java.io.File; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import org.springframework.core.io.DefaultResourceLoader; + +/** + * 字符串工具类, 继承org.apache.commons.lang3.StringUtils类 + */ +public class StringUtils extends org.apache.commons.lang3.StringUtils { + + private static final char SEPARATOR = '_'; + private static final String CHARSET_NAME = "UTF-8"; + + /** + * 转换为字节数组 + * + * @param str + * @return + */ + public static byte[] getBytes(String str) { + if (str != null) { + try { + return str.getBytes(CHARSET_NAME); + } catch (UnsupportedEncodingException e) { + return null; + } + } else { + return null; + } + } + + /** + * 转换为字节数组 + * + * @param bytes + * @return + */ + public static String toString(byte[] bytes) { + try { + return new String(bytes, CHARSET_NAME); + } catch (UnsupportedEncodingException e) { + return EMPTY; + } + } + + /** + * 是否包含字符串 + * + * @param str 验证字符串 + * @param strs 字符串组 + * @return 包含返回true + */ + public static boolean inString(String str, String... strs) { + if (str != null) { + for (String s : strs) { + if (str.equals(trim(s))) { + return true; + } + } + } + return false; + } + + /** + * 转换为Double类型 + */ + public static Double toDouble(Object val) { + if (val == null) { + return 0D; + } + try { + return Double.valueOf(trim(val.toString())); + } catch (Exception e) { + return 0D; + } + } + + /** + * 转换为Float类型 + */ + public static Float toFloat(Object val) { + return toDouble(val).floatValue(); + } + + /** + * 转换为Long类型 + */ + public static Long toLong(Object val) { + return toDouble(val).longValue(); + } + + /** + * 转换为Integer类型 + */ + public static Integer toInteger(Object val) { + return toLong(val).intValue(); + } + + /** + * 驼峰命名法工具 + * + * @return toCamelCase(" hello_world ") == "helloWorld" + * toCapitalizeCamelCase("hello_world") == "HelloWorld" + * toUnderScoreCase("helloWorld") = "hello_world" + */ + public static String toCamelCase(String s) { + if (s == null) { + return null; + } + + s = s.toLowerCase(); + + StringBuilder sb = new StringBuilder(s.length()); + boolean upperCase = false; + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + + if (c == SEPARATOR) { + upperCase = true; + } else if (upperCase) { + sb.append(Character.toUpperCase(c)); + upperCase = false; + } else { + sb.append(c); + } + } + + return sb.toString(); + } + + /** + * 驼峰命名法工具 + * + * @return toCamelCase(" hello_world ") == "helloWorld" + * toCapitalizeCamelCase("hello_world") == "HelloWorld" + * toUnderScoreCase("helloWorld") = "hello_world" + */ + public static String toCapitalizeCamelCase(String s) { + if (s == null) { + return null; + } + s = toCamelCase(s); + return s.substring(0, 1).toUpperCase() + s.substring(1); + } + + /** + * 驼峰命名法工具 + * + * @return toCamelCase(" hello_world ") == "helloWorld" + * toCapitalizeCamelCase("hello_world") == "HelloWorld" + * toUnderScoreCase("helloWorld") = "hello_world" + */ + public static String toUnderScoreCase(String s) { + if (s == null) { + return null; + } + + StringBuilder sb = new StringBuilder(); + boolean upperCase = false; + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + + boolean nextUpperCase = true; + + if (i < (s.length() - 1)) { + nextUpperCase = Character.isUpperCase(s.charAt(i + 1)); + } + + if ((i > 0) && Character.isUpperCase(c)) { + if (!upperCase || !nextUpperCase) { + sb.append(SEPARATOR); + } + upperCase = true; + } else { + upperCase = false; + } + + sb.append(Character.toLowerCase(c)); + } + + return sb.toString(); + } + + /** + * 获取工程路径 + * + * @return + */ + public static String getProjectPath() { + String projectPath = ""; + try { + File file = new DefaultResourceLoader().getResource("").getFile(); + if (file != null) { + while (true) { + File f = new File(file.getPath() + File.separator + "src" + File.separator + "main"); + if (f == null || f.exists()) { + break; + } + if (file.getParentFile() != null) { + file = file.getParentFile(); + } else { + break; + } + } + projectPath = file.toString(); + } + } catch (IOException e) { + e.printStackTrace(); + } + return projectPath; + } +} \ No newline at end of file diff --git a/src/main/java/me/zhengjie/common/utils/ThrowableUtil.java b/src/main/java/me/zhengjie/common/utils/ThrowableUtil.java new file mode 100644 index 00000000..f8fc215e --- /dev/null +++ b/src/main/java/me/zhengjie/common/utils/ThrowableUtil.java @@ -0,0 +1,28 @@ +package me.zhengjie.common.utils; + +import java.io.PrintWriter; +import java.io.StringWriter; + +/** + * 异常工具 + * @author jie + * @date 2019-01-06 + */ +public class ThrowableUtil { + + /** + * 获取堆栈信息 + * @param throwable + * @return + */ + public static String getStackTrace(Throwable throwable){ + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + try { + throwable.printStackTrace(pw); + return "\n"+sw.toString(); + } finally { + pw.close(); + } + } +} diff --git a/src/main/java/me/zhengjie/monitor/domain/Logging.java b/src/main/java/me/zhengjie/monitor/domain/Logging.java index 0531694e..c36e8465 100644 --- a/src/main/java/me/zhengjie/monitor/domain/Logging.java +++ b/src/main/java/me/zhengjie/monitor/domain/Logging.java @@ -38,7 +38,7 @@ public class Logging { /** * 参数 */ - @Column(length = 1500) + @Column(columnDefinition = "text") private String params; /** @@ -59,7 +59,7 @@ public class Logging { /** * 异常详细 */ - @Column(length = 1500) + @Column(columnDefinition = "text") private String exceptionDetail; /** diff --git a/src/main/java/me/zhengjie/monitor/repository/LoggingRepository.java b/src/main/java/me/zhengjie/monitor/repository/LoggingRepository.java index b7c4b29b..143c278d 100644 --- a/src/main/java/me/zhengjie/monitor/repository/LoggingRepository.java +++ b/src/main/java/me/zhengjie/monitor/repository/LoggingRepository.java @@ -6,8 +6,6 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; -import java.util.List; - /** * @author jie * @date 2018-11-24 @@ -21,6 +19,6 @@ public interface LoggingRepository extends JpaRepository, JpaSpeci * @param date2 * @return */ - @Query(value = "select count(*) FROM (select * FROM log where createTime between ?1 and ?2 GROUP BY requestIp) as s",nativeQuery = true) + @Query(value = "select count(*) FROM (select requestIp FROM log where createTime between ?1 and ?2 GROUP BY requestIp) as s",nativeQuery = true) Long findIp(String date1, String date2); } diff --git a/src/main/java/me/zhengjie/monitor/service/impl/LoggingServiceImpl.java b/src/main/java/me/zhengjie/monitor/service/impl/LoggingServiceImpl.java index a55bc210..e1d05800 100644 --- a/src/main/java/me/zhengjie/monitor/service/impl/LoggingServiceImpl.java +++ b/src/main/java/me/zhengjie/monitor/service/impl/LoggingServiceImpl.java @@ -83,9 +83,7 @@ public class LoggingServiceImpl implements LoggingService { username = user.getUsername(); } - if (params.length() > 1000){ - params = params.substring(0,999); - } + logging.setMethod(methodName); logging.setUsername(username); logging.setParams(params + " }"); loggingRepository.save(logging); diff --git a/src/main/java/me/zhengjie/system/service/impl/MenuServiceImpl.java b/src/main/java/me/zhengjie/system/service/impl/MenuServiceImpl.java index d6bdd54d..11d26bd4 100644 --- a/src/main/java/me/zhengjie/system/service/impl/MenuServiceImpl.java +++ b/src/main/java/me/zhengjie/system/service/impl/MenuServiceImpl.java @@ -101,9 +101,13 @@ public class MenuServiceImpl implements MenuService { List> list = new LinkedList<>(); menus.forEach(menu -> { if (menu!=null){ + List menuList = menuRepository.findByPid(menu.getId()); Map map = new HashMap<>(); map.put("id",menu.getId()); map.put("label",menu.getName()); + if(menuList!=null && menuList.size()!=0){ + map.put("children",getMenuTree(menuList)); + } list.add(map); } }