Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cyclone.lunchvoting.config;
- import com.fasterxml.jackson.core.JsonGenerator;
- import com.fasterxml.jackson.core.JsonParser;
- import com.fasterxml.jackson.core.JsonProcessingException;
- import com.fasterxml.jackson.databind.*;
- import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;
- import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import java.io.IOException;
- import java.time.LocalTime;
- import java.time.format.DateTimeFormatter;
- @Configuration
- public class JacksonConfiguration {
- private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");
- @Bean
- public ObjectMapper objectMapper() {
- ObjectMapper objectMapper = new ObjectMapper();
- // http://lewandowski.io/2016/02/formatting-java-time-with-spring-boot-using-json/
- // I hope there is an easier way to set global time format
- JavaTimeModule javaTimeModule = new JavaTimeModule();
- javaTimeModule.addSerializer(LocalTime.class, new JsonSerializer<LocalTime>() {
- @Override
- public void serialize(LocalTime localTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
- jsonGenerator.writeString(localTime.format(TIME_FORMATTER));
- }
- });
- javaTimeModule.addDeserializer(LocalTime.class, new JsonDeserializer<LocalTime>() {
- @Override
- public LocalTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
- return LocalTime.parse(jsonParser.getValueAsString(), TIME_FORMATTER);
- }
- });
- objectMapper.registerModule(javaTimeModule);
- objectMapper.registerModule(new Hibernate5Module());
- return objectMapper;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement