!45 使用fastJson消息转换器解决接口返回String 类型转换异常

Merge pull request !45 from kl/master
pull/26/head
Java3y 2 years ago committed by Gitee
commit 712de46ec7
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

@ -0,0 +1,20 @@
package com.java3y.austin.web.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author kl
* @version 1.0.0
* @description
* @date 2023/2/23 9:01
*/
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AustinAspect {
}

@ -0,0 +1,113 @@
package com.java3y.austin.web.aspect;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.java3y.austin.web.vo.RequestLogDTO;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
/**
* @author kl
* @version 1.0.0
* @description
* @date 2023/2/23 9:17
*/
@Slf4j
@Aspect
@Component
public class AustinAspect {
@Autowired
private HttpServletRequest request;
/**
* KEY
*/
private final String requestIdKey = "request_unique_id";
/**
* AustinAspect
*/
@Pointcut("@within(com.java3y.austin.web.annotation.AustinAspect) || @annotation(com.java3y.austin.web.annotation.AustinAspect)")
public void executeService() {
}
/**
*
*
* @param joinPoint
*/
@Before("executeService()")
public void doBeforeAdvice(JoinPoint joinPoint){
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
this.printRequestLog(methodSignature, joinPoint.getArgs());
}
/**
*
*
* @param ex
*/
@AfterThrowing(value = "executeService()", throwing = "ex")
public void doAfterThrowingAdvice(Throwable ex) {
printExceptionLog(ex);
}
/**
*
*
* @param methodSignature
* @param argObs
*/
public void printRequestLog(MethodSignature methodSignature, Object[] argObs) {
RequestLogDTO logVo = new RequestLogDTO();
//设置请求唯一ID
logVo.setId(IdUtil.fastUUID());
request.setAttribute(requestIdKey, logVo.getId());
logVo.setUri(request.getRequestURI());
logVo.setMethod(request.getMethod());
List<Object> args = Lists.newArrayList();
//过滤掉一些不能转为json字符串的参数
Arrays.stream(argObs).forEach(e -> {
if (e instanceof MultipartFile || e instanceof HttpServletRequest
|| e instanceof HttpServletResponse || e instanceof BindingResult) {
return;
}
args.add(e);
});
logVo.setArgs(args.toArray());
logVo.setProduct("austin");
logVo.setPath(methodSignature.getDeclaringTypeName() + "." + methodSignature.getMethod().getName());
logVo.setReferer(request.getHeader("referer"));
logVo.setRemoteAddr(request.getRemoteAddr());
logVo.setUserAgent(request.getHeader("user-agent"));
log.info("austin-aspect-log,request:" + JSON.toJSONString(logVo));
}
/**
*
*
* @param ex
*/
public void printExceptionLog(Throwable ex) {
JSONObject logVo = new JSONObject();
logVo.put("id", request.getAttribute(requestIdKey));
log.error("austin-aspect-log,exception:" + JSON.toJSONString(logVo), ex);
}
}

@ -0,0 +1,34 @@
package com.java3y.austin.web.config;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.google.common.collect.Lists;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import java.util.List;
/**
* @author kl
* @version 1.0.0
* @description
* @date 2023/2/23 10:40
*/
@Configuration
public class CommonConfiguration {
/**
* FastJson json
*
* @return
*/
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
List<MediaType> supportedMediaTypes = Lists.newArrayList();
supportedMediaTypes.add(MediaType.APPLICATION_JSON);
fastConverter.setSupportedMediaTypes(supportedMediaTypes);
return new HttpMessageConverters(fastConverter);
}
}

@ -5,6 +5,7 @@ import cn.hutool.core.util.StrUtil;
import com.java3y.austin.common.constant.AustinConstant;
import com.java3y.austin.common.enums.RespStatusEnum;
import com.java3y.austin.support.domain.ChannelAccount;
import com.java3y.austin.web.annotation.AustinAspect;
import com.java3y.austin.web.annotation.AustinResult;
import com.java3y.austin.web.exception.CommonException;
import com.java3y.austin.web.service.ChannelAccountService;
@ -27,6 +28,7 @@ import java.util.stream.Collectors;
* @author 3y
*/
@Slf4j
@AustinAspect
@AustinResult
@RestController
@RequestMapping("/account")

@ -1,6 +1,7 @@
package com.java3y.austin.web.controller;
import cn.hutool.core.util.StrUtil;
import com.java3y.austin.web.annotation.AustinAspect;
import com.java3y.austin.web.annotation.AustinResult;
import com.java3y.austin.web.service.DataService;
import com.java3y.austin.web.vo.DataParam;
@ -25,6 +26,7 @@ import java.util.Objects;
* @author 3y
*/
@Slf4j
@AustinAspect
@AustinResult
@RestController
@RequestMapping("/trace")

