Skip to content

Commit

Permalink
configure jackson for rest templates
Browse files Browse the repository at this point in the history
  • Loading branch information
rfresh2 committed Nov 4, 2023
1 parent 3c0095f commit 5366e6d
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions src/main/java/vc/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package vc;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import discord4j.core.DiscordClientBuilder;
Expand All @@ -16,8 +17,12 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import org.threeten.bp.OffsetDateTime;

import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

Expand Down Expand Up @@ -51,18 +56,42 @@ public RestClient discordRestClient(GatewayDiscordClient client) {
}

@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
public RestTemplate restTemplate(ObjectMapper objectMapper) {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(0, new MappingJackson2HttpMessageConverter(objectMapper));
return restTemplate;
}

@Bean
@Primary
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.registerModule(new JavaTimeModule());
JavaTimeModule javaTimeModule = new JavaTimeModule();
objectMapper.registerModule(javaTimeModule);
javaTimeModule.addSerializer(OffsetDateTime.class, new OffsetDateTimeSerializer());
javaTimeModule.addDeserializer(OffsetDateTime.class, new OffsetDateTimeDeserializer());
// objectMapper.registerModule(new ThreeTenModule());
objectMapper.registerModule(javaTimeModule);

return objectMapper;
}


public static class OffsetDateTimeSerializer extends JsonSerializer<OffsetDateTime> {
@Override
public void serialize(OffsetDateTime arg0, JsonGenerator arg1, SerializerProvider arg2) throws IOException {
arg1.writeString(arg0.toString());
}
}

public static class OffsetDateTimeDeserializer extends JsonDeserializer<OffsetDateTime> {
@Override
public OffsetDateTime deserialize(JsonParser arg0, DeserializationContext arg1) throws IOException {
return OffsetDateTime.parse(arg0.getText());
}
}

@Bean
public ScheduledExecutorService scheduledExecutorService() {
return Executors.newScheduledThreadPool(4, new ThreadFactoryBuilder()
Expand Down

0 comments on commit 5366e6d

Please sign in to comment.