perf(all): 修改为使用fastjson进行json序列化,去除了jackson依赖

This commit is contained in:
wzp 2024-04-08 16:53:41 +08:00
parent 0341293512
commit b4c4833e44
5 changed files with 89 additions and 5 deletions

View File

@ -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")

View File

@ -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<HttpMessageConverter<?>> 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<MediaType> 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);
}
}

View File

@ -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<Object> 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;
}

View File

@ -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<T> {
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 <T> BaseResponse<T> success(T data){
return new BaseResponse<>(200, "接口执行成功", data);

View File

@ -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) {
}