Commit 2cd27500 by HlQ

[chg]

1.ResBodyAdvice 兼容 vion.controller.mqtt 目录
2.拦截器不拦截 mqtt 相关接口
3.JsonUtil 工具类更新
1 parent 44760b31
...@@ -2,8 +2,6 @@ package vion.advice; ...@@ -2,8 +2,6 @@ package vion.advice;
import cn.dev33.satoken.exception.NotLoginException; import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException; import cn.dev33.satoken.exception.NotPermissionException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.dromara.hutool.core.util.ObjUtil; import org.dromara.hutool.core.util.ObjUtil;
import org.springframework.core.MethodParameter; import org.springframework.core.MethodParameter;
...@@ -18,11 +16,12 @@ import org.springframework.web.bind.annotation.RestControllerAdvice; ...@@ -18,11 +16,12 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import vion.utils.JsonUtil;
import vion.utils.ResultUtil; import vion.utils.ResultUtil;
import vion.vo.ResultVO; import vion.vo.ResultVO;
/** /**
* @author HlQ * @author vion
* @date 2023/11/2 * @date 2023/11/2
*/ */
@Component @Component
...@@ -36,19 +35,19 @@ public class ResBodyAdvice implements ResponseBodyAdvice<Object> { ...@@ -36,19 +35,19 @@ public class ResBodyAdvice implements ResponseBodyAdvice<Object> {
@Override @Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
if (ObjUtil.isNull(body) || body instanceof ResultVO || body instanceof ModelAndView || body instanceof Resource) { if (ObjUtil.isNull(body)
|| body instanceof ResultVO
|| body instanceof ModelAndView
|| body instanceof Resource
|| "vion.controller.mqtt".equalsIgnoreCase(returnType.getDeclaringClass().getPackageName())) {
return body; return body;
} }
var resultVO = ResultUtil.success(body); var resultVO = ResultUtil.success(body);
if (returnType.getParameterType().isAssignableFrom(String.class)) { if (returnType.getParameterType().isAssignableFrom(String.class)) {
// 字符串类型特殊处理 // 字符串类型特殊处理
try { response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
response.getHeaders().setContentType(MediaType.APPLICATION_JSON); return JsonUtil.toJsonString(resultVO);
return new ObjectMapper().writeValueAsString(resultVO);
} catch (JsonProcessingException e) {
log.error("Json 序列化失败");
}
} }
return resultVO; return resultVO;
} }
......
...@@ -28,6 +28,7 @@ public class InterceptorConfig implements WebMvcConfigurer { ...@@ -28,6 +28,7 @@ public class InterceptorConfig implements WebMvcConfigurer {
.excludePathPatterns("/api/getQRCode", "/api/getFollowingOpenid", "/api/verifyScan") .excludePathPatterns("/api/getQRCode", "/api/getFollowingOpenid", "/api/verifyScan")
.excludePathPatterns("/api/point/getBindQRCode", "/api/point/getBindOpenid") .excludePathPatterns("/api/point/getBindQRCode", "/api/point/getBindOpenid")
.excludePathPatterns("/api/point/upd/{uuid}", "/api/point/get/{uuid}", "/api/point/install/submit/{uuid}", "/api/point/client/reject", "/api/point/reject/uuid/{uuid}") .excludePathPatterns("/api/point/upd/{uuid}", "/api/point/get/{uuid}", "/api/point/install/submit/{uuid}", "/api/point/client/reject", "/api/point/reject/uuid/{uuid}")
.excludePathPatterns("/api/sparePart/frontSubmit", "/api/repairRec/frontSubmit"); .excludePathPatterns("/api/sparePart/frontSubmit", "/api/repairRec/frontSubmit")
.excludePathPatterns("/api/mqtt/**", "/api/monitor/**");
} }
} }
...@@ -33,7 +33,7 @@ public interface IContractService extends MPJBaseService<Contract> { ...@@ -33,7 +33,7 @@ public interface IContractService extends MPJBaseService<Contract> {
JsonNode calAmount(ContractDTO dto); JsonNode calAmount(ContractDTO dto);
Map<Integer, Long> statStatus(ContractDTO dto); JsonNode statStatus(ContractDTO dto);
Map<String, Object> analyze(ContractDTO dto); Map<String, Object> analyze(ContractDTO dto);
......
...@@ -9,6 +9,12 @@ import com.fasterxml.jackson.databind.SerializationFeature; ...@@ -9,6 +9,12 @@ import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.dromara.hutool.core.array.ArrayUtil; import org.dromara.hutool.core.array.ArrayUtil;
...@@ -16,6 +22,10 @@ import org.dromara.hutool.core.text.StrUtil; ...@@ -16,6 +22,10 @@ import org.dromara.hutool.core.text.StrUtil;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -29,13 +39,23 @@ public class JsonUtil { ...@@ -29,13 +39,23 @@ public class JsonUtil {
private static ObjectMapper objectMapper = new ObjectMapper(); private static ObjectMapper objectMapper = new ObjectMapper();
private static final String dateTimeFormat = "yyyy-MM-dd HH:mm:ss";
private static final String dateFormat = "yyyy-MM-dd";
private static final String timeFormat = "HH:mm:ss";
static { static {
var module = new JavaTimeModule();
module.addSerializer(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat)));
module.addSerializer(new LocalDateSerializer(DateTimeFormatter.ofPattern(dateFormat)));
module.addSerializer(new LocalTimeSerializer(DateTimeFormatter.ofPattern(timeFormat)));
module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(dateTimeFormat)));
module.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(dateFormat)));
module.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(timeFormat)));
objectMapper.registerModule(module);
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 忽略 null 值 // 忽略 null 值
// objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); // objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
// 解决 LocalDateTime 的序列化
objectMapper.registerModules(new JavaTimeModule());
} }
/** /**
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!