mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-10-31 18:28:43 +08:00 
			
		
		
		
	解决各种 json 库的兼容性问题
This commit is contained in:
		| @@ -5,6 +5,7 @@ import cn.iocoder.dashboard.common.exception.GlobalException; | ||||
| import cn.iocoder.dashboard.common.exception.ServiceException; | ||||
| import cn.iocoder.dashboard.common.exception.enums.GlobalErrorCodeConstants; | ||||
| import com.alibaba.fastjson.annotation.JSONField; | ||||
| import com.fasterxml.jackson.annotation.JsonIgnore; | ||||
| import lombok.Data; | ||||
| import org.springframework.util.Assert; | ||||
|  | ||||
| @@ -68,12 +69,14 @@ public final class CommonResult<T> implements Serializable { | ||||
|         return result; | ||||
|     } | ||||
|  | ||||
|     @JSONField(serialize = false) // 避免序列化 | ||||
|     @JSONField(serialize = false) // 避免 fastjson 序列化 | ||||
|     @JsonIgnore // 避免 jackson 序列化 | ||||
|     public boolean isSuccess() { | ||||
|         return GlobalErrorCodeConstants.SUCCESS.getCode().equals(code); | ||||
|     } | ||||
|  | ||||
|     @JSONField(serialize = false) // 避免序列化 | ||||
|     @JSONField(serialize = false) // 避免 fastjson 序列化 | ||||
|     @JsonIgnore // 避免 jackson 序列化 | ||||
|     public boolean isError() { | ||||
|         return !isSuccess(); | ||||
|     } | ||||
|   | ||||
| @@ -1,9 +1,15 @@ | ||||
| package cn.iocoder.dashboard.framework.web.config; | ||||
|  | ||||
| import cn.iocoder.dashboard.util.servlet.ServletUtils; | ||||
| import com.alibaba.fastjson.serializer.SerializerFeature; | ||||
| import com.alibaba.fastjson.support.config.FastJsonConfig; | ||||
| import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; | ||||
| import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||||
| import org.springframework.context.annotation.Bean; | ||||
| import org.springframework.context.annotation.Configuration; | ||||
| import org.springframework.core.annotation.Order; | ||||
| import org.springframework.http.MediaType; | ||||
| import org.springframework.http.converter.HttpMessageConverter; | ||||
| import org.springframework.web.bind.annotation.RestController; | ||||
| import org.springframework.web.cors.CorsConfiguration; | ||||
| import org.springframework.web.cors.UrlBasedCorsConfigurationSource; | ||||
| @@ -12,6 +18,10 @@ import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; | ||||
| import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||||
|  | ||||
| import javax.annotation.Resource; | ||||
| import javax.servlet.http.HttpServletRequest; | ||||
| import java.nio.charset.Charset; | ||||
| import java.util.Collections; | ||||
| import java.util.List; | ||||
|  | ||||
| /** | ||||
|  * Web 配置类 | ||||
| @@ -27,26 +37,48 @@ public class WebConfiguration implements WebMvcConfigurer { | ||||
|     public void configurePathMatch(PathMatchConfigurer configurer) { | ||||
|         configurer.addPathPrefix(webProperties.getApiPrefix(), clazz -> | ||||
|                 clazz.isAnnotationPresent(RestController.class) | ||||
|                 && clazz.getPackage().getName().contains("cn.iocoder.dashboard")); | ||||
|                 && clazz.getPackage().getName().startsWith(webProperties.getControllerPackage())); | ||||
|     } | ||||
|  | ||||
|     // ========== MessageConverter 相关 ========== | ||||
|  | ||||
| //    @Override | ||||
| //    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { | ||||
| //        // 创建 FastJsonHttpMessageConverter 对象 | ||||
| //        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); | ||||
| //        // 自定义 FastJson 配置 | ||||
| //        FastJsonConfig fastJsonConfig = new FastJsonConfig(); | ||||
| //        fastJsonConfig.setCharset(Charset.defaultCharset()); // 设置字符集 | ||||
| //        fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect, // 剔除循环引用 | ||||
| //                SerializerFeature.WriteNonStringKeyAsString); // 解决 Integer 作为 Key 时,转换为 String 类型,避免浏览器报错 | ||||
| //        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); | ||||
| //        // 设置支持的 MediaType | ||||
| //        fastJsonHttpMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON)); | ||||
| //        // 添加到 converters 中 | ||||
| //        converters.add(0, fastJsonHttpMessageConverter); // 注意,添加到最开头,放在 MappingJackson2XmlHttpMessageConverter 前面 | ||||
| //    } | ||||
|     @Override | ||||
|     public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { | ||||
|         // 创建 FastJsonHttpMessageConverter 对象 | ||||
|         // 重写 canRead 和 canWrite 方法,判断只处理自己写的 API 为前缀的 URL。原因是,FastJSON 和一些三方框架集成存在问题,例如说: | ||||
|         //      1. 与 Spring Boot Admin 时,由于 Registration 基于 Builder 构造对象,导致它无法反序列化 | ||||
|         //      2. 与 Spring Boot Actuator 时,貌似也存在问题,具体还没去排查。 | ||||
|         // 但是,为什么不替换回 Jackson 呢? | ||||
|         // 原因是,一些 Number 数值比较小时,反序列化回来是 Integer 类型,实际是 Long 类型。此时,在序列化时,会报 Integer 无法转换成 Long 的异常 | ||||
|         FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter() { | ||||
|  | ||||
|             @Override | ||||
|             protected boolean canRead(MediaType mediaType) { | ||||
|                 return isApiPrefix() && super.canRead(mediaType); | ||||
|             } | ||||
|  | ||||
|             @Override | ||||
|             protected boolean canWrite(MediaType mediaType) { | ||||
|                 return isApiPrefix() && super.canWrite(mediaType); | ||||
|             } | ||||
|  | ||||
|             private boolean isApiPrefix() { | ||||
|                 HttpServletRequest request = ServletUtils.getRequest(); | ||||
|                 return request != null && request.getRequestURI().startsWith(webProperties.getApiPrefix()); | ||||
|             } | ||||
|  | ||||
|         }; | ||||
|         // 自定义 FastJson 配置 | ||||
|         FastJsonConfig fastJsonConfig = new FastJsonConfig(); | ||||
|         fastJsonConfig.setCharset(Charset.defaultCharset()); // 设置字符集 | ||||
|         fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect, // 剔除循环引用 | ||||
|                 SerializerFeature.WriteNonStringKeyAsString); // 解决 Integer 作为 Key 时,转换为 String 类型,避免浏览器报错 | ||||
|         fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); | ||||
|         // 设置支持的 MediaType | ||||
|         fastJsonHttpMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON)); | ||||
|         // 添加到 converters 中 | ||||
|         converters.add(0, fastJsonHttpMessageConverter); // 注意,添加到最开头,放在 MappingJackson2XmlHttpMessageConverter 前面 | ||||
|     } | ||||
|  | ||||
|     // ========== Filter 相关 ========== | ||||
|  | ||||
|   | ||||
| @@ -24,4 +24,14 @@ public class WebProperties { | ||||
|     @NotNull(message = "API 前缀不能为空") | ||||
|     private String apiPrefix; | ||||
|  | ||||
|     /** | ||||
|      * Controller 所在包 | ||||
|      * | ||||
|      * 主要目的是,给该 Controller 设置指定的 {@link #apiPrefix} | ||||
|      * | ||||
|      * 因为我们有多个 modules 包里会包含 Controller,所以只需要写到 cn.iocoder.dashboard 这样的层级 | ||||
|      */ | ||||
|     @NotNull(message = "Controller 所在包不能为空") | ||||
|     private String controllerPackage; | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -1,5 +1,6 @@ | ||||
| package cn.iocoder.dashboard.modules.system.controller.user.vo.user; | ||||
|  | ||||
| import com.fasterxml.jackson.annotation.JsonIgnore; | ||||
| import io.swagger.annotations.ApiModel; | ||||
| import io.swagger.annotations.ApiModelProperty; | ||||
| import lombok.AllArgsConstructor; | ||||
| @@ -17,6 +18,7 @@ public class SysUserPageItemRespVO extends SysUserRespVO { | ||||
|     /** | ||||
|      * 所在部门 | ||||
|      */ | ||||
|     @JsonIgnore | ||||
|     private Dept dept; | ||||
|  | ||||
|     @ApiModel("部门") | ||||
|   | ||||
| @@ -36,6 +36,7 @@ spring: | ||||
| yudao: | ||||
|   web: | ||||
|     api-prefix: /api | ||||
|     controller-package: cn.iocoder.dashboard | ||||
|   security: | ||||
|     token-header: Authorization | ||||
|     token-secret: abcdefghijklmnopqrstuvwxyz | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 YunaiV
					YunaiV