BAEL-4687: added spring boot jackson configuration example

This commit is contained in:
Adina Rolea 2020-10-27 15:47:40 +02:00
parent d48defc3e2
commit 9b8455e829
9 changed files with 191 additions and 0 deletions

View File

@ -68,6 +68,7 @@
<module>spring-boot-vue</module>
<module>spring-boot-xml</module>
<module>spring-boot-actuator</module>
<module>spring-boot-jackson</module>
</modules>

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>parent-boot-2</artifactId>
<groupId>com.baeldung</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-2</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-jackson</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,12 @@
package com.baeldung.boot.jackson;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.boot.jackson.config;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import java.time.format.DateTimeFormatter;
@Configuration
public class CoffeeConfiguration {
public static final String dateTimeFormat = "dd-MM-yyyy HH:mm";
private LocalDateTimeSerializer localDateTimeSerializer = new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat));
@Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
return builder -> builder.serializationInclusion(JsonInclude.Include.NON_NULL)
.serializers(localDateTimeSerializer);
}
@Bean
@Primary
public ObjectMapper objectMapper() {
JavaTimeModule module = new JavaTimeModule();
module.addSerializer(localDateTimeSerializer);
return new ObjectMapper()
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
.registerModule(module);
}
@Bean
@Primary
public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() {
return new Jackson2ObjectMapperBuilder()
.serializers(localDateTimeSerializer)
.serializationInclusion(JsonInclude.Include.NON_NULL);
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.boot.jackson.controller;
import com.baeldung.boot.jackson.model.Coffee;
import com.baeldung.boot.jackson.model.CoffeeResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
@RestController
public class CoffeeController {
@GetMapping("/coffee")
public CoffeeResponse<Coffee> createCoffee(@RequestParam(required = false) String brand,
@RequestParam(required = false) String name) {
Coffee coffee = new Coffee()
.setBrand(brand)
.setName(name);
return new CoffeeResponse<Coffee>()
.setDate(LocalDateTime.now())
.setBody(coffee);
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.boot.jackson.model;
public class Coffee {
private String name;
private String brand;
public String getName() {
return name;
}
public Coffee setName(String name) {
this.name = name;
return this;
}
public String getBrand() {
return brand;
}
public Coffee setBrand(String brand) {
this.brand = brand;
return this;
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.boot.jackson.model;
import java.time.LocalDateTime;
public class CoffeeResponse<T> {
private LocalDateTime date;
private T body;
public LocalDateTime getDate() {
return date;
}
public CoffeeResponse<T> setDate(LocalDateTime date) {
this.date = date;
return this;
}
public T getBody() {
return body;
}
public CoffeeResponse<T> setBody(T body) {
this.body = body;
return this;
}
}

View File

@ -0,0 +1,3 @@
spring.jackson.default-property-inclusion=non_null
spring.jackson.serialization.write-dates-as-timestamps=false
spring.jackson.date-format=dd-MM-yyyy HH:mm

View File

@ -0,0 +1,32 @@
package com.baeldung.boot.jackson;
import com.baeldung.boot.jackson.config.CoffeeConfiguration;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class CoffeeIntegrationTest {
@Autowired
TestRestTemplate restTemplate;
@Test
public void whenQueryCoffeeWithoutParam_thenNullIsNotInserted() {
String formattedDate = DateTimeFormatter.ofPattern(CoffeeConfiguration.dateTimeFormat)
.format(LocalDateTime.now());
String brand = "Lavazza";
String url = "/coffee?brand=" + brand;
String response = restTemplate.getForObject(url, String.class);
assertThat(response).isEqualTo(
"{\"date\":\"" + formattedDate + "\",\"body\":{\"brand\":\"" + brand + "\"}}");
}
}