From b8abef9cff9aee1c40522e2321cff698203e3bae Mon Sep 17 00:00:00 2001 From: Rainnny7 Date: Sat, 6 Apr 2024 22:15:19 -0400 Subject: [PATCH] Replace Jackson with Gson --- pom.xml | 8 +++++ src/main/java/me/braydon/mc/RESTfulMC.java | 5 ++-- .../me/braydon/mc/config/WebMvcConfig.java | 29 +++++++++++++++++++ .../mc/model/response/ErrorResponse.java | 2 -- .../mc/model/token/JavaServerStatusToken.java | 1 - 5 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 src/main/java/me/braydon/mc/config/WebMvcConfig.java diff --git a/pom.xml b/pom.xml index 0152b1a..91e3cc4 100644 --- a/pom.xml +++ b/pom.xml @@ -48,6 +48,14 @@ org.springframework.boot spring-boot-starter-web + + + + + org.springframework.boot + spring-boot-starter-json + + diff --git a/src/main/java/me/braydon/mc/RESTfulMC.java b/src/main/java/me/braydon/mc/RESTfulMC.java index 5d9ea44..e506fd9 100644 --- a/src/main/java/me/braydon/mc/RESTfulMC.java +++ b/src/main/java/me/braydon/mc/RESTfulMC.java @@ -8,6 +8,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; @@ -21,11 +22,11 @@ import java.util.Objects; /** * @author Braydon */ -@SpringBootApplication +@SpringBootApplication(exclude = { JacksonAutoConfiguration.class }) @Slf4j(topic = "RESTfulMC") public class RESTfulMC { public static final Gson GSON = new GsonBuilder() - .serializeNulls() + .setDateFormat("MM-dd-yyyy HH:mm:ss") .create(); /** diff --git a/src/main/java/me/braydon/mc/config/WebMvcConfig.java b/src/main/java/me/braydon/mc/config/WebMvcConfig.java new file mode 100644 index 0000000..735062f --- /dev/null +++ b/src/main/java/me/braydon/mc/config/WebMvcConfig.java @@ -0,0 +1,29 @@ +package me.braydon.mc.config; + +import com.google.gson.Gson; +import me.braydon.mc.RESTfulMC; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.json.GsonHttpMessageConverter; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +import java.util.List; + +/** + * @author Braydon + */ +@Configuration +public class WebMvcConfig implements WebMvcConfigurer { + @Override + public void configureMessageConverters(List> converters) { + GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter(); + gsonHttpMessageConverter.setGson(gson()); + converters.add(gsonHttpMessageConverter); + } + + @Bean + public Gson gson() { + return RESTfulMC.GSON; + } +} \ No newline at end of file diff --git a/src/main/java/me/braydon/mc/model/response/ErrorResponse.java b/src/main/java/me/braydon/mc/model/response/ErrorResponse.java index c986c1b..ca26ee7 100644 --- a/src/main/java/me/braydon/mc/model/response/ErrorResponse.java +++ b/src/main/java/me/braydon/mc/model/response/ErrorResponse.java @@ -1,6 +1,5 @@ package me.braydon.mc.model.response; -import com.fasterxml.jackson.annotation.JsonFormat; import lombok.*; import org.springframework.http.HttpStatus; @@ -26,7 +25,6 @@ public final class ErrorResponse { /** * The timestamp this error occurred. */ - @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss") private Date timestamp; public ErrorResponse(@NonNull HttpStatus status, @NonNull String message) { diff --git a/src/main/java/me/braydon/mc/model/token/JavaServerStatusToken.java b/src/main/java/me/braydon/mc/model/token/JavaServerStatusToken.java index 911f727..2f9bb29 100644 --- a/src/main/java/me/braydon/mc/model/token/JavaServerStatusToken.java +++ b/src/main/java/me/braydon/mc/model/token/JavaServerStatusToken.java @@ -1,6 +1,5 @@ package me.braydon.mc.model.token; -import com.fasterxml.jackson.annotation.JsonProperty; import com.google.gson.annotations.SerializedName; import lombok.AllArgsConstructor; import lombok.Getter;