4040/**
4141 * A Jackson 3.x based {@link MessageConverter} implementation.
4242 *
43- * <p>The default constructor loads {@link tools.jackson.databind.JacksonModule}s
44- * found by {@link MapperBuilder#findModules(ClassLoader)}.
45- *
4643 * @author Sebastien Deleuze
4744 * @since 7.0
4845 */
@@ -51,7 +48,7 @@ public class JacksonJsonMessageConverter extends AbstractMessageConverter {
5148 private static final MimeType [] DEFAULT_MIME_TYPES = new MimeType [] {
5249 new MimeType ("application" , "json" ), new MimeType ("application" , "*+json" )};
5350
54- private final JsonMapper jsonMapper ;
51+ private final JsonMapper mapper ;
5552
5653
5754 /**
@@ -71,36 +68,58 @@ public JacksonJsonMessageConverter() {
7168 * @param supportedMimeTypes the supported MIME types
7269 */
7370 public JacksonJsonMessageConverter (MimeType ... supportedMimeTypes ) {
74- super (supportedMimeTypes );
75- this .jsonMapper = JsonMapper .builder ().findAndAddModules (JacksonJsonMessageConverter .class .getClassLoader ()).build ();
71+ this (JsonMapper .builder (), supportedMimeTypes );
7672 }
7773
7874 /**
7975 * Construct a new instance with the provided {@link JsonMapper}.
8076 * @see JsonMapper#builder()
81- * @see MapperBuilder#findModules(ClassLoader)
8277 */
83- public JacksonJsonMessageConverter (JsonMapper jsonMapper ) {
84- this (jsonMapper , DEFAULT_MIME_TYPES );
78+ public JacksonJsonMessageConverter (JsonMapper mapper ) {
79+ this (mapper , DEFAULT_MIME_TYPES );
80+ }
81+
82+
83+ /**
84+ * Construct a new instance with the provided {@link JsonMapper.Builder} customized
85+ * with the {@link tools.jackson.databind.JacksonModule}s found
86+ * by {@link MapperBuilder#findModules(ClassLoader)}.
87+ * @see JsonMapper#builder()
88+ */
89+ public JacksonJsonMessageConverter (JsonMapper .Builder builder ) {
90+ this (builder , DEFAULT_MIME_TYPES );
8591 }
8692
8793 /**
8894 * Construct a new instance with the provided {@link JsonMapper} and the
8995 * provided {@link MimeType}s.
9096 * @see JsonMapper#builder()
91- * @see MapperBuilder#findModules(ClassLoader)
9297 */
93- public JacksonJsonMessageConverter (JsonMapper jsonMapper , MimeType ... supportedMimeTypes ) {
98+ public JacksonJsonMessageConverter (JsonMapper mapper , MimeType ... supportedMimeTypes ) {
99+ super (supportedMimeTypes );
100+ Assert .notNull (mapper , "JsonMapper must not be null" );
101+ this .mapper = mapper ;
102+ }
103+
104+ /**
105+ * Construct a new instance with the provided {@link JsonMapper} customized
106+ * with the {@link tools.jackson.databind.JacksonModule}s found by
107+ * {@link MapperBuilder#findModules(ClassLoader)}, and the provided
108+ * {@link MimeType}s.
109+ * @see JsonMapper#builder()
110+ */
111+ public JacksonJsonMessageConverter (JsonMapper .Builder builder , MimeType ... supportedMimeTypes ) {
94112 super (supportedMimeTypes );
95- Assert .notNull (jsonMapper , "JsonMapper must not be null" );
96- this .jsonMapper = jsonMapper ;
113+ Assert .notNull (builder , "JsonMapper.Builder must not be null" );
114+ this .mapper = builder . findAndAddModules ( JacksonJsonMessageConverter . class . getClassLoader ()). build () ;
97115 }
98116
117+
99118 /**
100119 * Return the underlying {@code JsonMapper} for this converter.
101120 */
102121 protected JsonMapper getJsonMapper () {
103- return this .jsonMapper ;
122+ return this .mapper ;
104123 }
105124
106125 @ Override
@@ -121,7 +140,7 @@ protected boolean supports(Class<?> clazz) {
121140
122141 @ Override
123142 protected @ Nullable Object convertFromInternal (Message <?> message , Class <?> targetClass , @ Nullable Object conversionHint ) {
124- JavaType javaType = this .jsonMapper .constructType (getResolvedType (targetClass , conversionHint ));
143+ JavaType javaType = this .mapper .constructType (getResolvedType (targetClass , conversionHint ));
125144 Object payload = message .getPayload ();
126145 Class <?> view = getSerializationView (conversionHint );
127146 try {
@@ -130,19 +149,19 @@ protected boolean supports(Class<?> clazz) {
130149 }
131150 else if (payload instanceof byte [] bytes ) {
132151 if (view != null ) {
133- return this .jsonMapper .readerWithView (view ).forType (javaType ).readValue (bytes );
152+ return this .mapper .readerWithView (view ).forType (javaType ).readValue (bytes );
134153 }
135154 else {
136- return this .jsonMapper .readValue (bytes , javaType );
155+ return this .mapper .readValue (bytes , javaType );
137156 }
138157 }
139158 else {
140159 // Assuming a text-based source payload
141160 if (view != null ) {
142- return this .jsonMapper .readerWithView (view ).forType (javaType ).readValue (payload .toString ());
161+ return this .mapper .readerWithView (view ).forType (javaType ).readValue (payload .toString ());
143162 }
144163 else {
145- return this .jsonMapper .readValue (payload .toString (), javaType );
164+ return this .mapper .readValue (payload .toString (), javaType );
146165 }
147166 }
148167 }
@@ -160,12 +179,12 @@ else if (payload instanceof byte[] bytes) {
160179 if (byte [].class == getSerializedPayloadClass ()) {
161180 ByteArrayOutputStream out = new ByteArrayOutputStream (1024 );
162181 JsonEncoding encoding = getJsonEncoding (getMimeType (headers ));
163- try (JsonGenerator generator = this .jsonMapper .createGenerator (out , encoding )) {
182+ try (JsonGenerator generator = this .mapper .createGenerator (out , encoding )) {
164183 if (view != null ) {
165- this .jsonMapper .writerWithView (view ).writeValue (generator , payload );
184+ this .mapper .writerWithView (view ).writeValue (generator , payload );
166185 }
167186 else {
168- this .jsonMapper .writeValue (generator , payload );
187+ this .mapper .writeValue (generator , payload );
169188 }
170189 payload = out .toByteArray ();
171190 }
@@ -174,10 +193,10 @@ else if (payload instanceof byte[] bytes) {
174193 // Assuming a text-based target payload
175194 Writer writer = new StringWriter (1024 );
176195 if (view != null ) {
177- this .jsonMapper .writerWithView (view ).writeValue (writer , payload );
196+ this .mapper .writerWithView (view ).writeValue (writer , payload );
178197 }
179198 else {
180- this .jsonMapper .writeValue (writer , payload );
199+ this .mapper .writeValue (writer , payload );
181200 }
182201 payload = writer .toString ();
183202 }
0 commit comments