Skip to content

Commit e9d25bc

Browse files
committed
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).
1 parent b19b515 commit e9d25bc

File tree

22 files changed

+671
-16
lines changed

22 files changed

+671
-16
lines changed

spring-boot-autoconfigure/pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@
120120
<artifactId>cache-api</artifactId>
121121
<optional>true</optional>
122122
</dependency>
123+
<dependency>
124+
<groupId>javax.json.bind</groupId>
125+
<artifactId>javax.json.bind-api</artifactId>
126+
<optional>true</optional>
127+
</dependency>
123128
<dependency>
124129
<groupId>io.searchbox</groupId>
125130
<artifactId>jest</artifactId>
@@ -274,6 +279,11 @@
274279
<artifactId>javax-websocket-server-impl</artifactId>
275280
<optional>true</optional>
276281
</dependency>
282+
<dependency>
283+
<groupId>org.glassfish</groupId>
284+
<artifactId>javax.json</artifactId>
285+
<optional>true</optional>
286+
</dependency>
277287
<dependency>
278288
<groupId>io.undertow</groupId>
279289
<artifactId>undertow-servlet</artifactId>
@@ -723,6 +733,12 @@
723733
<artifactId>mysql-connector-java</artifactId>
724734
<scope>test</scope>
725735
</dependency>
736+
<dependency>
737+
<groupId>org.eclipse</groupId>
738+
<artifactId>yasson</artifactId>
739+
<version>${javax-jsonb.version}</version>
740+
<scope>test</scope>
741+
</dependency>
726742
<dependency>
727743
<groupId>org.hsqldb</groupId>
728744
<artifactId>hsqldb</artifactId>

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/http/GsonHttpMessageConvertersConfiguration.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2424
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2525
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
26+
import org.springframework.boot.autoconfigure.condition.NoneNestedConditions;
2627
import org.springframework.context.annotation.Bean;
2728
import org.springframework.context.annotation.Conditional;
2829
import org.springframework.context.annotation.Configuration;
@@ -33,6 +34,7 @@
3334
* Configuration for HTTP Message converters that use Gson.
3435
*
3536
* @author Andy Wilkinson
37+
* @author Eddú Meléndez
3638
* @since 1.2.2
3739
*/
3840
@Configuration
@@ -41,7 +43,7 @@ class GsonHttpMessageConvertersConfiguration {
4143

4244
@Configuration
4345
@ConditionalOnBean(Gson.class)
44-
@Conditional(PreferGsonOrMissingJacksonCondition.class)
46+
@Conditional(PreferGsonOrMissingJacksonAndJsonbCondition.class)
4547
protected static class GsonHttpMessageConverterConfiguration {
4648

4749
@Bean
@@ -54,9 +56,9 @@ public GsonHttpMessageConverter gsonHttpMessageConverter(Gson gson) {
5456

5557
}
5658

57-
private static class PreferGsonOrMissingJacksonCondition extends AnyNestedCondition {
59+
private static class PreferGsonOrMissingJacksonAndJsonbCondition extends AnyNestedCondition {
5860

59-
PreferGsonOrMissingJacksonCondition() {
61+
PreferGsonOrMissingJacksonAndJsonbCondition() {
6062
super(ConfigurationPhase.REGISTER_BEAN);
6163
}
6264

@@ -65,11 +67,29 @@ static class GsonPreferred {
6567

6668
}
6769

68-
@ConditionalOnMissingBean(MappingJackson2HttpMessageConverter.class)
70+
@Conditional(JacksonAndJsonbMissing.class)
71+
static class JacksonJsonbMissing {
72+
73+
}
74+
75+
}
76+
77+
private static class JacksonAndJsonbMissing extends NoneNestedConditions {
78+
79+
JacksonAndJsonbMissing() {
80+
super(ConfigurationPhase.REGISTER_BEAN);
81+
}
82+
83+
@ConditionalOnBean(MappingJackson2HttpMessageConverter.class)
6984
static class JacksonMissing {
7085

7186
}
7287

88+
@ConditionalOnProperty(name = HttpMessageConvertersAutoConfiguration.PREFERRED_MAPPER_PROPERTY, havingValue = "jsonb")
89+
static class JsonbMissing {
90+
91+
}
92+
7393
}
7494

7595
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/http/HttpMessageConvertersAutoConfiguration.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2727
import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;
2828
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
29+
import org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration;
2930
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3031
import org.springframework.context.annotation.Bean;
3132
import org.springframework.context.annotation.Configuration;
@@ -44,12 +45,15 @@
4445
* @author Andy Wilkinson
4546
* @author Sebastien Deleuze
4647
* @author Stephane Nicoll
48+
* @author Eddú Meléndez
4749
*/
4850
@Configuration
4951
@ConditionalOnClass(HttpMessageConverter.class)
50-
@AutoConfigureAfter({ GsonAutoConfiguration.class, JacksonAutoConfiguration.class })
52+
@AutoConfigureAfter({ GsonAutoConfiguration.class, JacksonAutoConfiguration.class,
53+
JsonbAutoConfiguration.class })
5154
@Import({ JacksonHttpMessageConvertersConfiguration.class,
52-
GsonHttpMessageConvertersConfiguration.class })
55+
GsonHttpMessageConvertersConfiguration.class,
56+
JsonbHttpMessageConvertersConfiguration.class })
5357
public class HttpMessageConvertersAutoConfiguration {
5458

5559
static final String PREFERRED_MAPPER_PROPERTY = "spring.http.converters.preferred-json-mapper";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.http;
18+
19+
import javax.json.bind.Jsonb;
20+
21+
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
23+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
26+
import org.springframework.boot.autoconfigure.condition.NoneNestedConditions;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Conditional;
29+
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.http.converter.json.JsonbHttpMessageConverter;
31+
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
32+
33+
/**
34+
* Configuration for HTTP Message converters that use JSON-B.
35+
*
36+
* @author Eddú Meléndez
37+
* @author 2.0.0
38+
*/
39+
@Configuration
40+
@ConditionalOnClass(Jsonb.class)
41+
class JsonbHttpMessageConvertersConfiguration {
42+
43+
@Configuration
44+
@ConditionalOnBean(Jsonb.class)
45+
@Conditional(PreferJsonbOrMissingJacksonAndGsonCondition.class)
46+
protected static class JsonbHttpMessageConverterConfiguration {
47+
48+
@Bean
49+
@ConditionalOnMissingBean
50+
public JsonbHttpMessageConverter jsonbHttpMessageConverter(Jsonb jsonb) {
51+
JsonbHttpMessageConverter converter = new JsonbHttpMessageConverter();
52+
converter.setJsonb(jsonb);
53+
return converter;
54+
}
55+
56+
}
57+
58+
private static class PreferJsonbOrMissingJacksonAndGsonCondition extends AnyNestedCondition {
59+
60+
PreferJsonbOrMissingJacksonAndGsonCondition() {
61+
super(ConfigurationPhase.REGISTER_BEAN);
62+
}
63+
64+
@ConditionalOnProperty(name = HttpMessageConvertersAutoConfiguration.PREFERRED_MAPPER_PROPERTY, havingValue = "jsonb", matchIfMissing = false)
65+
static class JsonbPreferred {
66+
67+
}
68+
69+
@Conditional(JacksonAndGsonMissing.class)
70+
static class JacksonGsonMissing {
71+
72+
}
73+
74+
}
75+
76+
private static class JacksonAndGsonMissing extends NoneNestedConditions {
77+
78+
JacksonAndGsonMissing() {
79+
super(ConfigurationPhase.REGISTER_BEAN);
80+
}
81+
82+
@ConditionalOnBean(MappingJackson2HttpMessageConverter.class)
83+
static class JacksonMissing {
84+
85+
}
86+
87+
@ConditionalOnProperty(name = HttpMessageConvertersAutoConfiguration.PREFERRED_MAPPER_PROPERTY, havingValue = "gson")
88+
static class GsonMissing {
89+
90+
}
91+
92+
}
93+
94+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.jsonb;
18+
19+
import javax.json.bind.Jsonb;
20+
import javax.json.bind.JsonbBuilder;
21+
22+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
23+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.context.annotation.Configuration;
27+
28+
/**
29+
* {@link EnableAutoConfiguration Auto-configuration} for JSON-B.
30+
*
31+
* @author Eddú Meléndez
32+
* @since 2.0.0
33+
*/
34+
@Configuration
35+
@ConditionalOnClass(Jsonb.class)
36+
public class JsonbAutoConfiguration {
37+
38+
@Bean
39+
@ConditionalOnMissingBean
40+
public Jsonb jsonb() {
41+
return JsonbBuilder.create();
42+
}
43+
44+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Auto-configuration for JSON-B.
19+
*/
20+
package org.springframework.boot.autoconfigure.jsonb;

spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,12 +572,16 @@
572572
},
573573
{
574574
"name": "spring.http.converters.preferred-json-mapper",
575+
"defaultValue" : "jackson",
575576
"values": [
576577
{
577578
"value": "gson"
578579
},
579580
{
580581
"value": "jackson"
582+
},
583+
{
584+
"value": "jsonb"
581585
}
582586
],
583587
"providers": [

spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ org.springframework.boot.autoconfigure.jms.artemis.ArtemisAutoConfiguration,\
7979
org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\
8080
org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration,\
8181
org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration,\
82+
org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration,\
8283
org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration,\
8384
org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration,\
8485
org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration,\

0 commit comments

Comments
 (0)