@ -3,6 +3,7 @@ package com.java3y.austin.web.controller;
import com.java3y.austin.common.enums.ChannelType;
import com.java3y.austin.common.vo.BasicResultVO;
import com.java3y.austin.web.annotation.AustinAspect;
import com.java3y.austin.web.service.MaterialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@ -21,6 +22,7 @@ import org.springframework.web.multipart.MultipartFile;
* @author 3y
*/
@Slf4j
@AustinAspect
@RestController
@RequestMapping("/material")
@Api("素材管理接口")

@ -14,6 +14,7 @@ import com.java3y.austin.service.api.enums.BusinessCode;
import com.java3y.austin.service.api.service.RecallService;
import com.java3y.austin.service.api.service.SendService;
import com.java3y.austin.support.domain.MessageTemplate;
import com.java3y.austin.web.annotation.AustinAspect;
import com.java3y.austin.web.annotation.AustinResult;
import com.java3y.austin.web.exception.CommonException;
import com.java3y.austin.web.service.MessageTemplateService;
@ -43,6 +44,7 @@ import java.util.stream.Collectors;
* @author 3y
*/
@Slf4j
@AustinAspect
@AustinResult
@RestController
@RequestMapping("/messageTemplate")

@ -6,6 +6,7 @@ import cn.hutool.http.HttpUtil;
import com.google.common.base.Throwables;
import com.java3y.austin.common.enums.RespStatusEnum;
import com.java3y.austin.support.utils.AccountUtils;
import com.java3y.austin.web.annotation.AustinAspect;
import com.java3y.austin.web.annotation.AustinResult;
import com.java3y.austin.web.exception.CommonException;
import com.java3y.austin.web.utils.Convert4Amis;
@ -30,6 +31,7 @@ import java.util.Objects;
* @author 3y
*/
@Slf4j
@AustinAspect
@AustinResult
@RestController
@RequestMapping("/miniProgram")

@ -12,6 +12,7 @@ import com.java3y.austin.common.constant.CommonConstant;
import com.java3y.austin.common.constant.OfficialAccountParamConstant;
import com.java3y.austin.common.enums.RespStatusEnum;
import com.java3y.austin.support.utils.AccountUtils;
import com.java3y.austin.web.annotation.AustinAspect;
import com.java3y.austin.web.annotation.AustinResult;
import com.java3y.austin.web.config.WeChatLoginConfig;
import com.java3y.austin.web.exception.CommonException;
@ -44,6 +45,7 @@ import java.util.*;
* @author 3y
*/
@Slf4j
@AustinAspect
@AustinResult
@RequestMapping("/officialAccount")
@RestController

@ -4,6 +4,7 @@ package com.java3y.austin.web.controller;
import com.java3y.austin.common.enums.ChannelType;
import com.java3y.austin.cron.handler.RefreshDingDingAccessTokenHandler;
import com.java3y.austin.cron.handler.RefreshGeTuiAccessTokenHandler;
import com.java3y.austin.web.annotation.AustinAspect;
import com.java3y.austin.web.annotation.AustinResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@ -16,6 +17,7 @@ import org.springframework.web.bind.annotation.RestController;
* @Author 3y
*/
@AustinResult
@AustinAspect
@Api(tags = {"手动刷新token的接口"})
@RestController
public class RefreshTokenController {

@ -4,6 +4,7 @@ package com.java3y.austin.web.controller;
import com.java3y.austin.service.api.domain.SendRequest;
import com.java3y.austin.service.api.domain.SendResponse;
import com.java3y.austin.service.api.service.SendService;
import com.java3y.austin.web.annotation.AustinAspect;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
@ -17,6 +18,7 @@ import org.springframework.web.bind.annotation.RestController;
@Api(tags = {"发送消息"})
@RestController
@AustinAspect
public class SendController {
@Autowired

@ -0,0 +1,91 @@
package com.java3y.austin.web.vo;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author kl
* @version 1.0.0
* @description Vo
* @date 2023/2/23 9:20
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class RequestLogDTO {
/**
* IDUUID ResponseLogVo id
*/
private String id;
/**
* URI
*/
@JSONField(ordinal = 1)
private String uri;
/**
*
*/
@JSONField(ordinal = 2)
private String method;
/**
*
*/
@JSONField(ordinal = 3)
private Object[] args;
/**
*
*/
@JSONField(ordinal = 4)
private Boolean auth;
/**
*
*/
@JSONField(ordinal = 5)
private String token;
/**
*
*/
@JSONField(ordinal = 6)
private Object loginAccount;
/**
*
*/
@JSONField(ordinal = 7)
private String product;
/**
* +
*/
@JSONField(ordinal = 8)
private String path;
/**
*
*/
@JSONField(ordinal = 9)
private String referer;
/**
*
*/
@JSONField(ordinal = 10)
private String remoteAddr;
/**
*
*/
@JSONField(ordinal = 11)
private String userAgent;
}
Loading…
Cancel
Save