Skip to content

Commit 2e01d90

Browse files
committed
Merge pull request #9648 from eddumelendez:jsonb
* pr/9648: Polish "Add Jsonb support" Add Jsonb support
2 parents 676dec8 + 8d63b74 commit 2e01d90

File tree

24 files changed

+783
-169
lines changed

24 files changed

+783
-169
lines changed

spring-boot-autoconfigure/pom.xml

Lines changed: 15 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>
@@ -724,11 +729,21 @@
724729
<artifactId>json-path</artifactId>
725730
<scope>test</scope>
726731
</dependency>
732+
<dependency>
733+
<groupId>javax.json</groupId>
734+
<artifactId>javax.json-api</artifactId>
735+
<scope>test</scope>
736+
</dependency>
727737
<dependency>
728738
<groupId>mysql</groupId>
729739
<artifactId>mysql-connector-java</artifactId>
730740
<scope>test</scope>
731741
</dependency>
742+
<dependency>
743+
<groupId>org.apache.johnzon</groupId>
744+
<artifactId>johnzon-jsonb</artifactId>
745+
<scope>test</scope>
746+
</dependency>
732747
<dependency>
733748
<groupId>org.hsqldb</groupId>
734749
<artifactId>hsqldb</artifactId>

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

Lines changed: 26 additions & 5 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(PreferGsonOrJacksonAndJsonbUnavailableCondition.class)
4547
protected static class GsonHttpMessageConverterConfiguration {
4648

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

5557
}
5658

57-
private static class PreferGsonOrMissingJacksonCondition extends AnyNestedCondition {
59+
private static class PreferGsonOrJacksonAndJsonbUnavailableCondition
60+
extends AnyNestedCondition {
5861

59-
PreferGsonOrMissingJacksonCondition() {
62+
PreferGsonOrJacksonAndJsonbUnavailableCondition() {
6063
super(ConfigurationPhase.REGISTER_BEAN);
6164
}
6265

63-
@ConditionalOnProperty(name = HttpMessageConvertersAutoConfiguration.PREFERRED_MAPPER_PROPERTY, havingValue = "gson", matchIfMissing = false)
66+
@ConditionalOnProperty(name = HttpMessageConvertersAutoConfiguration.PREFERRED_MAPPER_PROPERTY, havingValue = "gson")
6467
static class GsonPreferred {
6568

6669
}
6770

68-
@ConditionalOnMissingBean(MappingJackson2HttpMessageConverter.class)
71+
@Conditional(JacksonAndJsonbUnavailable.class)
72+
static class JacksonJsonbUnavailable {
73+
74+
}
75+
76+
}
77+
78+
private static class JacksonAndJsonbUnavailable extends NoneNestedConditions {
79+
80+
JacksonAndJsonbUnavailable() {
81+
super(ConfigurationPhase.REGISTER_BEAN);
82+
}
83+
84+
@ConditionalOnBean(MappingJackson2HttpMessageConverter.class)
6985
static class JacksonMissing {
7086

7187
}
7288

89+
@ConditionalOnProperty(name = HttpMessageConvertersAutoConfiguration.PREFERRED_MAPPER_PROPERTY, havingValue = "jsonb")
90+
static class JsonbPreferred {
91+
92+
}
93+
7394
}
7495

7596
}

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,78 @@
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.context.annotation.Bean;
27+
import org.springframework.context.annotation.Conditional;
28+
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.http.converter.json.GsonHttpMessageConverter;
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
59+
extends AnyNestedCondition {
60+
61+
PreferJsonbOrMissingJacksonAndGsonCondition() {
62+
super(ConfigurationPhase.REGISTER_BEAN);
63+
}
64+
65+
@ConditionalOnProperty(name = HttpMessageConvertersAutoConfiguration.PREFERRED_MAPPER_PROPERTY, havingValue = "jsonb")
66+
static class JsonbPreferred {
67+
68+
}
69+
70+
@ConditionalOnMissingBean({ MappingJackson2HttpMessageConverter.class, GsonHttpMessageConverter.class })
71+
static class JacksonAndGsonMissing {
72+
73+
}
74+
75+
}
76+
77+
78+
}
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@
200200
{
201201
"name": "spring.http.converters.preferred-json-mapper",
202202
"type": "java.lang.String",
203-
"description": "Preferred JSON mapper to use for HTTP message conversion. Set to \"gson\" to force the use of Gson when both it and Jackson are on the classpath."
203+
"description": "Preferred JSON mapper to use for HTTP message conversion, auto-detected according to the environment by default."
204204
},
205205
{
206206
"name": "spring.jersey.type",
@@ -1362,6 +1362,9 @@
13621362
},
13631363
{
13641364
"value": "jackson"
1365+
},
1366+
{
1367+
"value": "jsonb"
13651368
}
13661369
],
13671370
"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
@@ -80,6 +80,7 @@ org.springframework.boot.autoconfigure.jms.artemis.ArtemisAutoConfiguration,\
8080
org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\
8181
org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration,\
8282
org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration,\
83+
org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration,\
8384
org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration,\
8485
org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration,\
8586
org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration,\

0 commit comments

Comments
 (0)