BAEL-4687: renamed project from jackson to data 2
This commit is contained in:
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.boot.jackson.app;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackages = "com.baeldung.boot.jackson.controller")
|
||||
public class Application {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.boot.jackson.config;
|
||||
|
||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
|
||||
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
public class CoffeeConstants {
|
||||
|
||||
public static final String dateTimeFormat = "dd-MM-yyyy HH:mm";
|
||||
public static LocalDateTimeSerializer localDateTimeSerializer = new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat));
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.boot.jackson.config;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer;
|
||||
|
||||
@Configuration
|
||||
public class CoffeeCustomizerConfig {
|
||||
|
||||
@Bean
|
||||
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
|
||||
return builder -> builder.serializationInclusion(JsonInclude.Include.NON_NULL)
|
||||
.serializers(localDateTimeSerializer);
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.boot.jackson.config;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
|
||||
import static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer;
|
||||
|
||||
@Configuration
|
||||
public class CoffeeHttpConverterConfiguration {
|
||||
|
||||
@Bean
|
||||
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
|
||||
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
|
||||
.serializers(localDateTimeSerializer)
|
||||
.serializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
return new MappingJackson2HttpMessageConverter(builder.build());
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.boot.jackson.config;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
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 static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer;
|
||||
|
||||
@Configuration
|
||||
public class CoffeeJacksonBuilderConfig {
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() {
|
||||
return new Jackson2ObjectMapperBuilder()
|
||||
.serializers(localDateTimeSerializer)
|
||||
.serializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
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 org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
|
||||
import static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer;
|
||||
|
||||
@Configuration
|
||||
public class CoffeeObjectMapperConfig {
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
public ObjectMapper objectMapper() {
|
||||
JavaTimeModule module = new JavaTimeModule();
|
||||
module.addSerializer(localDateTimeSerializer);
|
||||
return new ObjectMapper()
|
||||
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
|
||||
.registerModule(module);
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.boot.jackson.config;
|
||||
|
||||
import com.fasterxml.jackson.databind.Module;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
import static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer;
|
||||
|
||||
@Configuration
|
||||
@PropertySource("classpath:coffee.properties")
|
||||
public class CoffeeRegisterModuleConfig {
|
||||
|
||||
@Bean
|
||||
public Module javaTimeModule() {
|
||||
JavaTimeModule module = new JavaTimeModule();
|
||||
module.addSerializer(localDateTimeSerializer);
|
||||
return module;
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.boot.jackson.controller;
|
||||
|
||||
import com.baeldung.boot.jackson.model.Coffee;
|
||||
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 Coffee getCoffee(@RequestParam(required = false) String brand,
|
||||
@RequestParam(required = false) String name) {
|
||||
return new Coffee()
|
||||
.setBrand(brand)
|
||||
.setDate(LocalDateTime.now())
|
||||
.setName(name);
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.boot.jackson.model;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class Coffee {
|
||||
|
||||
private String name;
|
||||
|
||||
private String brand;
|
||||
|
||||
private LocalDateTime date;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public LocalDateTime getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public Coffee setDate(LocalDateTime date) {
|
||||
this.date = date;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
spring.jackson.default-property-inclusion=non_null
|
||||
Reference in New Issue
Block a user