依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>com.vdurmont</groupId>
<artifactId>emoji-java</artifactId>
<version>4.0.0</version>
</dependency>
注解
package com.xxx.base.common.aop;
import java.lang.annotation.*;
/**
* @author Mr.Guan
* @Title: 转换Emoji表情
* @Package ${package_name}
* @Description: ${todo}
* @Mail [email protected]
* @date 2019
*/
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EmojiAnn {
//增加参数,用来支持不同的业务
/**
* 需要转换Emoji表情的参数下标 0开始
* @return
*/
int[] appointInd() default {};
}
AOP
package com.xxx.base.common.aop;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.extra.emoji.EmojiUtil;
import cn.hutool.json.JSONUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;
/**
* @program:
*
* @description: 转换Emoji表情aop
*
* @author: Mr.Guan
*
* @Mail: [email protected]
*
* @create: 2019
**/
@Component
@Scope
@Aspect
public class EmojiAspect {
/**
* Service层切点 增加时间日志
* TODO 该路径应写成TimerLogHutool注解的路径
*/
@Pointcut("@annotation(com.xxx.base.common.aop.EmojiAnn)")
public void ServiceAspect() {
}
@Around("ServiceAspect()")
public Object around(ProceedingJoinPoint joinPoint) {
Object obj = null;
// //获取方法名称
// String methodName = joinPoint.getSignature().getName();
// //获取类名
// String name = joinPoint.getTarget().getClass().getName();
// //开启计时
// TimeInterval timer = DateUtil.timer();
// Console.log("【计时器@TimerLog】{}.{} - 开启计时,方法执行中...", name, methodName);
try {
//获取注解中的参数
MethodSignature m = (MethodSignature)joinPoint.getSignature();
//获取需要转换Emoji的参数下标
EmojiAnn annotation = m.getMethod().getAnnotation(EmojiAnn.class);
int[] appoint = annotation.appointInd();
Object[] args = joinPoint.getArgs();
for (int i = 0; i < args.length; i++) {
//判断是否需要转换Emoji
if(ArrayUtil.isNotEmpty(appoint)){
if(!ArrayUtil.contains(appoint, i)){
continue;
}
}
Object arg = args[i];
if(arg instanceof HttpServletRequest){
continue;
// }else if(arg instanceof Integer){
// continue;
}else if(NumberUtil.isNumber(Objects.toString(arg, ""))){
continue;
}
String s = JSONUtil.toJsonStr(arg);
boolean isJson = JSONUtil.isJson(s);
s = EmojiUtil.toAlias(s);
if(isJson){
Class<?> aClass = arg.getClass();
args[i] = JSONUtil.toBean(s, aClass);
}else{
args[i] = s;
}
}
//执行方法
obj = joinPoint.proceed(args);
String s = JSONUtil.toJsonStr(obj);
String s1 = EmojiUtil.toUnicode(s);
if(JSONUtil.isJson(s)){
obj = JSONUtil.toBean(s1, obj.getClass());
}else{
obj = s1;
}
// Console.log("【计时器@TimerLog】{}.{} - 结束计时,用时:{} 。", name, methodName, this.formatDuring(timer.interval()));
} catch (Throwable e) {
// Console.error("【计时器@TimerLog】{}.{} - 方法出错,结束计时,用时:{} 毫秒,错误信息:{}。", name, methodName, timer.interval(), e.getMessage());
e.printStackTrace();
}
return obj;
}
private String formatDuring(long mss) {
long days = mss / (1000 * 60 * 60 * 24);
long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
long seconds = (mss % (1000 * 60)) / 1000;
long msec = (mss % 1000);
String msg = "";
if(days > 0){
msg += days + "天 ";
}
if(hours > 0){
msg += hours + "时 ";
}
if(minutes > 0){
msg += minutes + "分 ";
}
if(seconds > 0){
msg += seconds + "秒 ";
}
if(msec > 0){
msg += msec + "毫秒";
}
return msg;
}
public static void main(String[] args) {
int[] arr = {0,1,3,5};
}
}
标签:String,Spring,60,注解,import,1000,annotation,Emoji From: https://blog.51cto.com/u_14976802/5932927