GH-1043 Add support for communicating type via MimeType parameter for JsonMessageConverter
Resolves #1043
This commit is contained in:
@@ -31,6 +31,7 @@ import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.messaging.converter.AbstractMessageConverter;
|
||||
import org.springframework.messaging.converter.MessageConverter;
|
||||
import org.springframework.util.MimeType;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Implementation of {@link MessageConverter} which uses Jackson or Gson libraries to do the
|
||||
@@ -71,7 +72,15 @@ public class JsonMessageConverter extends AbstractMessageConverter {
|
||||
|
||||
@Override
|
||||
protected boolean canConvertFrom(Message<?> message, @Nullable Class<?> targetClass) {
|
||||
if (targetClass == null || !supportsMimeType(message.getHeaders())) {
|
||||
return supportsMimeType(message.getHeaders()) && this.canDiscoverConvertToType(message, targetClass);
|
||||
}
|
||||
|
||||
private boolean canDiscoverConvertToType(Message<?> message, Class<?> targetClass) {
|
||||
if (targetClass == null || targetClass == Object.class) {
|
||||
MimeType mimeType = getMimeType(message.getHeaders());
|
||||
if (StringUtils.hasText(mimeType.getParameter("type"))) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -83,8 +92,20 @@ public class JsonMessageConverter extends AbstractMessageConverter {
|
||||
conversionHint = ((ParameterizedTypeReference<?>) conversionHint).getType();
|
||||
}
|
||||
Type convertToType = this.getResolvedType(targetClass, conversionHint);
|
||||
if (convertToType == Object.class) {
|
||||
return message.getPayload();
|
||||
if (convertToType == null || convertToType == Object.class) {
|
||||
MimeType mimeType = getMimeType(message.getHeaders());
|
||||
String type = mimeType.getParameter("type");
|
||||
if (StringUtils.hasText(type)) {
|
||||
try {
|
||||
convertToType = Thread.currentThread().getContextClassLoader().loadClass(type);
|
||||
}
|
||||
catch (ClassNotFoundException e) {
|
||||
throw new IllegalArgumentException("Failed to load class `" + type + "` specified by the provided content-type: " + mimeType, e);
|
||||
}
|
||||
}
|
||||
else {
|
||||
return message.getPayload();
|
||||
}
|
||||
}
|
||||
if (targetClass == byte[].class && message.getPayload() instanceof String) {
|
||||
return ((String) message.getPayload()).getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright 2023-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.function.context.config;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.cloud.function.json.JacksonMapper;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
*/
|
||||
public class JsonMessageConverterTests {
|
||||
|
||||
@Test
|
||||
public void testTypeInference() {
|
||||
JsonMessageConverter converter = new JsonMessageConverter(new JacksonMapper(new ObjectMapper()));
|
||||
|
||||
Message<String> message = MessageBuilder.withPayload("{\"name\":\"bill\"}").build();
|
||||
assertThat(converter.canConvertFrom(message, Person.class)).isTrue();
|
||||
|
||||
message = MessageBuilder.withPayload("{\"name\":\"bill\"}").setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON).build();
|
||||
assertThat(converter.canConvertFrom(message, Person.class)).isTrue();
|
||||
assertThat(converter.canConvertFrom(message, Object.class)).isFalse();
|
||||
assertThat(converter.canConvertFrom(message, null)).isFalse();
|
||||
assertThat(converter.convertFromInternal(message, Person.class, null)).isInstanceOf(Person.class);
|
||||
|
||||
message = MessageBuilder.withPayload("{\"name\":\"bill\"}")
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON.toString() + ";type=" + Person.class.getName()).build();
|
||||
assertThat(converter.canConvertFrom(message, Object.class)).isTrue();
|
||||
assertThat(converter.canConvertFrom(message, null)).isTrue();
|
||||
assertThat(converter.convertFromInternal(message, Person.class, null)).isInstanceOf(Person.class);
|
||||
assertThat(converter.convertFromInternal(message, Object.class, null)).isInstanceOf(Person.class);
|
||||
assertThat(converter.convertFromInternal(message, null, null)).isInstanceOf(Person.class);
|
||||
}
|
||||
|
||||
public static class Person {
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user