|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2018 the original author or authors. |
| 2 | + * Copyright 2002-2019 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
@@ -74,7 +74,8 @@ public class MappingJackson2MessageConverter implements SmartMessageConverter, B
|
74 | 74 |
|
75 | 75 | private MessageType targetType = MessageType.BYTES;
|
76 | 76 |
|
77 |
| - private String encoding = DEFAULT_ENCODING; |
| 77 | + @Nullable |
| 78 | + private String encoding; |
78 | 79 |
|
79 | 80 | @Nullable
|
80 | 81 | private String encodingPropertyName;
|
@@ -293,13 +294,21 @@ protected BytesMessage mapToBytesMessage(Object object, Session session, ObjectW
|
293 | 294 | throws JMSException, IOException {
|
294 | 295 |
|
295 | 296 | ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
|
296 |
| - OutputStreamWriter writer = new OutputStreamWriter(bos, this.encoding); |
297 |
| - objectWriter.writeValue(writer, object); |
| 297 | + if (this.encoding != null) { |
| 298 | + OutputStreamWriter writer = new OutputStreamWriter(bos, this.encoding); |
| 299 | + objectWriter.writeValue(writer, object); |
| 300 | + } |
| 301 | + else { |
| 302 | + // Jackson usually defaults to UTF-8 but can also go straight to bytes, e.g. for Smile. |
| 303 | + // We use a direct byte array argument for the latter case to work as well. |
| 304 | + objectWriter.writeValue(bos, object); |
| 305 | + } |
298 | 306 |
|
299 | 307 | BytesMessage message = session.createBytesMessage();
|
300 | 308 | message.writeBytes(bos.toByteArray());
|
301 | 309 | if (this.encodingPropertyName != null) {
|
302 |
| - message.setStringProperty(this.encodingPropertyName, this.encoding); |
| 310 | + message.setStringProperty(this.encodingPropertyName, |
| 311 | + (this.encoding != null ? this.encoding : DEFAULT_ENCODING)); |
303 | 312 | }
|
304 | 313 | return message;
|
305 | 314 | }
|
@@ -393,12 +402,18 @@ protected Object convertFromBytesMessage(BytesMessage message, JavaType targetJa
|
393 | 402 | }
|
394 | 403 | byte[] bytes = new byte[(int) message.getBodyLength()];
|
395 | 404 | message.readBytes(bytes);
|
396 |
| - try { |
397 |
| - String body = new String(bytes, encoding); |
398 |
| - return this.objectMapper.readValue(body, targetJavaType); |
| 405 | + if (encoding != null) { |
| 406 | + try { |
| 407 | + String body = new String(bytes, encoding); |
| 408 | + return this.objectMapper.readValue(body, targetJavaType); |
| 409 | + } |
| 410 | + catch (UnsupportedEncodingException ex) { |
| 411 | + throw new MessageConversionException("Cannot convert bytes to String", ex); |
| 412 | + } |
399 | 413 | }
|
400 |
| - catch (UnsupportedEncodingException ex) { |
401 |
| - throw new MessageConversionException("Cannot convert bytes to String", ex); |
| 414 | + else { |
| 415 | + // Jackson internally performs encoding detection, falling back to UTF-8. |
| 416 | + return this.objectMapper.readValue(bytes, targetJavaType); |
402 | 417 | }
|
403 | 418 | }
|
404 | 419 |
|
|
0 commit comments