From b4c4833e44c352eaebdab13431a91702ccde87b7 Mon Sep 17 00:00:00 2001 From: wzp Date: Mon, 8 Apr 2024 16:53:41 +0800 Subject: [PATCH] =?UTF-8?q?perf(all):=20=E4=BF=AE=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E4=BD=BF=E7=94=A8fastjson=E8=BF=9B=E8=A1=8Cjson=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E5=8C=96=EF=BC=8C=E5=8E=BB=E9=99=A4=E4=BA=86jackson?= =?UTF-8?q?=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 8 +- .../configuration/FastJsonConfiguration.java | 73 +++++++++++++++++++ .../configuration/TokenConfiguration.java | 5 +- .../org/mmga/clubs/entities/BaseResponse.java | 6 +- .../mmga/clubs/entities/user/UserLoginVo.java | 2 +- 5 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 src/main/java/org/mmga/clubs/configuration/FastJsonConfiguration.java diff --git a/build.gradle.kts b/build.gradle.kts index d12190c..1b94cda 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -33,7 +33,9 @@ extra["springShellVersion"] = "3.2.3" dependencies { implementation("org.springframework.boot:spring-boot-starter-actuator") - implementation("org.springframework.boot:spring-boot-starter-web") + implementation("org.springframework.boot:spring-boot-starter-web") { + exclude("com.fasterxml.jackson.core", "jackson-core") + } implementation("org.springframework.boot:spring-boot-starter-websocket") implementation("org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.3") implementation("org.springframework.shell:spring-shell-starter") @@ -42,6 +44,10 @@ dependencies { // https://mvnrepository.com/artifact/commons-codec/commons-codec implementation("commons-codec:commons-codec:1.16.1") // https://mvnrepository.com/artifact/com.auth0/java-jwt + // https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2 + implementation("com.alibaba.fastjson2:fastjson2:2.0.48") +// https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2-extension-spring6 + implementation("com.alibaba.fastjson2:fastjson2-extension-spring6:2.0.48") implementation("com.auth0:java-jwt:4.4.0") compileOnly("org.projectlombok:lombok") developmentOnly("org.springframework.boot:spring-boot-devtools") diff --git a/src/main/java/org/mmga/clubs/configuration/FastJsonConfiguration.java b/src/main/java/org/mmga/clubs/configuration/FastJsonConfiguration.java new file mode 100644 index 0000000..1021ea9 --- /dev/null +++ b/src/main/java/org/mmga/clubs/configuration/FastJsonConfiguration.java @@ -0,0 +1,73 @@ +package org.mmga.clubs.configuration; + +import com.alibaba.fastjson2.JSONWriter; +import com.alibaba.fastjson2.support.config.FastJsonConfig; +import com.alibaba.fastjson2.support.spring6.http.converter.FastJsonHttpMessageConverter; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.MediaType; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; + +@Configuration +public class FastJsonConfiguration implements WebMvcConfigurer { + /** + * 配置fastjson输出格式 + **/ + @Override + public void configureMessageConverters(List> converters) { + // 1. 配置fastjson + FastJsonConfig config = new FastJsonConfig(); + + config.setDateFormat("yyyy-MM-dd HH:mm:ss"); + config.setCharset(StandardCharsets.UTF_8); + config.setWriterFeatures( + JSONWriter.Feature.WriteNullListAsEmpty, + //json格式化 + JSONWriter.Feature.PrettyFormat, + //输出map中value为null的数据 + JSONWriter.Feature.WriteMapNullValue, + //输出boolean 为 false + JSONWriter.Feature.WriteNullBooleanAsFalse, + //输出list 为 [] + JSONWriter.Feature.WriteNullListAsEmpty, + //输出number 为 0 + JSONWriter.Feature.WriteNullNumberAsZero, + //输出字符串 为 "" + JSONWriter.Feature.WriteNullStringAsEmpty, + //对map进行排序 + JSONWriter.Feature.SortMapEntriesByKeys + ); + + // 2. 添加fastjson转换器 + FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); + List supportedMediaTypes = new ArrayList<>(); + + // 3. 添加支持的媒体类型 + supportedMediaTypes.add(MediaType.APPLICATION_JSON); + supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML); + supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED); + supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM); + supportedMediaTypes.add(MediaType.APPLICATION_PDF); + supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML); + supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML); + supportedMediaTypes.add(MediaType.APPLICATION_XML); + supportedMediaTypes.add(MediaType.IMAGE_GIF); + supportedMediaTypes.add(MediaType.IMAGE_JPEG); + supportedMediaTypes.add(MediaType.IMAGE_PNG); + supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM); + supportedMediaTypes.add(MediaType.TEXT_HTML); + supportedMediaTypes.add(MediaType.TEXT_MARKDOWN); + supportedMediaTypes.add(MediaType.TEXT_PLAIN); + supportedMediaTypes.add(MediaType.TEXT_XML); + + converter.setSupportedMediaTypes(supportedMediaTypes); + + //4 将convert添加到converters + converter.setFastJsonConfig(config); + converters.add(0,converter); + } +} \ No newline at end of file diff --git a/src/main/java/org/mmga/clubs/configuration/TokenConfiguration.java b/src/main/java/org/mmga/clubs/configuration/TokenConfiguration.java index 6361dc0..7bd8471 100644 --- a/src/main/java/org/mmga/clubs/configuration/TokenConfiguration.java +++ b/src/main/java/org/mmga/clubs/configuration/TokenConfiguration.java @@ -1,5 +1,6 @@ package org.mmga.clubs.configuration; +import com.alibaba.fastjson2.JSON; import com.auth0.jwt.exceptions.JWTVerificationException; import jakarta.servlet.ServletOutputStream; import jakarta.servlet.http.HttpServletRequest; @@ -35,9 +36,9 @@ public class TokenConfiguration implements HandlerInterceptor, WebMvcConfigurer{ log.debug("用户鉴权时出现错误:", e); ServletOutputStream outputStream = response.getOutputStream(); response.addHeader("Content-Encoding", "UTF-8"); - //TODO 使用fastjson2 进行json序列化 + response.addHeader("Content-Type", "application/json; charset=utf-8"); BaseResponse err = BaseResponse.failed(401, "token错误"); - outputStream.write(err.toString().getBytes(StandardCharsets.UTF_8)); + outputStream.write(JSON.toJSONString(err).getBytes(StandardCharsets.UTF_8)); outputStream.close(); return false; } diff --git a/src/main/java/org/mmga/clubs/entities/BaseResponse.java b/src/main/java/org/mmga/clubs/entities/BaseResponse.java index 65dc0b7..f1a0d3d 100644 --- a/src/main/java/org/mmga/clubs/entities/BaseResponse.java +++ b/src/main/java/org/mmga/clubs/entities/BaseResponse.java @@ -2,7 +2,8 @@ package org.mmga.clubs.entities; import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; -import org.mmga.clubs.entities.user.User; + +import java.util.Date; @Data @Schema(description = "基础返回值") @@ -11,12 +12,15 @@ public class BaseResponse { private int code; @Schema(description = "返回信息(自定义)") private String msg; + @Schema(description = "响应生成时间") + private final Date time; @Schema(description = "返回数据") private T data; private BaseResponse(int code, String msg, T data){ this.code = code; this.msg = msg; this.data = data; + this.time = new Date(); } public static BaseResponse success(T data){ return new BaseResponse<>(200, "接口执行成功", data); diff --git a/src/main/java/org/mmga/clubs/entities/user/UserLoginVo.java b/src/main/java/org/mmga/clubs/entities/user/UserLoginVo.java index 1174fcb..7883577 100644 --- a/src/main/java/org/mmga/clubs/entities/user/UserLoginVo.java +++ b/src/main/java/org/mmga/clubs/entities/user/UserLoginVo.java @@ -3,5 +3,5 @@ package org.mmga.clubs.entities.user; import io.swagger.v3.oas.annotations.media.Schema; @Schema(description = "用户登录参数类") -public record UserLoginVo(@Schema(description = "用户名") String username,@Schema(description = "用户密码(使用MD5摘要后的hex字符串)") String password) { +public record UserLoginVo(@Schema(description = "用户名", requiredMode = Schema.RequiredMode.REQUIRED) String username, @Schema(description = "用户密码(使用MD5摘要后的hex字符串)") String password) { }