-
Notifications
You must be signed in to change notification settings - Fork 41.3k
Add Jsonb support #9648
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Jsonb support #9648
Changes from all commits
bf4e710
17dcc17
8446f48
bb36711
de63cc0
091bad7
c66456f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That particular one looks like a smell to me. What were you trying to express? |
||
static class JsonbMissing { | ||
|
||
} | ||
|
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. We should probably have a specific condition implementation for this |
||
|
||
JacksonAndGsonMissing() { | ||
super(ConfigurationPhase.REGISTER_BEAN); | ||
} | ||
|
||
@ConditionalOnBean(MappingJackson2HttpMessageConverter.class) | ||
static class JacksonMissing { | ||
|
||
} | ||
|
||
@ConditionalOnProperty(name = HttpMessageConvertersAutoConfiguration.PREFERRED_MAPPER_PROPERTY, havingValue = "gson") | ||
static class GsonMissing { | ||
|
||
} | ||
|
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -578,6 +578,9 @@ | |
}, | ||
{ | ||
"value": "jackson" | ||
}, | ||
{ | ||
"value": "jsonb" | ||
} | ||
], | ||
"providers": [ | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the rationale behind this?
Gson
is to be used if both Jackson and JSonB are missing? With three choices, the combinations are becoming increasingly complex so I wonder if we shouldn't revisit this differently.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
regarding to the conditional thing. Should I rewrite conditions with an
SpringBootCondition
implementation? Just to make sure, I would like to know that the current conditions are ok.