From bf4e71011e4fd1655ac9497760bf2236b1b48026 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Mon, 10 Apr 2017 21:01:13 -0500 Subject: [PATCH 1/7] Add Jsonb support Spring Framework 5 will support Jsonb as a HttpMessageConverter, this commit adds auto-configuration support. Also, support for Jsonb in the @JsonTest has been added. This implementation is running against Yasson (RI for Jsonb). --- spring-boot-autoconfigure/pom.xml | 16 +++ ...sonHttpMessageConvertersConfiguration.java | 28 +++- ...ttpMessageConvertersAutoConfiguration.java | 8 +- ...onbHttpMessageConvertersConfiguration.java | 94 +++++++++++++ .../jsonb/JsonbAutoConfiguration.java | 44 ++++++ .../autoconfigure/jsonb/package-info.java | 20 +++ ...itional-spring-configuration-metadata.json | 4 + .../main/resources/META-INF/spring.factories | 1 + ...ssageConvertersAutoConfigurationTests.java | 73 +++++++++- .../jsonb/JsonbAutoConfigurationTests.java | 72 ++++++++++ spring-boot-dependencies/pom.xml | 12 ++ spring-boot-test-autoconfigure/pom.xml | 16 +++ .../json/AutoConfigureJsonTesters.java | 5 +- .../test/autoconfigure/json/JsonTest.java | 6 +- .../json/JsonTestersAutoConfiguration.java | 20 ++- .../main/resources/META-INF/spring.factories | 10 +- .../json/JsonTestIntegrationTests.java | 12 ++ ...TestWithAutoConfigureJsonTestersTests.java | 9 ++ ...TestWithAutoConfigureJsonTestersTests.java | 4 + spring-boot-test/pom.xml | 16 +++ .../boot/test/json/JsonbTester.java | 128 ++++++++++++++++++ .../boot/test/json/JsonbTesterTests.java | 89 ++++++++++++ 22 files changed, 671 insertions(+), 16 deletions(-) create mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/http/JsonbHttpMessageConvertersConfiguration.java create mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jsonb/JsonbAutoConfiguration.java create mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jsonb/package-info.java create mode 100644 spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jsonb/JsonbAutoConfigurationTests.java create mode 100644 spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonbTester.java create mode 100644 spring-boot-test/src/test/java/org/springframework/boot/test/json/JsonbTesterTests.java diff --git a/spring-boot-autoconfigure/pom.xml b/spring-boot-autoconfigure/pom.xml index fbedf78fd2ec..6956f379ddc0 100755 --- a/spring-boot-autoconfigure/pom.xml +++ b/spring-boot-autoconfigure/pom.xml @@ -120,6 +120,11 @@ cache-api true + + javax.json.bind + javax.json.bind-api + true + io.searchbox jest @@ -274,6 +279,11 @@ javax-websocket-server-impl true + + org.glassfish + javax.json + true + io.undertow undertow-servlet @@ -728,6 +738,12 @@ mysql-connector-java test + + org.eclipse + yasson + ${javax-jsonb.version} + test + org.hsqldb hsqldb diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/http/GsonHttpMessageConvertersConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/http/GsonHttpMessageConvertersConfiguration.java index b5d454834b7f..5d10366d2ea2 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/http/GsonHttpMessageConvertersConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/http/GsonHttpMessageConvertersConfiguration.java @@ -23,6 +23,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.autoconfigure.condition.NoneNestedConditions; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; @@ -33,6 +34,7 @@ * Configuration for HTTP Message converters that use Gson. * * @author Andy Wilkinson + * @author Eddú Meléndez * @since 1.2.2 */ @Configuration @@ -41,7 +43,7 @@ class GsonHttpMessageConvertersConfiguration { @Configuration @ConditionalOnBean(Gson.class) - @Conditional(PreferGsonOrMissingJacksonCondition.class) + @Conditional(PreferGsonOrMissingJacksonAndJsonbCondition.class) protected static class GsonHttpMessageConverterConfiguration { @Bean @@ -54,9 +56,9 @@ public GsonHttpMessageConverter gsonHttpMessageConverter(Gson gson) { } - private static class PreferGsonOrMissingJacksonCondition extends AnyNestedCondition { + private static class PreferGsonOrMissingJacksonAndJsonbCondition extends AnyNestedCondition { - PreferGsonOrMissingJacksonCondition() { + PreferGsonOrMissingJacksonAndJsonbCondition() { super(ConfigurationPhase.REGISTER_BEAN); } @@ -65,11 +67,29 @@ static class GsonPreferred { } - @ConditionalOnMissingBean(MappingJackson2HttpMessageConverter.class) + @Conditional(JacksonAndJsonbMissing.class) + static class JacksonJsonbMissing { + + } + + } + + private static class JacksonAndJsonbMissing extends NoneNestedConditions { + + JacksonAndJsonbMissing() { + super(ConfigurationPhase.REGISTER_BEAN); + } + + @ConditionalOnBean(MappingJackson2HttpMessageConverter.class) static class JacksonMissing { } + @ConditionalOnProperty(name = HttpMessageConvertersAutoConfiguration.PREFERRED_MAPPER_PROPERTY, havingValue = "jsonb") + static class JsonbMissing { + + } + } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/http/HttpMessageConvertersAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/http/HttpMessageConvertersAutoConfiguration.java index f962e7673df4..7cb9ae218f53 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/http/HttpMessageConvertersAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/http/HttpMessageConvertersAutoConfiguration.java @@ -26,6 +26,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration; import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; +import org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -44,12 +45,15 @@ * @author Andy Wilkinson * @author Sebastien Deleuze * @author Stephane Nicoll + * @author Eddú Meléndez */ @Configuration @ConditionalOnClass(HttpMessageConverter.class) -@AutoConfigureAfter({ GsonAutoConfiguration.class, JacksonAutoConfiguration.class }) +@AutoConfigureAfter({ GsonAutoConfiguration.class, JacksonAutoConfiguration.class, + JsonbAutoConfiguration.class }) @Import({ JacksonHttpMessageConvertersConfiguration.class, - GsonHttpMessageConvertersConfiguration.class }) + GsonHttpMessageConvertersConfiguration.class, + JsonbHttpMessageConvertersConfiguration.class }) public class HttpMessageConvertersAutoConfiguration { static final String PREFERRED_MAPPER_PROPERTY = "spring.http.converters.preferred-json-mapper"; diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/http/JsonbHttpMessageConvertersConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/http/JsonbHttpMessageConvertersConfiguration.java new file mode 100644 index 000000000000..9e19dab0938c --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/http/JsonbHttpMessageConvertersConfiguration.java @@ -0,0 +1,94 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.http; + +import javax.json.bind.Jsonb; + +import org.springframework.boot.autoconfigure.condition.AnyNestedCondition; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.autoconfigure.condition.NoneNestedConditions; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Conditional; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.converter.json.JsonbHttpMessageConverter; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; + +/** + * Configuration for HTTP Message converters that use JSON-B. + * + * @author Eddú Meléndez + * @author 2.0.0 + */ +@Configuration +@ConditionalOnClass(Jsonb.class) +class JsonbHttpMessageConvertersConfiguration { + + @Configuration + @ConditionalOnBean(Jsonb.class) + @Conditional(PreferJsonbOrMissingJacksonAndGsonCondition.class) + protected static class JsonbHttpMessageConverterConfiguration { + + @Bean + @ConditionalOnMissingBean + public JsonbHttpMessageConverter jsonbHttpMessageConverter(Jsonb jsonb) { + JsonbHttpMessageConverter converter = new JsonbHttpMessageConverter(); + converter.setJsonb(jsonb); + return converter; + } + + } + + private static class PreferJsonbOrMissingJacksonAndGsonCondition extends AnyNestedCondition { + + PreferJsonbOrMissingJacksonAndGsonCondition() { + super(ConfigurationPhase.REGISTER_BEAN); + } + + @ConditionalOnProperty(name = HttpMessageConvertersAutoConfiguration.PREFERRED_MAPPER_PROPERTY, havingValue = "jsonb", matchIfMissing = false) + static class JsonbPreferred { + + } + + @Conditional(JacksonAndGsonMissing.class) + static class JacksonGsonMissing { + + } + + } + + private static class JacksonAndGsonMissing extends NoneNestedConditions { + + JacksonAndGsonMissing() { + super(ConfigurationPhase.REGISTER_BEAN); + } + + @ConditionalOnBean(MappingJackson2HttpMessageConverter.class) + static class JacksonMissing { + + } + + @ConditionalOnProperty(name = HttpMessageConvertersAutoConfiguration.PREFERRED_MAPPER_PROPERTY, havingValue = "gson") + static class GsonMissing { + + } + + } + +} diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jsonb/JsonbAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jsonb/JsonbAutoConfiguration.java new file mode 100644 index 000000000000..8b20f6425523 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jsonb/JsonbAutoConfiguration.java @@ -0,0 +1,44 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.jsonb; + +import javax.json.bind.Jsonb; +import javax.json.bind.JsonbBuilder; + +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * {@link EnableAutoConfiguration Auto-configuration} for JSON-B. + * + * @author Eddú Meléndez + * @since 2.0.0 + */ +@Configuration +@ConditionalOnClass(Jsonb.class) +public class JsonbAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + public Jsonb jsonb() { + return JsonbBuilder.create(); + } + +} diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jsonb/package-info.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jsonb/package-info.java new file mode 100644 index 000000000000..a812ace6ca76 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jsonb/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Auto-configuration for JSON-B. + */ +package org.springframework.boot.autoconfigure.jsonb; diff --git a/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 04bf53776c35..d89c46607d5a 100644 --- a/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -572,12 +572,16 @@ }, { "name": "spring.http.converters.preferred-json-mapper", + "defaultValue" : "jackson", "values": [ { "value": "gson" }, { "value": "jackson" + }, + { + "value": "jsonb" } ], "providers": [ diff --git a/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories index 21846af9f047..98fa0b4ddf2b 100644 --- a/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories @@ -80,6 +80,7 @@ org.springframework.boot.autoconfigure.jms.artemis.ArtemisAutoConfiguration,\ org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\ org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration,\ org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration,\ +org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration,\ org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration,\ org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration,\ org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration,\ diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/http/HttpMessageConvertersAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/http/HttpMessageConvertersAutoConfigurationTests.java index 592eb342433b..9ccfe0a29c90 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/http/HttpMessageConvertersAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/http/HttpMessageConvertersAutoConfigurationTests.java @@ -19,6 +19,8 @@ import java.util.Arrays; import java.util.List; +import javax.json.bind.Jsonb; + import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; import org.junit.After; @@ -28,6 +30,7 @@ import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration; import org.springframework.boot.autoconfigure.http.JacksonHttpMessageConvertersConfiguration.MappingJackson2HttpMessageConverterConfiguration; import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; +import org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration; import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; @@ -38,6 +41,7 @@ import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.http.converter.json.GsonHttpMessageConverter; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; +import org.springframework.http.converter.json.JsonbHttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter; @@ -51,6 +55,7 @@ * @author David Liu * @author Andy Wilkinson * @author Sebastien Deleuze + * @author Eddú Meléndez */ public class HttpMessageConvertersAutoConfigurationTests { @@ -140,19 +145,20 @@ public void defaultGsonConverter() throws Exception { @Test public void jacksonIsPreferredByDefaultWhenBothGsonAndJacksonAreAvailable() { this.context.register(GsonAutoConfiguration.class, JacksonAutoConfiguration.class, - HttpMessageConvertersAutoConfiguration.class); + JsonbAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class); this.context.refresh(); assertConverterBeanExists(MappingJackson2HttpMessageConverter.class, "mappingJackson2HttpMessageConverter"); assertConverterBeanRegisteredWithHttpMessageConverters( MappingJackson2HttpMessageConverter.class); assertThat(this.context.getBeansOfType(GsonHttpMessageConverter.class)).isEmpty(); + assertThat(this.context.getBeansOfType(JsonbHttpMessageConverter.class)).isEmpty(); } @Test public void gsonCanBePreferredWhenBothGsonAndJacksonAreAvailable() { this.context.register(GsonAutoConfiguration.class, JacksonAutoConfiguration.class, - HttpMessageConvertersAutoConfiguration.class); + JsonbAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class); TestPropertyValues.of("spring.http.converters.preferred-json-mapper:gson") .applyTo(this.context); this.context.refresh(); @@ -160,6 +166,8 @@ public void gsonCanBePreferredWhenBothGsonAndJacksonAreAvailable() { "gsonHttpMessageConverter"); assertConverterBeanRegisteredWithHttpMessageConverters( GsonHttpMessageConverter.class); + assertThat(this.context.getBeansOfType(JsonbHttpMessageConverter.class)) + .isEmpty(); assertThat(this.context.getBeansOfType(MappingJackson2HttpMessageConverter.class)) .isEmpty(); } @@ -176,6 +184,55 @@ public void customGsonConverter() throws Exception { GsonHttpMessageConverter.class); } + @Test + public void noJsonb() throws Exception { + this.context.register(HttpMessageConvertersAutoConfiguration.class); + this.context.refresh(); + assertThat(this.context.getBeansOfType(Jsonb.class).isEmpty()).isTrue(); + assertThat(this.context.getBeansOfType(JsonbHttpMessageConverter.class).isEmpty()) + .isTrue(); + } + + @Test + public void defaultJsonbConverter() throws Exception { + this.context.register(JsonbAutoConfiguration.class, + HttpMessageConvertersAutoConfiguration.class); + this.context.refresh(); + assertConverterBeanExists(JsonbHttpMessageConverter.class, + "jsonbHttpMessageConverter"); + + assertConverterBeanRegisteredWithHttpMessageConverters( + JsonbHttpMessageConverter.class); + } + + @Test + public void jsonbCanBePreferredWhenBothGsonAndJacksonAreAvailable() { + this.context.register(GsonAutoConfiguration.class, JacksonAutoConfiguration.class, + JsonbAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class); + TestPropertyValues.of("spring.http.converters.preferred-json-mapper:jsonb") + .applyTo(this.context); + this.context.refresh(); + assertConverterBeanExists(JsonbHttpMessageConverter.class, + "jsonbHttpMessageConverter"); + assertConverterBeanRegisteredWithHttpMessageConverters( + JsonbHttpMessageConverter.class); + assertThat(this.context.getBeansOfType(GsonHttpMessageConverter.class)) + .isEmpty(); + assertThat(this.context.getBeansOfType(MappingJackson2HttpMessageConverter.class)) + .isEmpty(); + } + + @Test + public void customJsonbConverter() throws Exception { + this.context.register(JsonbAutoConfiguration.class, JsonbConverterConfig.class, + HttpMessageConvertersAutoConfiguration.class); + this.context.refresh(); + assertConverterBeanExists(JsonbHttpMessageConverter.class, + "customJsonbMessageConverter"); + assertConverterBeanRegisteredWithHttpMessageConverters( + JsonbHttpMessageConverter.class); + } + @Test public void defaultStringConverter() throws Exception { this.context.register(HttpMessageConvertersAutoConfiguration.class); @@ -288,6 +345,18 @@ public GsonHttpMessageConverter customGsonMessageConverter(Gson gson) { } + @Configuration + protected static class JsonbConverterConfig { + + @Bean + public JsonbHttpMessageConverter customJsonbMessageConverter(Jsonb jsonb) { + JsonbHttpMessageConverter converter = new JsonbHttpMessageConverter(); + converter.setJsonb(jsonb); + return converter; + } + + } + @Configuration protected static class StringConverterConfig { diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jsonb/JsonbAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jsonb/JsonbAutoConfigurationTests.java new file mode 100644 index 000000000000..0cdb73d542dd --- /dev/null +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jsonb/JsonbAutoConfigurationTests.java @@ -0,0 +1,72 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.jsonb; + +import javax.json.bind.Jsonb; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link JsonbAutoConfiguration}. + * + * @author Eddú Meléndez + */ +public class JsonbAutoConfigurationTests { + + AnnotationConfigApplicationContext context; + + @Before + public void setUp() { + this.context = new AnnotationConfigApplicationContext(); + } + + @After + public void tearDown() { + if (this.context != null) { + this.context.close(); + } + } + + @Test + public void jsonbRegistration() { + this.context.register(JsonbAutoConfiguration.class); + this.context.refresh(); + Jsonb jsonb = this.context.getBean(Jsonb.class); + assertThat(jsonb.toJson(new DataObject())).isEqualTo("{\"data\":\"hello\"}"); + } + + public class DataObject { + + @SuppressWarnings("unused") + private String data = "hello"; + + public String getData() { + return this.data; + } + + public void setData(String data) { + this.data = data; + } + } + +} diff --git a/spring-boot-dependencies/pom.xml b/spring-boot-dependencies/pom.xml index 6ba3f1bd0f6c..9f1e105cfee2 100644 --- a/spring-boot-dependencies/pom.xml +++ b/spring-boot-dependencies/pom.xml @@ -119,6 +119,8 @@ 1.3.7 3.9.4 1.5.0 + 1.1 + 1.0 2.4.0 1.2 1.3.1 @@ -883,6 +885,16 @@ javax.jms-api ${javax-jms.version} + + org.glassfish + javax.json + ${javax.json-api.version} + + + javax.json.bind + javax.json.bind-api + ${javax-jsonb.version} + javax.mail javax.mail-api diff --git a/spring-boot-test-autoconfigure/pom.xml b/spring-boot-test-autoconfigure/pom.xml index 3743ae3f3027..6061096f0c2d 100644 --- a/spring-boot-test-autoconfigure/pom.xml +++ b/spring-boot-test-autoconfigure/pom.xml @@ -29,6 +29,11 @@ spring-boot-autoconfigure + + javax.json.bind + javax.json.bind-api + true + javax.servlet javax.servlet-api @@ -59,6 +64,11 @@ htmlunit true + + org.glassfish + javax.json + true + org.hibernate hibernate-core @@ -208,6 +218,12 @@ jooq test + + org.eclipse + yasson + ${javax-jsonb.version} + test + org.mongodb mongodb-driver-async diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/json/AutoConfigureJsonTesters.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/json/AutoConfigureJsonTesters.java index ea48f3c07be9..e68477b34d31 100644 --- a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/json/AutoConfigureJsonTesters.java +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/json/AutoConfigureJsonTesters.java @@ -28,6 +28,7 @@ import org.springframework.boot.test.json.BasicJsonTester; import org.springframework.boot.test.json.GsonTester; import org.springframework.boot.test.json.JacksonTester; +import org.springframework.boot.test.json.JsonbTester; /** * Annotation that can be applied to a test class to enable and configure @@ -45,8 +46,8 @@ public @interface AutoConfigureJsonTesters { /** - * If {@link BasicJsonTester}, {@link JacksonTester} and {@link GsonTester} beans - * should be registered. Defaults to {@code true} + * If {@link BasicJsonTester}, {@link JacksonTester}, {@link JsonbTester} and + * {@link GsonTester} beans should be registered. Defaults to {@code true} * @return if tester support is enabled */ boolean enabled() default true; diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/json/JsonTest.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/json/JsonTest.java index 676459c58589..84aa698d6bb7 100644 --- a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/json/JsonTest.java +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/json/JsonTest.java @@ -31,6 +31,7 @@ import org.springframework.boot.test.context.SpringBootTestContextBootstrapper; import org.springframework.boot.test.json.GsonTester; import org.springframework.boot.test.json.JacksonTester; +import org.springframework.boot.test.json.JsonbTester; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.core.annotation.AliasFor; import org.springframework.test.context.BootstrapWith; @@ -45,8 +46,9 @@ * {@code Module}) *

* By default, tests annotated with {@code JsonTest} will also initialize - * {@link JacksonTester} and {@link GsonTester} fields. More fine-grained control can be - * provided via the {@link AutoConfigureJsonTesters @AutoConfigureJsonTesters} annotation. + * {@link JacksonTester}, {@link JsonbTester} and {@link GsonTester} fields. More + * fine-grained control can be provided via the {@link AutoConfigureJsonTesters @AutoConfigureJsonTesters} + * annotation. * * @author Phillip Webb * @see AutoConfigureJson diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/json/JsonTestersAutoConfiguration.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/json/JsonTestersAutoConfiguration.java index 4110e3116700..ffb7b10c3181 100644 --- a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/json/JsonTestersAutoConfiguration.java +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/json/JsonTestersAutoConfiguration.java @@ -19,6 +19,8 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Field; +import javax.json.bind.Jsonb; + import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; @@ -33,10 +35,12 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration; import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; +import org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration; import org.springframework.boot.test.json.AbstractJsonMarshalTester; import org.springframework.boot.test.json.BasicJsonTester; import org.springframework.boot.test.json.GsonTester; import org.springframework.boot.test.json.JacksonTester; +import org.springframework.boot.test.json.JsonbTester; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; @@ -48,13 +52,15 @@ * Auto-configuration for Json testers. * * @author Phillip Webb + * @author Eddú Meléndez * @see AutoConfigureJsonTesters * @since 1.4.0 */ @Configuration @ConditionalOnClass(name = "org.assertj.core.api.Assert") @ConditionalOnProperty("spring.test.jsontesters.enabled") -@AutoConfigureAfter({ JacksonAutoConfiguration.class, GsonAutoConfiguration.class }) +@AutoConfigureAfter({ JacksonAutoConfiguration.class, GsonAutoConfiguration.class, + JsonbAutoConfiguration.class }) public class JsonTestersAutoConfiguration { @Bean @@ -94,6 +100,18 @@ public FactoryBean> gsonTesterFactoryBean(Gson gson) { } + @ConditionalOnClass(Jsonb.class) + private static class JsonbJsonTesterConfiguration { + + @Bean + @Scope("prototype") + @ConditionalOnBean(Jsonb.class) + public FactoryBean> jsonbTesterFactoryBean(Jsonb jsonb) { + return new JsonTesterFactoryBean<>(JsonbTester.class, jsonb); + } + + } + /** * {@link FactoryBean} used to create JSON Tester instances. */ diff --git a/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories index 0d335481a554..71c98e3c4338 100644 --- a/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories @@ -67,13 +67,15 @@ org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration # AutoConfigureJson auto-configuration imports org.springframework.boot.test.autoconfigure.json.AutoConfigureJson=\ org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\ -org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration +org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\ +org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration # AutoConfigureJsonTesters auto-configuration imports org.springframework.boot.test.autoconfigure.json.AutoConfigureJsonTesters=\ org.springframework.boot.test.autoconfigure.json.JsonTestersAutoConfiguration,\ org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\ -org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration +org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\ +org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration # AutoConfigureWebClient auto-configuration imports org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient=\ @@ -111,6 +113,7 @@ org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\ org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration,\ org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration,\ org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\ +org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration,\ org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration,\ org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration @@ -123,6 +126,7 @@ org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\ org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\ org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration,\ org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\ +org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration.\ org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\ org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\ org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,\ @@ -145,4 +149,4 @@ org.springframework.test.context.TestExecutionListener=\ org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener,\ org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener,\ org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener,\ -org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener \ No newline at end of file +org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/json/JsonTestIntegrationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/json/JsonTestIntegrationTests.java index 64dbb108ba85..f4ce184ae781 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/json/JsonTestIntegrationTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/json/JsonTestIntegrationTests.java @@ -28,6 +28,7 @@ import org.springframework.boot.test.json.GsonTester; import org.springframework.boot.test.json.JacksonTester; import org.springframework.boot.test.json.JsonContent; +import org.springframework.boot.test.json.JsonbTester; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; @@ -38,6 +39,7 @@ * * @author Phillip Webb * @author Madhura Bhave + * @author Eddú Meléndez */ @RunWith(SpringRunner.class) @JsonTest @@ -59,6 +61,9 @@ public class JsonTestIntegrationTests { @Autowired private GsonTester gsonJson; + @Autowired + private JsonbTester jsonbJson; + @Test public void basicJson() throws Exception { assertThat(this.basicJson.from("{\"a\":\"b\"}")).hasJsonPathStringValue("@.a"); @@ -84,6 +89,13 @@ public void gson() throws Exception { assertThat(this.gsonJson.write(object)).isEqualToJson("example.json"); } + @Test + public void jsonb() throws Exception { + ExampleBasicObject object = new ExampleBasicObject(); + object.setValue("spring"); + assertThat(this.jsonbJson.write(object)).isEqualToJson("example.json"); + } + @Test public void customView() throws Exception { ExampleJsonObjectWithView object = new ExampleJsonObjectWithView(); diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/json/JsonTestWithAutoConfigureJsonTestersTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/json/JsonTestWithAutoConfigureJsonTestersTests.java index 1c34491b1327..62b44ac09f8e 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/json/JsonTestWithAutoConfigureJsonTestersTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/json/JsonTestWithAutoConfigureJsonTestersTests.java @@ -25,6 +25,7 @@ import org.springframework.boot.test.json.BasicJsonTester; import org.springframework.boot.test.json.GsonTester; import org.springframework.boot.test.json.JacksonTester; +import org.springframework.boot.test.json.JsonbTester; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; @@ -50,6 +51,9 @@ public class JsonTestWithAutoConfigureJsonTestersTests { @Autowired(required = false) private GsonTester gsonTester; + @Autowired(required = false) + private JsonbTester jsonbTester; + @Test public void basicJson() throws Exception { assertThat(this.basicJson).isNull(); @@ -65,4 +69,9 @@ public void gson() throws Exception { assertThat(this.gsonTester).isNull(); } + @Test + public void jsonb() throws Exception { + assertThat(this.jsonbTester).isNull(); + } + } diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/json/SpringBootTestWithAutoConfigureJsonTestersTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/json/SpringBootTestWithAutoConfigureJsonTestersTests.java index 8c997427bae9..6584e1af35ae 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/json/SpringBootTestWithAutoConfigureJsonTestersTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/json/SpringBootTestWithAutoConfigureJsonTestersTests.java @@ -26,6 +26,7 @@ import org.springframework.boot.test.json.BasicJsonTester; import org.springframework.boot.test.json.GsonTester; import org.springframework.boot.test.json.JacksonTester; +import org.springframework.boot.test.json.JsonbTester; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; @@ -51,6 +52,9 @@ public class SpringBootTestWithAutoConfigureJsonTestersTests { @Autowired private GsonTester gsonTester; + @Autowired + JsonbTester jsonbTester; + @Test public void contextLoads() { assertThat(this.basicJson).isNotNull(); diff --git a/spring-boot-test/pom.xml b/spring-boot-test/pom.xml index e4011bd9c461..d7c03f8713a6 100644 --- a/spring-boot-test/pom.xml +++ b/spring-boot-test/pom.xml @@ -45,6 +45,11 @@ reactor-netty true + + javax.json.bind + javax.json.bind-api + true + javax.servlet javax.servlet-api @@ -65,6 +70,11 @@ assertj-core true + + org.glassfish + javax.json + true + org.hamcrest hamcrest-core @@ -142,6 +152,12 @@ true test + + org.eclipse + yasson + ${javax-jsonb.version} + test + org.jetbrains.kotlin kotlin-runtime diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonbTester.java b/spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonbTester.java new file mode 100644 index 000000000000..0e91f363385d --- /dev/null +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonbTester.java @@ -0,0 +1,128 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.json; + +import java.io.IOException; +import java.io.Reader; + +import javax.json.bind.Jsonb; + +import org.springframework.beans.factory.ObjectFactory; +import org.springframework.core.ResolvableType; +import org.springframework.util.Assert; + +/** + * AssertJ based JSON tester backed by Jsonb. Usually instantiated via + * {@link #initFields(Object, Jsonb)}, for example:

+ * public class ExampleObjectJsonTests {
+ *
+ * 	private JsonbTester<ExampleObject> json;
+ *
+ * 	@Before
+ * 	public void setup() {
+ * 		Jsonb jsonb = JsonbBuilder.create();
+ * 		JsonbTester.initFields(this, jsonb);
+ * 	}
+ *
+ * 	@Test
+ * 	public void testWriteJson() throws IOException {
+ * 		ExampleObject object = // ...
+ * 		assertThat(json.write(object)).isEqualToJson("expected.json");
+ * 	}
+ *
+ * }
+ * 
+ * + * See {@link AbstractJsonMarshalTester} for more details. + * + * @param the type under test + * @author Eddú Meléndez + * @since 2.0.0 + */ +public class JsonbTester extends AbstractJsonMarshalTester { + + private final Jsonb jsonb; + + /** + * Create a new uninitialized {@link GsonTester} instance. + * @param jsonb the Jsonb instance + */ + protected JsonbTester(Jsonb jsonb) { + Assert.notNull(jsonb, "Jsonb must not be null"); + this.jsonb = jsonb; + } + + /** + * Create a new {@link JsonbTester} instance. + * @param resourceLoadClass the source class used to load resources + * @param type the type under test + * @param jsonb the Jsonb instance + * @see #initFields(Object, Jsonb) + */ + public JsonbTester(Class resourceLoadClass, ResolvableType type, Jsonb jsonb) { + super(resourceLoadClass, type); + Assert.notNull(jsonb, "Jsonb must not be null"); + this.jsonb = jsonb; + } + + @Override + protected String writeObject(T value, ResolvableType type) throws IOException { + return this.jsonb.toJson(value, type.getType()); + } + + @Override + protected T readObject(Reader reader, ResolvableType type) throws IOException { + return this.jsonb.fromJson(reader, type.getType()); + } + + /** + * Utility method to initialize {@link JsonbTester} fields. See {@link JsonbTester + * class-level documentation} for example usage. + * @param testInstance the test instance + * @param jsonb the Gson instance + */ + public static void initFields(Object testInstance, Jsonb jsonb) { + new JsonbFieldInitializer().initFields(testInstance, jsonb); + } + + /** + * Utility method to initialize {@link JsonbTester} fields. See {@link JsonbTester + * class-level documentation} for example usage. + * @param testInstance the test instance + * @param jsonb an object factory to create the Gson instance + */ + public static void initFields(Object testInstance, ObjectFactory jsonb) { + new JsonbTester.JsonbFieldInitializer().initFields(testInstance, jsonb); + } + + /** + * {@link FieldInitializer} for Jsonb. + */ + private static class JsonbFieldInitializer extends FieldInitializer { + + protected JsonbFieldInitializer() { + super(JsonbTester.class); + } + + @Override + protected AbstractJsonMarshalTester createTester( + Class resourceLoadClass, ResolvableType type, Jsonb marshaller) { + return new JsonbTester<>(resourceLoadClass, type, marshaller); + } + } + +} diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/json/JsonbTesterTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/json/JsonbTesterTests.java new file mode 100644 index 000000000000..a946bd1ba577 --- /dev/null +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/json/JsonbTesterTests.java @@ -0,0 +1,89 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.json; + +import java.util.List; + +import javax.json.bind.Jsonb; +import javax.json.bind.JsonbBuilder; + +import org.junit.Test; + +import org.springframework.core.ResolvableType; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link JsonbTester}. + * + * @author Eddú Meléndez + */ +public class JsonbTesterTests extends AbstractJsonMarshalTesterTests { + + @Test + public void initFieldsWhenTestIsNullShouldThrowException() { + this.thrown.expect(IllegalArgumentException.class); + this.thrown.expectMessage("TestInstance must not be null"); + JsonbTester.initFields(null, JsonbBuilder.create()); + } + + @Test + public void initFieldsWhenMarshallerIsNullShouldThrowException() { + this.thrown.expect(IllegalArgumentException.class); + this.thrown.expectMessage("Marshaller must not be null"); + JsonbTester.initFields(new InitFieldsTestClass(), (Jsonb) null); + } + + @Test + public void initFieldsShouldSetNullFields() { + InitFieldsTestClass test = new InitFieldsTestClass(); + assertThat(test.test).isNull(); + assertThat(test.base).isNull(); + JsonbTester.initFields(test, JsonbBuilder.create()); + assertThat(test.test).isNotNull(); + assertThat(test.base).isNotNull(); + assertThat(test.test.getType().resolve()).isEqualTo(List.class); + assertThat(test.test.getType().resolveGeneric()).isEqualTo(ExampleObject.class); + } + + @Override + protected AbstractJsonMarshalTester createTester(Class resourceLoadClass, + ResolvableType type) { + return new JsonbTester(resourceLoadClass, type, JsonbBuilder.create()); + } + + static abstract class InitFieldsBaseClass { + + public JsonbTester base; + + public JsonbTester baseSet = new JsonbTester<>( + InitFieldsBaseClass.class, ResolvableType.forClass(ExampleObject.class), + JsonbBuilder.create()); + + } + + static class InitFieldsTestClass extends InitFieldsBaseClass { + + public JsonbTester> test; + + public JsonbTester testSet = new JsonbTester<>( + InitFieldsBaseClass.class, ResolvableType.forClass(ExampleObject.class), + JsonbBuilder.create()); + + } + +} From 17dcc17aa80d60fa055b8bdecd313c60a38b4dc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Fri, 28 Jul 2017 11:29:12 -0500 Subject: [PATCH 2/7] Move yasson version to spring-boot-dependencies/pom.xml --- spring-boot-autoconfigure/pom.xml | 1 - spring-boot-dependencies/pom.xml | 5 +++++ spring-boot-test-autoconfigure/pom.xml | 1 - spring-boot-test/pom.xml | 1 - 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/spring-boot-autoconfigure/pom.xml b/spring-boot-autoconfigure/pom.xml index 6956f379ddc0..dcec6b7c15b6 100755 --- a/spring-boot-autoconfigure/pom.xml +++ b/spring-boot-autoconfigure/pom.xml @@ -741,7 +741,6 @@ org.eclipse yasson - ${javax-jsonb.version} test diff --git a/spring-boot-dependencies/pom.xml b/spring-boot-dependencies/pom.xml index 9f1e105cfee2..eb6b6439e2a2 100644 --- a/spring-boot-dependencies/pom.xml +++ b/spring-boot-dependencies/pom.xml @@ -2454,6 +2454,11 @@ snakeyaml ${snakeyaml.version} + + org.eclipse + yasson + ${javax-jsonb.version} + redis.clients jedis diff --git a/spring-boot-test-autoconfigure/pom.xml b/spring-boot-test-autoconfigure/pom.xml index 6061096f0c2d..39a6960c7cfa 100644 --- a/spring-boot-test-autoconfigure/pom.xml +++ b/spring-boot-test-autoconfigure/pom.xml @@ -221,7 +221,6 @@ org.eclipse yasson - ${javax-jsonb.version} test diff --git a/spring-boot-test/pom.xml b/spring-boot-test/pom.xml index d7c03f8713a6..12b7d12a6880 100644 --- a/spring-boot-test/pom.xml +++ b/spring-boot-test/pom.xml @@ -155,7 +155,6 @@ org.eclipse yasson - ${javax-jsonb.version} test From 8446f48bb7f9c9142df32a6475d34e506c2b64de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Fri, 28 Jul 2017 11:32:23 -0500 Subject: [PATCH 3/7] Remove defaultValue --- .../META-INF/additional-spring-configuration-metadata.json | 1 - 1 file changed, 1 deletion(-) diff --git a/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json index d89c46607d5a..30e0b96f2b31 100644 --- a/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -572,7 +572,6 @@ }, { "name": "spring.http.converters.preferred-json-mapper", - "defaultValue" : "jackson", "values": [ { "value": "gson" From bb36711a5cf3c34a58cf964601eadd6f21cc201b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Fri, 28 Jul 2017 11:36:28 -0500 Subject: [PATCH 4/7] Fix typo --- .../org/springframework/boot/test/json/JsonbTester.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonbTester.java b/spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonbTester.java index 0e91f363385d..899f90eeaad3 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonbTester.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonbTester.java @@ -58,7 +58,7 @@ public class JsonbTester extends AbstractJsonMarshalTester { private final Jsonb jsonb; /** - * Create a new uninitialized {@link GsonTester} instance. + * Create a new uninitialized {@link JsonbTester} instance. * @param jsonb the Jsonb instance */ protected JsonbTester(Jsonb jsonb) { @@ -93,7 +93,7 @@ protected T readObject(Reader reader, ResolvableType type) throws IOException { * Utility method to initialize {@link JsonbTester} fields. See {@link JsonbTester * class-level documentation} for example usage. * @param testInstance the test instance - * @param jsonb the Gson instance + * @param jsonb the Jsonb instance */ public static void initFields(Object testInstance, Jsonb jsonb) { new JsonbFieldInitializer().initFields(testInstance, jsonb); @@ -103,7 +103,7 @@ public static void initFields(Object testInstance, Jsonb jsonb) { * Utility method to initialize {@link JsonbTester} fields. See {@link JsonbTester * class-level documentation} for example usage. * @param testInstance the test instance - * @param jsonb an object factory to create the Gson instance + * @param jsonb an object factory to create the Jsonb instance */ public static void initFields(Object testInstance, ObjectFactory jsonb) { new JsonbTester.JsonbFieldInitializer().initFields(testInstance, jsonb); From de63cc0a8671840fd173bb4081571f90c103db60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Fri, 28 Jul 2017 12:15:29 -0500 Subject: [PATCH 5/7] Polish --- .../json/SpringBootTestWithAutoConfigureJsonTestersTests.java | 3 ++- .../org/springframework/boot/test/json/JsonbTesterTests.java | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/json/SpringBootTestWithAutoConfigureJsonTestersTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/json/SpringBootTestWithAutoConfigureJsonTestersTests.java index 6584e1af35ae..330dc41881ed 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/json/SpringBootTestWithAutoConfigureJsonTestersTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/json/SpringBootTestWithAutoConfigureJsonTestersTests.java @@ -53,12 +53,13 @@ public class SpringBootTestWithAutoConfigureJsonTestersTests { private GsonTester gsonTester; @Autowired - JsonbTester jsonbTester; + private JsonbTester jsonbTester; @Test public void contextLoads() { assertThat(this.basicJson).isNotNull(); assertThat(this.jacksonTester).isNotNull(); + assertThat(this.jsonbTester).isNotNull(); assertThat(this.gsonTester).isNotNull(); } diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/json/JsonbTesterTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/json/JsonbTesterTests.java index a946bd1ba577..91bd5987edfb 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/json/JsonbTesterTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/json/JsonbTesterTests.java @@ -63,7 +63,7 @@ public void initFieldsShouldSetNullFields() { @Override protected AbstractJsonMarshalTester createTester(Class resourceLoadClass, ResolvableType type) { - return new JsonbTester(resourceLoadClass, type, JsonbBuilder.create()); + return new JsonbTester<>(resourceLoadClass, type, JsonbBuilder.create()); } static abstract class InitFieldsBaseClass { From 091bad78738671bb71aa32da322042c83d0773d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Sun, 6 Aug 2017 13:16:17 -0500 Subject: [PATCH 6/7] Move from yasson to johnzon --- spring-boot-autoconfigure/pom.xml | 9 ++------- spring-boot-dependencies/pom.xml | 17 ++++++----------- spring-boot-test-autoconfigure/pom.xml | 9 ++------- spring-boot-test/pom.xml | 9 ++------- 4 files changed, 12 insertions(+), 32 deletions(-) diff --git a/spring-boot-autoconfigure/pom.xml b/spring-boot-autoconfigure/pom.xml index dcec6b7c15b6..48e5106b3f3c 100755 --- a/spring-boot-autoconfigure/pom.xml +++ b/spring-boot-autoconfigure/pom.xml @@ -279,11 +279,6 @@ javax-websocket-server-impl true - - org.glassfish - javax.json - true - io.undertow undertow-servlet @@ -739,8 +734,8 @@ test - org.eclipse - yasson + org.apache.johnzon + johnzon-jsonb test diff --git a/spring-boot-dependencies/pom.xml b/spring-boot-dependencies/pom.xml index eb6b6439e2a2..a8f431197b4e 100644 --- a/spring-boot-dependencies/pom.xml +++ b/spring-boot-dependencies/pom.xml @@ -117,9 +117,9 @@ 4.4.0 2.9.9 1.3.7 + 1.1.2 3.9.4 1.5.0 - 1.1 1.0 2.4.0 1.2 @@ -885,11 +885,6 @@ javax.jms-api ${javax-jms.version} - - org.glassfish - javax.json - ${javax.json-api.version} - javax.json.bind javax.json.bind-api @@ -1252,6 +1247,11 @@ httpmime ${httpclient.version} + + org.apache.johnzon + johnzon-jsonb + ${johnzon-jsonb.version} + org.apache.logging.log4j log4j-bom @@ -2454,11 +2454,6 @@ snakeyaml ${snakeyaml.version} - - org.eclipse - yasson - ${javax-jsonb.version} - redis.clients jedis diff --git a/spring-boot-test-autoconfigure/pom.xml b/spring-boot-test-autoconfigure/pom.xml index 39a6960c7cfa..4ab971dd9536 100644 --- a/spring-boot-test-autoconfigure/pom.xml +++ b/spring-boot-test-autoconfigure/pom.xml @@ -64,11 +64,6 @@ htmlunit true - - org.glassfish - javax.json - true - org.hibernate hibernate-core @@ -219,8 +214,8 @@ test - org.eclipse - yasson + org.apache.johnzon + johnzon-jsonb test diff --git a/spring-boot-test/pom.xml b/spring-boot-test/pom.xml index 12b7d12a6880..a5bd0a416eda 100644 --- a/spring-boot-test/pom.xml +++ b/spring-boot-test/pom.xml @@ -70,11 +70,6 @@ assertj-core true - - org.glassfish - javax.json - true - org.hamcrest hamcrest-core @@ -153,8 +148,8 @@ test - org.eclipse - yasson + org.apache.johnzon + johnzon-jsonb test From c66456f6673cd10019fe182b9bc20220c18a8f3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Sun, 6 Aug 2017 13:50:02 -0500 Subject: [PATCH 7/7] Add javax.json:javax.json-api --- spring-boot-autoconfigure/pom.xml | 5 +++++ spring-boot-dependencies/pom.xml | 6 ++++++ spring-boot-test-autoconfigure/pom.xml | 5 +++++ spring-boot-test/pom.xml | 5 +++++ 4 files changed, 21 insertions(+) diff --git a/spring-boot-autoconfigure/pom.xml b/spring-boot-autoconfigure/pom.xml index 48e5106b3f3c..5f727a1d94a5 100755 --- a/spring-boot-autoconfigure/pom.xml +++ b/spring-boot-autoconfigure/pom.xml @@ -728,6 +728,11 @@ json-path test + + javax.json + javax.json-api + test + mysql mysql-connector-java diff --git a/spring-boot-dependencies/pom.xml b/spring-boot-dependencies/pom.xml index a8f431197b4e..3e376eb9ded2 100644 --- a/spring-boot-dependencies/pom.xml +++ b/spring-boot-dependencies/pom.xml @@ -120,6 +120,7 @@ 1.1.2 3.9.4 1.5.0 + 1.1 1.0 2.4.0 1.2 @@ -885,6 +886,11 @@ javax.jms-api ${javax-jms.version} + + javax.json + javax.json-api + ${javax-json.version} + javax.json.bind javax.json.bind-api diff --git a/spring-boot-test-autoconfigure/pom.xml b/spring-boot-test-autoconfigure/pom.xml index 4ab971dd9536..2a0ff88d9656 100644 --- a/spring-boot-test-autoconfigure/pom.xml +++ b/spring-boot-test-autoconfigure/pom.xml @@ -188,6 +188,11 @@ reactor-core test + + javax.json + javax.json-api + test + org.apache.commons commons-pool2 diff --git a/spring-boot-test/pom.xml b/spring-boot-test/pom.xml index a5bd0a416eda..0fcb3c2a147f 100644 --- a/spring-boot-test/pom.xml +++ b/spring-boot-test/pom.xml @@ -121,6 +121,11 @@ true + + javax.json + javax.json-api + test + org.springframework.boot spring-boot-test-support