Skip to content

Commit c412eab

Browse files
committed
Jackson2ObjectMapperBuilder/FactoryBean accepts deserializers by handled type
Issue: SPR-14337
1 parent abcfffd commit c412eab

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -284,8 +284,7 @@ public Jackson2ObjectMapperBuilder mixIns(Map<Class<?>, Class<?>> mixIns) {
284284

285285
/**
286286
* Configure custom serializers. Each serializer is registered for the type
287-
* returned by {@link JsonSerializer#handledType()}, which must not be
288-
* {@code null}.
287+
* returned by {@link JsonSerializer#handledType()}, which must not be {@code null}.
289288
* @see #serializersByType(Map)
290289
*/
291290
public Jackson2ObjectMapperBuilder serializers(JsonSerializer<?>... serializers) {
@@ -324,6 +323,25 @@ public Jackson2ObjectMapperBuilder serializersByType(Map<Class<?>, JsonSerialize
324323
return this;
325324
}
326325

326+
/**
327+
* Configure custom deserializers. Each deserializer is registered for the type
328+
* returned by {@link JsonDeserializer#handledType()}, which must not be {@code null}.
329+
* @since 4.3
330+
* @see #deserializersByType(Map)
331+
*/
332+
public Jackson2ObjectMapperBuilder deserializers(JsonDeserializer<?>... deserializers) {
333+
if (deserializers != null) {
334+
for (JsonDeserializer<?> deserializer : deserializers) {
335+
Class<?> handledType = deserializer.handledType();
336+
if (handledType == null || handledType == Object.class) {
337+
throw new IllegalArgumentException("Unknown handled type in " + deserializer.getClass().getName());
338+
}
339+
this.deserializers.put(deserializer.handledType(), deserializer);
340+
}
341+
}
342+
return this;
343+
}
344+
327345
/**
328346
* Configure a custom deserializer for the given type.
329347
* @since 4.1.2

spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBean.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -256,8 +256,7 @@ public void setMixIns(Map<Class<?>, Class<?>> mixIns) {
256256

257257
/**
258258
* Configure custom serializers. Each serializer is registered for the type
259-
* returned by {@link JsonSerializer#handledType()}, which must not be
260-
* {@code null}.
259+
* returned by {@link JsonSerializer#handledType()}, which must not be {@code null}.
261260
* @see #setSerializersByType(Map)
262261
*/
263262
public void setSerializers(JsonSerializer<?>... serializers) {
@@ -272,6 +271,16 @@ public void setSerializersByType(Map<Class<?>, JsonSerializer<?>> serializers) {
272271
this.builder.serializersByType(serializers);
273272
}
274273

274+
/**
275+
* Configure custom deserializers. Each deserializer is registered for the type
276+
* returned by {@link JsonDeserializer#handledType()}, which must not be {@code null}.
277+
* @since 4.3
278+
* @see #setDeserializersByType(Map)
279+
*/
280+
public void setDeserializers(JsonDeserializer<?>... deserializers) {
281+
this.builder.deserializers(deserializers);
282+
}
283+
275284
/**
276285
* Configure custom deserializers for the given types.
277286
*/

0 commit comments

Comments
 (0)