|
| 1 | +/* |
| 2 | + * Copyright 2002-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.messaging.converter; |
| 18 | + |
| 19 | +import java.io.ByteArrayOutputStream; |
| 20 | +import java.io.StringWriter; |
| 21 | +import java.io.Writer; |
| 22 | +import java.nio.charset.Charset; |
| 23 | + |
| 24 | +import com.fasterxml.jackson.annotation.JsonView; |
| 25 | +import org.jspecify.annotations.Nullable; |
| 26 | +import tools.jackson.core.JacksonException; |
| 27 | +import tools.jackson.core.JsonEncoding; |
| 28 | +import tools.jackson.core.JsonGenerator; |
| 29 | +import tools.jackson.databind.JavaType; |
| 30 | +import tools.jackson.databind.ObjectMapper; |
| 31 | +import tools.jackson.databind.cfg.MapperBuilder; |
| 32 | +import tools.jackson.databind.json.JsonMapper; |
| 33 | + |
| 34 | +import org.springframework.core.MethodParameter; |
| 35 | +import org.springframework.messaging.Message; |
| 36 | +import org.springframework.messaging.MessageHeaders; |
| 37 | +import org.springframework.util.Assert; |
| 38 | +import org.springframework.util.ClassUtils; |
| 39 | +import org.springframework.util.MimeType; |
| 40 | + |
| 41 | +/** |
| 42 | + * A Jackson 3.x based {@link MessageConverter} implementation. |
| 43 | + * |
| 44 | + * <p>The default constructor loads {@link tools.jackson.databind.JacksonModule}s |
| 45 | + * found by {@link MapperBuilder#findModules(ClassLoader)}. |
| 46 | + * |
| 47 | + * @author Sebastien Deleuze |
| 48 | + * @since 7.0 |
| 49 | + */ |
| 50 | +public class JacksonJsonMessageConverter extends AbstractMessageConverter { |
| 51 | + |
| 52 | + private static final MimeType[] DEFAULT_MIME_TYPES = new MimeType[] { |
| 53 | + new MimeType("application", "json"), new MimeType("application", "*+json")}; |
| 54 | + |
| 55 | + private final ObjectMapper objectMapper; |
| 56 | + |
| 57 | + |
| 58 | + /** |
| 59 | + * Construct a new instance with a {@link JsonMapper} customized with the |
| 60 | + * {@link tools.jackson.databind.JacksonModule}s found by |
| 61 | + * {@link MapperBuilder#findModules(ClassLoader)}. |
| 62 | + */ |
| 63 | + public JacksonJsonMessageConverter() { |
| 64 | + this(DEFAULT_MIME_TYPES); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Construct a new instance with a {@link JsonMapper} customized |
| 69 | + * with the {@link tools.jackson.databind.JacksonModule}s found |
| 70 | + * by {@link MapperBuilder#findModules(ClassLoader)} and the |
| 71 | + * provided {@link MimeType}s. |
| 72 | + * @param supportedMimeTypes the supported MIME types |
| 73 | + */ |
| 74 | + public JacksonJsonMessageConverter(MimeType... supportedMimeTypes) { |
| 75 | + super(supportedMimeTypes); |
| 76 | + this.objectMapper = JsonMapper.builder().findAndAddModules(JacksonJsonMessageConverter.class.getClassLoader()).build(); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Construct a new instance with the provided {@link ObjectMapper}. |
| 81 | + * @see JsonMapper#builder() |
| 82 | + * @see MapperBuilder#findModules(ClassLoader) |
| 83 | + */ |
| 84 | + public JacksonJsonMessageConverter(ObjectMapper objectMapper) { |
| 85 | + this(objectMapper, DEFAULT_MIME_TYPES); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Construct a new instance with the provided {@link ObjectMapper} and the |
| 90 | + * provided {@link MimeType}s. |
| 91 | + * @see JsonMapper#builder() |
| 92 | + * @see MapperBuilder#findModules(ClassLoader) |
| 93 | + */ |
| 94 | + public JacksonJsonMessageConverter(ObjectMapper objectMapper, MimeType... supportedMimeTypes) { |
| 95 | + super(supportedMimeTypes); |
| 96 | + Assert.notNull(objectMapper, "ObjectMapper must not be null"); |
| 97 | + this.objectMapper = objectMapper; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + protected boolean canConvertFrom(Message<?> message, @Nullable Class<?> targetClass) { |
| 102 | + return targetClass != null && supportsMimeType(message.getHeaders()); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + protected boolean canConvertTo(Object payload, @Nullable MessageHeaders headers) { |
| 107 | + return supportsMimeType(headers); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + protected boolean supports(Class<?> clazz) { |
| 112 | + // should not be called, since we override canConvertFrom/canConvertTo instead |
| 113 | + throw new UnsupportedOperationException(); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + protected @Nullable Object convertFromInternal(Message<?> message, Class<?> targetClass, @Nullable Object conversionHint) { |
| 118 | + JavaType javaType = this.objectMapper.constructType(getResolvedType(targetClass, conversionHint)); |
| 119 | + Object payload = message.getPayload(); |
| 120 | + Class<?> view = getSerializationView(conversionHint); |
| 121 | + try { |
| 122 | + if (ClassUtils.isAssignableValue(targetClass, payload)) { |
| 123 | + return payload; |
| 124 | + } |
| 125 | + else if (payload instanceof byte[] bytes) { |
| 126 | + if (view != null) { |
| 127 | + return this.objectMapper.readerWithView(view).forType(javaType).readValue(bytes); |
| 128 | + } |
| 129 | + else { |
| 130 | + return this.objectMapper.readValue(bytes, javaType); |
| 131 | + } |
| 132 | + } |
| 133 | + else { |
| 134 | + // Assuming a text-based source payload |
| 135 | + if (view != null) { |
| 136 | + return this.objectMapper.readerWithView(view).forType(javaType).readValue(payload.toString()); |
| 137 | + } |
| 138 | + else { |
| 139 | + return this.objectMapper.readValue(payload.toString(), javaType); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + catch (JacksonException ex) { |
| 144 | + throw new MessageConversionException(message, "Could not read JSON: " + ex.getMessage(), ex); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + protected @Nullable Object convertToInternal(Object payload, @Nullable MessageHeaders headers, |
| 150 | + @Nullable Object conversionHint) { |
| 151 | + |
| 152 | + try { |
| 153 | + Class<?> view = getSerializationView(conversionHint); |
| 154 | + if (byte[].class == getSerializedPayloadClass()) { |
| 155 | + ByteArrayOutputStream out = new ByteArrayOutputStream(1024); |
| 156 | + JsonEncoding encoding = getJsonEncoding(getMimeType(headers)); |
| 157 | + try (JsonGenerator generator = this.objectMapper.createGenerator(out, encoding)) { |
| 158 | + if (view != null) { |
| 159 | + this.objectMapper.writerWithView(view).writeValue(generator, payload); |
| 160 | + } |
| 161 | + else { |
| 162 | + this.objectMapper.writeValue(generator, payload); |
| 163 | + } |
| 164 | + payload = out.toByteArray(); |
| 165 | + } |
| 166 | + } |
| 167 | + else { |
| 168 | + // Assuming a text-based target payload |
| 169 | + Writer writer = new StringWriter(1024); |
| 170 | + if (view != null) { |
| 171 | + this.objectMapper.writerWithView(view).writeValue(writer, payload); |
| 172 | + } |
| 173 | + else { |
| 174 | + this.objectMapper.writeValue(writer, payload); |
| 175 | + } |
| 176 | + payload = writer.toString(); |
| 177 | + } |
| 178 | + } |
| 179 | + catch (JacksonException ex) { |
| 180 | + throw new MessageConversionException("Could not write JSON: " + ex.getMessage(), ex); |
| 181 | + } |
| 182 | + return payload; |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Determine a Jackson serialization view based on the given conversion hint. |
| 187 | + * @param conversionHint the conversion hint Object as passed into the |
| 188 | + * converter for the current conversion attempt |
| 189 | + * @return the serialization view class, or {@code null} if none |
| 190 | + */ |
| 191 | + protected @Nullable Class<?> getSerializationView(@Nullable Object conversionHint) { |
| 192 | + if (conversionHint instanceof MethodParameter param) { |
| 193 | + JsonView annotation = (param.getParameterIndex() >= 0 ? |
| 194 | + param.getParameterAnnotation(JsonView.class) : param.getMethodAnnotation(JsonView.class)); |
| 195 | + if (annotation != null) { |
| 196 | + return extractViewClass(annotation, conversionHint); |
| 197 | + } |
| 198 | + } |
| 199 | + else if (conversionHint instanceof JsonView jsonView) { |
| 200 | + return extractViewClass(jsonView, conversionHint); |
| 201 | + } |
| 202 | + else if (conversionHint instanceof Class<?> clazz) { |
| 203 | + return clazz; |
| 204 | + } |
| 205 | + |
| 206 | + // No JSON view specified... |
| 207 | + return null; |
| 208 | + } |
| 209 | + |
| 210 | + private Class<?> extractViewClass(JsonView annotation, Object conversionHint) { |
| 211 | + Class<?>[] classes = annotation.value(); |
| 212 | + if (classes.length != 1) { |
| 213 | + throw new IllegalArgumentException( |
| 214 | + "@JsonView only supported for handler methods with exactly 1 class argument: " + conversionHint); |
| 215 | + } |
| 216 | + return classes[0]; |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * Determine the JSON encoding to use for the given content type. |
| 221 | + * @param contentType the MIME type from the MessageHeaders, if any |
| 222 | + * @return the JSON encoding to use (never {@code null}) |
| 223 | + */ |
| 224 | + protected JsonEncoding getJsonEncoding(@Nullable MimeType contentType) { |
| 225 | + if (contentType != null && contentType.getCharset() != null) { |
| 226 | + Charset charset = contentType.getCharset(); |
| 227 | + for (JsonEncoding encoding : JsonEncoding.values()) { |
| 228 | + if (charset.name().equals(encoding.getJavaName())) { |
| 229 | + return encoding; |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | + return JsonEncoding.UTF8; |
| 234 | + } |
| 235 | + |
| 236 | +} |
0 commit comments