本文整理汇总了Java中org.springframework.messaging.converter.MessageConverter.fromMessage方法的典型用法代码示例。如果您正苦于以下问题:Java MessageConverter.fromMessage方法的具体用法?Java MessageConverter.fromMessage怎么用?Java MessageConverter.fromMessage使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.messaging.converter.MessageConverter
的用法示例。
在下文中一共展示了MessageConverter.fromMessage方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addRemoteRegistryDto
import org.springframework.messaging.converter.MessageConverter; //导入方法依赖的package包/类
void addRemoteRegistryDto(Message<?> message, MessageConverter converter, long expirationPeriod) {
UserRegistryDto registryDto = (UserRegistryDto) converter.fromMessage(message, UserRegistryDto.class);
if (registryDto != null && !registryDto.getId().equals(this.id)) {
long expirationTime = System.currentTimeMillis() + expirationPeriod;
registryDto.setExpirationTime(expirationTime);
registryDto.restoreParentReferences();
this.remoteRegistries.put(registryDto.getId(), registryDto);
}
}
示例2: doConvert
import org.springframework.messaging.converter.MessageConverter; //导入方法依赖的package包/类
/**
* Convert from the given message to the given target class.
* @param message the message to convert
* @param targetClass the target class to convert the payload to
* @return the converted payload of the reply message (never {@code null})
*/
@SuppressWarnings("unchecked")
protected <T> T doConvert(Message<?> message, Class<T> targetClass) {
MessageConverter messageConverter = getMessageConverter();
T value = (T) messageConverter.fromMessage(message, targetClass);
if (value == null) {
throw new MessageConversionException(message, "Unable to convert payload [" + message.getPayload() +
"] to type [" + targetClass + "] using converter [" + messageConverter + "]");
}
return value;
}
示例3: deserializePayload
import org.springframework.messaging.converter.MessageConverter; //导入方法依赖的package包/类
/**
* Will *only* deserialize payload if its 'contentType' is 'text/* or application/json' or Java/Kryo serialized.
* While this would naturally happen via MessageConverters at the time of handler method
* invocation, doing it here also is strictly to support behavior established
* in previous versions of SCSt. One of these cases is return payload as String if contentType is text or json.
* Also to support certain type of assumptions on type-less handlers (i.e., handle(?) vs. handle(Foo));
*/
private Object deserializePayload(Message<?> message, MimeType contentTypeToUse) {
Object payload = null;
if ("text".equalsIgnoreCase(contentTypeToUse.getType()) || equalTypeAndSubType(MimeTypeUtils.APPLICATION_JSON, contentTypeToUse)) {
payload = new String((byte[])message.getPayload(), StandardCharsets.UTF_8);
}
else {
message = MessageBuilder.fromMessage(message).setHeader(MessageHeaders.CONTENT_TYPE, contentTypeToUse).build();
MessageConverter converter = equalTypeAndSubType(MessageConverterUtils.X_JAVA_SERIALIZED_OBJECT, contentTypeToUse)
? compositeMessageConverterFactory.getMessageConverterForType(contentTypeToUse)
: compositeMessageConverterFactory.getMessageConverterForAllRegistered();
String targetClassName = contentTypeToUse.getParameter("type");
Class<?> targetClass = null;
if (StringUtils.hasText(targetClassName)) {
try {
targetClass = Class.forName(targetClassName, false, Thread.currentThread().getContextClassLoader());
}
catch (Exception e) {
throw new IllegalStateException("Failed to determine class name for contentType: "
+ message.getHeaders().get(BinderHeaders.BINDER_ORIGINAL_CONTENT_TYPE), e);
}
}
Assert.isTrue(!(equalTypeAndSubType(MessageConverterUtils.X_JAVA_OBJECT, contentTypeToUse) && targetClass == null),
"Cannot deserialize into message since 'contentType` is not "
+ "encoded with the actual target type."
+ "Consider 'application/x-java-object; type=foo.bar.MyClass'");
payload = converter.fromMessage(message, targetClass);
}
return payload;
}
示例4: testToMessageAndFromMessage
import org.springframework.messaging.converter.MessageConverter; //导入方法依赖的package包/类
@Test
public void testToMessageAndFromMessage() throws Exception {
String content = "stringwithspecialcharsöäü€a8";
MySerializableClass sourceMessage = new MySerializableClass(content);
MessageConverter messageConverter = new ObjectMessageConverter();
Message<?> message = messageConverter.toMessage(sourceMessage, getMessageHeaders("UTF-8"));
assertTrue(Base64.isBase64(message.getPayload().toString().getBytes("UTF-8")));
MySerializableClass result = (MySerializableClass) messageConverter.fromMessage(message, MySerializableClass.class);
assertEquals(content, result.getContent());
}
示例5: testToMessageAndFromMessageWithCustomEncoding
import org.springframework.messaging.converter.MessageConverter; //导入方法依赖的package包/类
@Test
public void testToMessageAndFromMessageWithCustomEncoding() throws Exception {
String content = "stringwithspecialcharsöäü€a8";
MySerializableClass sourceMessage = new MySerializableClass(content);
MessageConverter messageConverter = new ObjectMessageConverter("ISO-8859-1");
Message<?> message = messageConverter.toMessage(sourceMessage, getMessageHeaders("ISO-8859-1"));
assertTrue(Base64.isBase64(message.getPayload().toString().getBytes("ISO-8859-1")));
MySerializableClass result = (MySerializableClass) messageConverter.fromMessage(message, MySerializableClass.class);
assertEquals(content, result.getContent());
}
示例6: preSend
import org.springframework.messaging.converter.MessageConverter; //导入方法依赖的package包/类
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
Class<?> targetClass = null;
MessageConverter converter = null;
MimeType contentType = message.getHeaders().containsKey(BinderHeaders.BINDER_ORIGINAL_CONTENT_TYPE)
? MimeType.valueOf((String)message.getHeaders().get(BinderHeaders.BINDER_ORIGINAL_CONTENT_TYPE))
: contentTypeResolver.resolve(message.getHeaders());
if (contentType != null){
if (equalTypeAndSubType(MessageConverterUtils.X_JAVA_SERIALIZED_OBJECT, contentType) ||
equalTypeAndSubType(MessageConverterUtils.X_JAVA_OBJECT, contentType)) {
// for Java and Kryo de-serialization we need to reset the content type
message = MessageBuilder.fromMessage(message).setHeader(MessageHeaders.CONTENT_TYPE, contentType).build();
converter = equalTypeAndSubType(MessageConverterUtils.X_JAVA_SERIALIZED_OBJECT, contentType)
? converterFactory.getMessageConverterForType(contentType)
: converterFactory.getMessageConverterForAllRegistered();
String targetClassName = contentType.getParameter("type");
if (StringUtils.hasText(targetClassName)) {
try {
targetClass = Class.forName(targetClassName, false, Thread.currentThread().getContextClassLoader());
}
catch (Exception e) {
throw new IllegalStateException("Failed to determine class name for contentType: "
+ message.getHeaders().get(BinderHeaders.BINDER_ORIGINAL_CONTENT_TYPE), e);
}
}
}
}
Object payload;
if (converter != null){
Assert.isTrue(!(equalTypeAndSubType(MessageConverterUtils.X_JAVA_OBJECT, contentType) && targetClass == null),
"Cannot deserialize into message since 'contentType` is not "
+ "encoded with the actual target type."
+ "Consider 'application/x-java-object; type=foo.bar.MyClass'");
payload = converter.fromMessage(message, targetClass);
}
else {
MimeType deserializeContentType = contentTypeResolver.resolve(message.getHeaders());
if (deserializeContentType == null) {
deserializeContentType = contentType;
}
payload = deserializeContentType == null ? message.getPayload() : this.deserializePayload(message.getPayload(), deserializeContentType);
}
message = MessageBuilder.withPayload(payload)
.copyHeaders(message.getHeaders())
.setHeader(MessageHeaders.CONTENT_TYPE, contentType)
.removeHeader(BinderHeaders.BINDER_ORIGINAL_CONTENT_TYPE)
.build();
return message;
}