diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/web/domain/SpringBootMvcsModulesFactory.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/web/domain/SpringBootMvcsModulesFactory.java index a7e6cb5b370..e45e630ecfa 100644 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/web/domain/SpringBootMvcsModulesFactory.java +++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/web/domain/SpringBootMvcsModulesFactory.java @@ -23,6 +23,10 @@ public class SpringBootMvcsModulesFactory { private static final JHipsterSource MAIN_SOURCE = SOURCE.append("main"); private static final JHipsterSource TEST_SOURCE = SOURCE.append("test"); + private static final JHipsterSource JACKSON_MAIN_SOURCE = from("server/springboot/jackson/main"); + private static final JHipsterSource JACKSON_TEST_SOURCE = from("server/springboot/jackson/test"); + private static final String WIRE_JACKSON_CONFIG = "wire/jackson/infrastructure/config"; + private static final GroupId SPRING_BOOT_GROUP = groupId("org.springframework.boot"); private static final ArtifactId STARTER_WEB_ARTIFACT_ID = artifactId("spring-boot-starter-web"); @@ -84,11 +88,14 @@ private JHipsterModuleBuilder springMvcBuilder(JHipsterModuleProperties properti .and() .springMainProperties() .set(SERVER_PORT, propertyValue(properties.serverPort().get())) + .set(propertyKey("spring.jackson.default-property-inclusion"), propertyValue("non_absent")) .and() .springTestProperties() .set(SERVER_PORT, propertyValue(0)) .and() .files() + .add(JACKSON_MAIN_SOURCE.append(WIRE_JACKSON_CONFIG).template("JacksonConfiguration.java"), toSrcMainJava().append(packagePath).append(WIRE_JACKSON_CONFIG).append("JacksonConfiguration.java")) + .add(JACKSON_TEST_SOURCE.append(WIRE_JACKSON_CONFIG).template("JacksonConfigurationIT.java"), toSrcTestJava().append(packagePath).append(WIRE_JACKSON_CONFIG).append("JacksonConfigurationIT.java")) .add(SOURCE.file("resources/404.html"), to("src/main/resources/public/error/404.html")) .batch(MAIN_SOURCE.append(CORS), mainDestination.append(CORS_PRIMARY)) .addTemplate("CorsFilterConfiguration.java") diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/webflux/web/domain/SpringBootWebfluxModuleFactory.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/webflux/web/domain/SpringBootWebfluxModuleFactory.java index 9c2cb0e0264..5292e8c7194 100644 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/webflux/web/domain/SpringBootWebfluxModuleFactory.java +++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/webflux/web/domain/SpringBootWebfluxModuleFactory.java @@ -14,7 +14,9 @@ public class SpringBootWebfluxModuleFactory { private static final JHipsterSource SOURCE = from("server/springboot/webflux/web"); - + private static final JHipsterSource JACKSON_MAIN_SOURCE = from("server/springboot/jackson/main"); + private static final JHipsterSource JACKSON_TEST_SOURCE = from("server/springboot/jackson/test"); + private static final String WIRE_JACKSON_CONFIG = "wire/jackson/infrastructure/config"; private static final PropertyKey SERVER_PORT = propertyKey("server.port"); private static final GroupId SPRING_GROUP = groupId("org.springframework.boot"); @@ -39,11 +41,14 @@ public JHipsterModule buildNettyModule(JHipsterModuleProperties properties) { .and() .springMainProperties() .set(SERVER_PORT, propertyValue(properties.serverPort().get())) + .set(propertyKey("spring.jackson.default-property-inclusion"), propertyValue("non_absent")) .and() .springTestProperties() .set(SERVER_PORT, propertyValue(0)) .and() .files() + .add(JACKSON_MAIN_SOURCE.append(WIRE_JACKSON_CONFIG).template("JacksonConfiguration.java"), toSrcMainJava().append(packagePath).append(WIRE_JACKSON_CONFIG).append("JacksonConfiguration.java")) + .add(JACKSON_TEST_SOURCE.append(WIRE_JACKSON_CONFIG).template("JacksonConfigurationIT.java"), toSrcTestJava().append(packagePath).append(WIRE_JACKSON_CONFIG).append("JacksonConfigurationIT.java")) .batch(SOURCE.append("main"), toSrcMainJava().append(packagePath).append(EXCEPTION_PRIMARY)) .addTemplate("FieldErrorDTO.java") .addTemplate("HeaderUtil.java") diff --git a/src/main/resources/config/application.yml b/src/main/resources/config/application.yml index 364a4bbc1cf..eac327a21a5 100644 --- a/src/main/resources/config/application.yml +++ b/src/main/resources/config/application.yml @@ -80,6 +80,6 @@ spring: virtual: enabled: true jackson: - default-property-inclusion: non_null + default-property-inclusion: non_absent application: name: jhlite diff --git a/src/main/resources/generator/server/springboot/jackson/main/wire/jackson/infrastructure/config/JacksonConfiguration.java.mustache b/src/main/resources/generator/server/springboot/jackson/main/wire/jackson/infrastructure/config/JacksonConfiguration.java.mustache new file mode 100644 index 00000000000..1e29f74c579 --- /dev/null +++ b/src/main/resources/generator/server/springboot/jackson/main/wire/jackson/infrastructure/config/JacksonConfiguration.java.mustache @@ -0,0 +1,20 @@ +package {{packageName}}.wire.jackson.infrastructure.config; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +class JacksonConfiguration { + + @Bean + public ObjectMapper objectMapper() { + return new ObjectMapper(); + } + + @Bean + public ObjectMapper objectMapperOptional() { + return new ObjectMapper().registerModule(new Jdk8Module()); + } +} diff --git a/src/main/resources/generator/server/springboot/jackson/test/wire/jackson/infrastructure/config/JacksonConfigurationIT.java.mustache b/src/main/resources/generator/server/springboot/jackson/test/wire/jackson/infrastructure/config/JacksonConfigurationIT.java.mustache new file mode 100644 index 00000000000..23025defbb2 --- /dev/null +++ b/src/main/resources/generator/server/springboot/jackson/test/wire/jackson/infrastructure/config/JacksonConfigurationIT.java.mustache @@ -0,0 +1,23 @@ +package {{packageName}}.wire.jackson.infrastructure.config; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import {{packageName}}.IntegrationTest; +import java.util.Optional; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +@IntegrationTest +class JacksonConfigurationIT { + + @Autowired + private ObjectMapper objectMapperOptional; + + @Test + void shouldHandleOptional() throws JsonProcessingException { + Optional optional = Optional.of("test"); + assertThat(objectMapperOptional.writeValueAsString(optional)).isEqualTo("\"test\""); + } +} diff --git a/src/test/features/server/springboot/spring-mvc.feature b/src/test/features/server/springboot/spring-mvc.feature index 199a2b6e918..7520d2e5b05 100644 --- a/src/test/features/server/springboot/spring-mvc.feature +++ b/src/test/features/server/springboot/spring-mvc.feature @@ -7,6 +7,10 @@ Feature: Spring MVC | spring-boot-mvc-empty | | spring-boot-tomcat | Then I should have "spring-boot-starter-web" in "pom.xml" + Then I should have files in "src/main/java/tech/jhipster/chips/wire/jackson/infrastructure/config" + | JacksonConfiguration.java | + Then I should have files in "src/test/java/tech/jhipster/chips/wire/jackson/infrastructure/config" + | JacksonConfigurationIT.java | Scenario: Should apply spring mvc undertow module When I apply modules to default project @@ -15,3 +19,7 @@ Feature: Spring MVC | spring-boot-mvc-empty | | spring-boot-undertow | Then I should have "spring-boot-starter-undertow" in "pom.xml" + Then I should have files in "src/main/java/tech/jhipster/chips/wire/jackson/infrastructure/config" + | JacksonConfiguration.java | + Then I should have files in "src/test/java/tech/jhipster/chips/wire/jackson/infrastructure/config" + | JacksonConfigurationIT.java | diff --git a/src/test/features/server/springboot/webflux.feature b/src/test/features/server/springboot/webflux.feature index 76e34a50902..b8c37ec3032 100644 --- a/src/test/features/server/springboot/webflux.feature +++ b/src/test/features/server/springboot/webflux.feature @@ -9,3 +9,7 @@ Feature: Webflux module Then I should have "spring-boot-starter-webflux" in "pom.xml" Then I should have files in "src/main/java/tech/jhipster/chips/shared/error/infrastructure/primary" | FieldErrorDTO.java | + Then I should have files in "src/main/java/tech/jhipster/chips/wire/jackson/infrastructure/config" + | JacksonConfiguration.java | + Then I should have files in "src/test/java/tech/jhipster/chips/wire/jackson/infrastructure/config" + | JacksonConfigurationIT.java | diff --git a/src/test/java/tech/jhipster/lite/generator/server/springboot/mvc/web/domain/SpringBootMvcsModulesFactoryTest.java b/src/test/java/tech/jhipster/lite/generator/server/springboot/mvc/web/domain/SpringBootMvcsModulesFactoryTest.java index 6292f7b92fc..51009b6b632 100644 --- a/src/test/java/tech/jhipster/lite/generator/server/springboot/mvc/web/domain/SpringBootMvcsModulesFactoryTest.java +++ b/src/test/java/tech/jhipster/lite/generator/server/springboot/mvc/web/domain/SpringBootMvcsModulesFactoryTest.java @@ -103,6 +103,9 @@ private JHipsterModuleAsserter assertMvcModule(JHipsterModule module) { """ server: port: 9000 + spring: + jackson: + default-property-inclusion: non_absent """ ) .and() @@ -145,6 +148,12 @@ private JHipsterModuleAsserter assertMvcModule(JHipsterModule module) { """ ) + .and() + .hasFile("src/main/java/tech/jhipster/jhlitest/wire/jackson/infrastructure/config/JacksonConfiguration.java") + .containing("public ObjectMapper objectMapper()") + .containing("public ObjectMapper objectMapperOptional()") + .and() + .hasFile("src/test/java/tech/jhipster/jhlitest/wire/jackson/infrastructure/config/JacksonConfigurationIT.java") .and(); } } diff --git a/src/test/java/tech/jhipster/lite/generator/server/springboot/webflux/web/domain/SpringBootWebfluxModuleFactoryTest.java b/src/test/java/tech/jhipster/lite/generator/server/springboot/webflux/web/domain/SpringBootWebfluxModuleFactoryTest.java index b0b2f0c3ad0..80cc68c1d2c 100644 --- a/src/test/java/tech/jhipster/lite/generator/server/springboot/webflux/web/domain/SpringBootWebfluxModuleFactoryTest.java +++ b/src/test/java/tech/jhipster/lite/generator/server/springboot/webflux/web/domain/SpringBootWebfluxModuleFactoryTest.java @@ -56,6 +56,9 @@ void shouldBuildWebfluxNettyModule() { """ server: port: 9000 + spring: + jackson: + default-property-inclusion: non_absent """ ) .and() @@ -67,6 +70,12 @@ void shouldBuildWebfluxNettyModule() { """ ) .and() + .hasFile("src/main/java/tech/jhipster/jhlitest/wire/jackson/infrastructure/config/JacksonConfiguration.java") + .containing("public ObjectMapper objectMapper()") + .containing("public ObjectMapper objectMapperOptional()") + .and() + .hasFile("src/test/java/tech/jhipster/jhlitest/wire/jackson/infrastructure/config/JacksonConfigurationIT.java") + .and() .hasPrefixedFiles("src/main/java/tech/jhipster/jhlitest/shared/error/infrastructure/primary", "HeaderUtil.java", "FieldErrorDTO.java") .hasPrefixedFiles( "src/test/java/tech/jhipster/jhlitest/shared/error/infrastructure/primary",