|
59 | 59 | */
|
60 | 60 | public class MappingJackson2MessageConverter extends AbstractMessageConverter {
|
61 | 61 |
|
| 62 | + private static final MimeType[] DEFAULT_MIME_TYPES = new MimeType[] { |
| 63 | + new MimeType("application", "json"), new MimeType("application", "*+json")}; |
| 64 | + |
62 | 65 | private ObjectMapper objectMapper;
|
63 | 66 |
|
64 | 67 | @Nullable
|
65 | 68 | private Boolean prettyPrint;
|
66 | 69 |
|
67 | 70 |
|
68 | 71 | /**
|
69 |
| - * Construct a {@code MappingJackson2MessageConverter} supporting |
70 |
| - * the {@code application/json} MIME type with {@code UTF-8} character set. |
| 72 | + * Construct a {@code MappingJackson2MessageConverter} with a default {@link ObjectMapper}, |
| 73 | + * supporting the {@code application/json} MIME type with {@code UTF-8} character set. |
71 | 74 | */
|
72 | 75 | public MappingJackson2MessageConverter() {
|
73 |
| - super(new MimeType("application", "json"), new MimeType("application", "*+json")); |
74 |
| - this.objectMapper = initObjectMapper(); |
| 76 | + this(DEFAULT_MIME_TYPES); |
75 | 77 | }
|
76 | 78 |
|
77 | 79 | /**
|
78 |
| - * Construct a {@code MappingJackson2MessageConverter} supporting |
79 |
| - * one or more custom MIME types. |
| 80 | + * Construct a {@code MappingJackson2MessageConverter} with a default {@link ObjectMapper}, |
| 81 | + * supporting one or more custom MIME types. |
80 | 82 | * @param supportedMimeTypes the supported MIME types
|
81 | 83 | * @since 4.1.5
|
82 | 84 | */
|
| 85 | + @SuppressWarnings("deprecation") // on Jackson 2.13: configure(MapperFeature, boolean) |
83 | 86 | public MappingJackson2MessageConverter(MimeType... supportedMimeTypes) {
|
84 | 87 | super(supportedMimeTypes);
|
85 |
| - this.objectMapper = initObjectMapper(); |
| 88 | + this.objectMapper = new ObjectMapper(); |
| 89 | + this.objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false); |
| 90 | + this.objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
86 | 91 | }
|
87 | 92 |
|
| 93 | + /** |
| 94 | + * Construct a {@code MappingJackson2MessageConverter} with a custom {@link ObjectMapper}, |
| 95 | + * supporting the {@code application/json} MIME type with {@code UTF-8} character set. |
| 96 | + * @param objectMapper the ObjectMapper to use |
| 97 | + * @since 6.1 |
| 98 | + */ |
88 | 99 | public MappingJackson2MessageConverter(ObjectMapper objectMapper) {
|
89 |
| - super(new MimeType("application", "json"), new MimeType("application", "*+json")); |
| 100 | + this(objectMapper, DEFAULT_MIME_TYPES); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Construct a {@code MappingJackson2MessageConverter} with a custom {@link ObjectMapper}, |
| 105 | + * supporting one or more custom MIME types. |
| 106 | + * @param objectMapper the ObjectMapper to use |
| 107 | + * @param supportedMimeTypes the supported MIME types |
| 108 | + * @since 6.1 |
| 109 | + */ |
| 110 | + public MappingJackson2MessageConverter(ObjectMapper objectMapper, MimeType... supportedMimeTypes) { |
| 111 | + super(supportedMimeTypes); |
90 | 112 | Assert.notNull(objectMapper, "ObjectMapper must not be null");
|
91 | 113 | this.objectMapper = objectMapper;
|
92 | 114 | }
|
93 | 115 |
|
94 | 116 |
|
95 |
| - |
96 |
| - @SuppressWarnings("deprecation") // on Jackson 2.13: configure(MapperFeature, boolean) |
97 |
| - private ObjectMapper initObjectMapper() { |
98 |
| - ObjectMapper objectMapper = new ObjectMapper(); |
99 |
| - objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false); |
100 |
| - objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
101 |
| - return objectMapper; |
102 |
| - } |
103 |
| - |
104 | 117 | /**
|
105 | 118 | * Set the {@code ObjectMapper} for this converter.
|
106 | 119 | * If not set, a default {@link ObjectMapper#ObjectMapper() ObjectMapper} is used.
|
|
0 commit comments