Deprecate JsonDeserializer.DEFAULT_VALUE_TYPE

* rename constant `DEFAULT_VALUE_TYPE` from `spring.json.default.value.type` to `spring.json.value.default.type`

* mark `DEFAULT_KEY_TYPE` and `DEFAULT_VALUE_TYPE` deprecated, introduce a new pair of `KEY_DEFAULT_TYPE` and `VALUE_DEFAULT_TYPE`

* fix javadoc problem, modify `kafka.adoc`

*  use `spring.json.default.value.type` literal instead of constant `DEFAULT_VALUE_TYPE`
This commit is contained in:
qct
2018-03-16 02:46:06 +08:00
committed by Artem Bilan
parent 1d080e711b
commit 17e255d759
3 changed files with 42 additions and 16 deletions

View File

@@ -54,14 +54,28 @@ public class JsonDeserializer<T> implements ExtendedDeserializer<T> {
/**
* Kafka config property for the default key type if no header.
* @deprecated in favor of {@link #KEY_DEFAULT_TYPE}
*/
@Deprecated
public static final String DEFAULT_KEY_TYPE = "spring.json.key.default.type";
/**
* Kafka config property for the default value type if no header.
* @deprecated in favor of {@link #VALUE_DEFAULT_TYPE}
*/
@Deprecated
public static final String DEFAULT_VALUE_TYPE = "spring.json.default.value.type";
/**
* Kafka config property for the default key type if no header.
*/
public static final String KEY_DEFAULT_TYPE = "spring.json.key.default.type";
/**
* Kafka config property for the default value type if no header.
*/
public static final String VALUE_DEFAULT_TYPE = "spring.json.value.default.type";
/**
* Kafka config property for trusted deserialization packages.
*/
@@ -136,26 +150,39 @@ public class JsonDeserializer<T> implements ExtendedDeserializer<T> {
public void configure(Map<String, ?> configs, boolean isKey) {
setUseTypeMapperForKey(isKey);
try {
if (isKey && configs.containsKey(DEFAULT_KEY_TYPE)) {
if (configs.get(DEFAULT_KEY_TYPE) instanceof Class) {
this.targetType = (Class<T>) configs.get(DEFAULT_KEY_TYPE);
if (isKey && configs.containsKey(KEY_DEFAULT_TYPE)) {
if (configs.get(KEY_DEFAULT_TYPE) instanceof Class) {
this.targetType = (Class<T>) configs.get(KEY_DEFAULT_TYPE);
}
else if (configs.get(DEFAULT_KEY_TYPE) instanceof String) {
this.targetType = (Class<T>) ClassUtils.forName((String) configs.get(DEFAULT_KEY_TYPE), null);
else if (configs.get(KEY_DEFAULT_TYPE) instanceof String) {
this.targetType = (Class<T>) ClassUtils.forName((String) configs.get(KEY_DEFAULT_TYPE), null);
}
else {
throw new IllegalStateException(DEFAULT_KEY_TYPE + " must be Class or String");
throw new IllegalStateException(KEY_DEFAULT_TYPE + " must be Class or String");
}
}
else if (!isKey && configs.containsKey(DEFAULT_VALUE_TYPE)) {
if (configs.get(DEFAULT_VALUE_TYPE) instanceof Class) {
this.targetType = (Class<T>) configs.get(DEFAULT_VALUE_TYPE);
// TODO don't forget to remove these code after DEFAULT_VALUE_TYPE being removed.
else if (!isKey && configs.containsKey("spring.json.default.value.type")) {
if (configs.get("spring.json.default.value.type") instanceof Class) {
this.targetType = (Class<T>) configs.get("spring.json.default.value.type");
}
else if (configs.get(DEFAULT_VALUE_TYPE) instanceof String) {
this.targetType = (Class<T>) ClassUtils.forName((String) configs.get(DEFAULT_VALUE_TYPE), null);
else if (configs.get("spring.json.default.value.type") instanceof String) {
this.targetType = (Class<T>) ClassUtils
.forName((String) configs.get("spring.json.default.value.type"), null);
}
else {
throw new IllegalStateException(DEFAULT_VALUE_TYPE + " must be Class or String");
throw new IllegalStateException("spring.json.default.value.type must be Class or String");
}
}
else if (!isKey && configs.containsKey(VALUE_DEFAULT_TYPE)) {
if (configs.get(VALUE_DEFAULT_TYPE) instanceof Class) {
this.targetType = (Class<T>) configs.get(VALUE_DEFAULT_TYPE);
}
else if (configs.get(VALUE_DEFAULT_TYPE) instanceof String) {
this.targetType = (Class<T>) ClassUtils.forName((String) configs.get(VALUE_DEFAULT_TYPE), null);
}
else {
throw new IllegalStateException(VALUE_DEFAULT_TYPE + " must be Class or String");
}
}
addTargetPackageToTrusted();
@@ -226,5 +253,4 @@ public class JsonDeserializer<T> implements ExtendedDeserializer<T> {
public void close() {
// No-op
}
}

View File

@@ -1458,7 +1458,7 @@ public class KafkaMessageListenerContainerTests {
this.logger.info("Start JSON1");
Map<String, Object> props = KafkaTestUtils.consumerProps("testJson", "false", embeddedKafka);
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
props.put(JsonDeserializer.DEFAULT_VALUE_TYPE, Foo.class);
props.put(JsonDeserializer.VALUE_DEFAULT_TYPE, Foo.class);
DefaultKafkaConsumerFactory<Integer, Foo> cf = new DefaultKafkaConsumerFactory<>(props);
ContainerProperties containerProps = new ContainerProperties(topic1);

View File

@@ -1421,8 +1421,8 @@ Starting with _version 2.1_, type information can be conveyed in record `Headers
In addition, the serializer/deserializer can be configured using Kafka properties.
- `JsonSerializer.ADD_TYPE_INFO_HEADERS` (default `true`); set to `false` to disable this feature.
- `JsonDeserializer.DEFAULT_KEY_TYPE`; fallback type for deserialization of keys if no header information is present.
- `JsonDeserializer.DEFAULT_VALUE_TYPE`; fallback type for deserialization of values if no header information is present.
- `JsonDeserializer.KEY_DEFAULT_TYPE`; fallback type for deserialization of keys if no header information is present.
- `JsonDeserializer.VALUE_DEFAULT_TYPE`; fallback type for deserialization of values if no header information is present.
- `JsonDeserializer.TRUSTED_PACKAGES` (default `java.util`, `java.lang`); comma-delimited list of package patterns allowed for deserialization; `*` means deserialize all.
Although the `Serializer`/`Deserializer` API is quite simple and flexible from the low-level Kafka `Consumer` and