org.springframework.messaging.converter.SmartMessageConverter Java Examples
The following examples show how to use
org.springframework.messaging.converter.SmartMessageConverter.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MessageMethodArgumentResolver.java From spring-analysis-note with MIT License | 6 votes |
private Object convertPayload(Message<?> message, MethodParameter parameter, Class<?> targetPayloadType) { Object result = null; if (this.converter instanceof SmartMessageConverter) { SmartMessageConverter smartConverter = (SmartMessageConverter) this.converter; result = smartConverter.fromMessage(message, targetPayloadType, parameter); } else if (this.converter != null) { result = this.converter.fromMessage(message, targetPayloadType); } if (result == null) { throw new MessageConversionException(message, "No converter found from actual payload type '" + ClassUtils.getDescriptiveType(message.getPayload()) + "' to expected payload type '" + ClassUtils.getQualifiedName(targetPayloadType) + "'"); } return result; }
Example #2
Source File: MessageMethodArgumentResolver.java From java-technology-stack with MIT License | 6 votes |
private Object convertPayload(Message<?> message, MethodParameter parameter, Class<?> targetPayloadType) { Object result = null; if (this.converter instanceof SmartMessageConverter) { SmartMessageConverter smartConverter = (SmartMessageConverter) this.converter; result = smartConverter.fromMessage(message, targetPayloadType, parameter); } else if (this.converter != null) { result = this.converter.fromMessage(message, targetPayloadType); } if (result == null) { throw new MessageConversionException(message, "No converter found from actual payload type '" + ClassUtils.getDescriptiveType(message.getPayload()) + "' to expected payload type '" + ClassUtils.getQualifiedName(targetPayloadType) + "'"); } return result; }
Example #3
Source File: DefaultRocketMQListenerContainer.java From rocketmq-spring with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") private Object doConvertMessage(MessageExt messageExt) { if (Objects.equals(messageType, MessageExt.class)) { return messageExt; } else { String str = new String(messageExt.getBody(), Charset.forName(charset)); if (Objects.equals(messageType, String.class)) { return str; } else { // If msgType not string, use objectMapper change it. try { if (messageType instanceof Class) { //if the messageType has not Generic Parameter return this.getMessageConverter().fromMessage(MessageBuilder.withPayload(str).build(), (Class<?>) messageType); } else { //if the messageType has Generic Parameter, then use SmartMessageConverter#fromMessage with third parameter "conversionHint". //we have validate the MessageConverter is SmartMessageConverter in this#getMethodParameter. return ((SmartMessageConverter) this.getMessageConverter()).fromMessage(MessageBuilder.withPayload(str).build(), (Class<?>) ((ParameterizedType) messageType).getRawType(), methodParameter); } } catch (Exception e) { log.info("convert failed. str:{}, msgType:{}", str, messageType); throw new RuntimeException("cannot convert message to " + messageType, e); } } } }
Example #4
Source File: DefaultRocketMQListenerContainer.java From rocketmq-spring with Apache License 2.0 | 6 votes |
private MethodParameter getMethodParameter() { Class<?> targetClass; if (rocketMQListener != null) { targetClass = AopProxyUtils.ultimateTargetClass(rocketMQListener); } else { targetClass = AopProxyUtils.ultimateTargetClass(rocketMQReplyListener); } Type messageType = this.getMessageType(); Class clazz = null; if (messageType instanceof ParameterizedType && messageConverter instanceof SmartMessageConverter) { clazz = (Class) ((ParameterizedType) messageType).getRawType(); } else if (messageType instanceof Class) { clazz = (Class) messageType; } else { throw new RuntimeException("parameterType:" + messageType + " of onMessage method is not supported"); } try { final Method method = targetClass.getMethod("onMessage", clazz); return new MethodParameter(method, 0); } catch (NoSuchMethodException e) { e.printStackTrace(); throw new RuntimeException("parameterType:" + messageType + " of onMessage method is not supported"); } }
Example #5
Source File: SmartMessageMethodArgumentResolver.java From spring-cloud-stream with Apache License 2.0 | 6 votes |
private Object convertPayload(Message<?> message, MethodParameter parameter, Class<?> targetPayloadType) { Object result = null; if (this.messageConverter instanceof SmartMessageConverter) { SmartMessageConverter smartConverter = (SmartMessageConverter) this.messageConverter; result = smartConverter.fromMessage(message, targetPayloadType, parameter); } else if (this.messageConverter != null) { result = this.messageConverter.fromMessage(message, targetPayloadType); } if (result == null) { throw new MessageConversionException(message, "No converter found from actual payload type '" + ClassUtils.getDescriptiveType(message.getPayload()) + "' to expected payload type '" + ClassUtils.getQualifiedName(targetPayloadType) + "'"); } return result; }
Example #6
Source File: DefaultRocketMQListenerContainer.java From rocketmq-spring with Apache License 2.0 | 5 votes |
private Message<?> doConvert(Object payload, MessageHeaders headers) { Message<?> message = this.messageConverter instanceof SmartMessageConverter ? ((SmartMessageConverter) this.messageConverter).toMessage(payload, headers, null) : this.messageConverter.toMessage(payload, headers); if (message == null) { String payloadType = payload.getClass().getName(); Object contentType = headers != null ? headers.get(MessageHeaders.CONTENT_TYPE) : null; throw new MessageConversionException("Unable to convert payload with type='" + payloadType + "', contentType='" + contentType + "', converter=[" + this.messageConverter + "]"); } MessageBuilder<?> builder = MessageBuilder.fromMessage(message); builder.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN); return builder.build(); }
Example #7
Source File: RocketMQTemplate.java From rocketmq-spring with Apache License 2.0 | 5 votes |
private Object doConvertMessage(MessageExt messageExt, Type type) { if (Objects.equals(type, MessageExt.class)) { return messageExt; } else if (Objects.equals(type, byte[].class)) { return messageExt.getBody(); } else { String str = new String(messageExt.getBody(), Charset.forName(charset)); if (Objects.equals(type, String.class)) { return str; } else { // If msgType not string, use objectMapper change it. try { if (type instanceof Class) { //if the messageType has not Generic Parameter return this.getMessageConverter().fromMessage(MessageBuilder.withPayload(str).build(), (Class<?>) type); } else { //if the messageType has Generic Parameter, then use SmartMessageConverter#fromMessage with third parameter "conversionHint". //we have validate the MessageConverter is SmartMessageConverter in this#getMethodParameter. return ((SmartMessageConverter) this.getMessageConverter()).fromMessage(MessageBuilder.withPayload(str).build(), (Class<?>) ((ParameterizedType) type).getRawType(), null); } } catch (Exception e) { log.error("convert failed. str:{}, msgType:{}", str, type); throw new RuntimeException("cannot convert message to " + type, e); } } } }
Example #8
Source File: PayloadArgumentResolver.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Override public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception { Payload ann = parameter.getParameterAnnotation(Payload.class); if (ann != null && StringUtils.hasText(ann.expression())) { throw new IllegalStateException("@Payload SpEL expressions not supported by this resolver"); } Object payload = message.getPayload(); if (isEmptyPayload(payload)) { if (ann == null || ann.required()) { String paramName = getParameterName(parameter); BindingResult bindingResult = new BeanPropertyBindingResult(payload, paramName); bindingResult.addError(new ObjectError(paramName, "Payload value must not be empty")); throw new MethodArgumentNotValidException(message, parameter, bindingResult); } else { return null; } } Class<?> targetClass = parameter.getParameterType(); if (ClassUtils.isAssignable(targetClass, payload.getClass())) { validate(message, parameter, payload); return payload; } else { payload = (this.converter instanceof SmartMessageConverter ? ((SmartMessageConverter) this.converter).fromMessage(message, targetClass, parameter) : this.converter.fromMessage(message, targetClass)); if (payload == null) { throw new MessageConversionException(message, "No converter found to convert to " + targetClass + ", message=" + message); } validate(message, parameter, payload); return payload; } }
Example #9
Source File: MessageSourceBindingTargetFactory.java From spring-cloud-stream with Apache License 2.0 | 5 votes |
public MessageSourceBindingTargetFactory(SmartMessageConverter messageConverter, MessageChannelConfigurer messageSourceConfigurer) { super(PollableMessageSource.class); Assert.isInstanceOf(MessageChannelAndSourceConfigurer.class, messageSourceConfigurer); this.messageSourceConfigurer = (MessageChannelAndSourceConfigurer) messageSourceConfigurer; this.messageConverter = messageConverter; }
Example #10
Source File: PayloadMethodArgumentResolver.java From spring-analysis-note with MIT License | 4 votes |
@Override @Nullable public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception { Payload ann = parameter.getParameterAnnotation(Payload.class); if (ann != null && StringUtils.hasText(ann.expression())) { throw new IllegalStateException("@Payload SpEL expressions not supported by this resolver"); } Object payload = message.getPayload(); if (isEmptyPayload(payload)) { if (ann == null || ann.required()) { String paramName = getParameterName(parameter); BindingResult bindingResult = new BeanPropertyBindingResult(payload, paramName); bindingResult.addError(new ObjectError(paramName, "Payload value must not be empty")); throw new MethodArgumentNotValidException(message, parameter, bindingResult); } else { return null; } } Class<?> targetClass = resolveTargetClass(parameter, message); Class<?> payloadClass = payload.getClass(); if (ClassUtils.isAssignable(targetClass, payloadClass)) { validate(message, parameter, payload); return payload; } else { if (this.converter instanceof SmartMessageConverter) { SmartMessageConverter smartConverter = (SmartMessageConverter) this.converter; payload = smartConverter.fromMessage(message, targetClass, parameter); } else { payload = this.converter.fromMessage(message, targetClass); } if (payload == null) { throw new MessageConversionException(message, "Cannot convert from [" + payloadClass.getName() + "] to [" + targetClass.getName() + "] for " + message); } validate(message, parameter, payload); return payload; } }
Example #11
Source File: PayloadArgumentResolver.java From java-technology-stack with MIT License | 4 votes |
@Override @Nullable public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception { Payload ann = parameter.getParameterAnnotation(Payload.class); if (ann != null && StringUtils.hasText(ann.expression())) { throw new IllegalStateException("@Payload SpEL expressions not supported by this resolver"); } Object payload = message.getPayload(); if (isEmptyPayload(payload)) { if (ann == null || ann.required()) { String paramName = getParameterName(parameter); BindingResult bindingResult = new BeanPropertyBindingResult(payload, paramName); bindingResult.addError(new ObjectError(paramName, "Payload value must not be empty")); throw new MethodArgumentNotValidException(message, parameter, bindingResult); } else { return null; } } Class<?> targetClass = parameter.getParameterType(); Class<?> payloadClass = payload.getClass(); if (ClassUtils.isAssignable(targetClass, payloadClass)) { validate(message, parameter, payload); return payload; } else { if (this.converter instanceof SmartMessageConverter) { SmartMessageConverter smartConverter = (SmartMessageConverter) this.converter; payload = smartConverter.fromMessage(message, targetClass, parameter); } else { payload = this.converter.fromMessage(message, targetClass); } if (payload == null) { throw new MessageConversionException(message, "Cannot convert from [" + payloadClass.getName() + "] to [" + targetClass.getName() + "] for " + message); } validate(message, parameter, payload); return payload; } }
Example #12
Source File: DefaultPollableMessageSource.java From spring-cloud-stream with Apache License 2.0 | 4 votes |
/** * @param messageConverter instance of {@link SmartMessageConverter}. Can be null. */ public DefaultPollableMessageSource( @Nullable SmartMessageConverter messageConverter) { this.messageConverter = messageConverter; }
Example #13
Source File: SmartPayloadArgumentResolver.java From spring-cloud-stream with Apache License 2.0 | 4 votes |
@Override @Nullable public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception { Payload ann = parameter.getParameterAnnotation(Payload.class); if (ann != null && StringUtils.hasText(ann.expression())) { throw new IllegalStateException( "@Payload SpEL expressions not supported by this resolver"); } Object payload = message.getPayload(); if (isEmptyPayload(payload)) { if (ann == null || ann.required()) { String paramName = getParameterName(parameter); BindingResult bindingResult = new BeanPropertyBindingResult(payload, paramName); bindingResult.addError( new ObjectError(paramName, "Payload value must not be empty")); throw new MethodArgumentNotValidException(message, parameter, bindingResult); } else { return null; } } Class<?> targetClass = parameter.getParameterType(); Class<?> payloadClass = payload.getClass(); if (conversionNotRequired(payloadClass, targetClass)) { validate(message, parameter, payload); return payload; } else { if (this.messageConverter instanceof SmartMessageConverter) { SmartMessageConverter smartConverter = (SmartMessageConverter) this.messageConverter; payload = smartConverter.fromMessage(message, targetClass, parameter); } else { payload = this.messageConverter.fromMessage(message, targetClass); } if (payload == null) { throw new MessageConversionException(message, "Cannot convert from [" + payloadClass.getName() + "] to [" + targetClass.getName() + "] for " + message); } validate(message, parameter, payload); return payload; } }