org.springframework.messaging.converter.MessageConversionException Java Examples
The following examples show how to use
org.springframework.messaging.converter.MessageConversionException.
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: DefaultStompSession.java From spring-analysis-note with MIT License | 6 votes |
@SuppressWarnings("unchecked") private Message<byte[]> createMessage(StompHeaderAccessor accessor, @Nullable Object payload) { accessor.updateSimpMessageHeadersFromStompHeaders(); Message<byte[]> message; if (payload == null) { message = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders()); } else if (payload instanceof byte[]) { message = MessageBuilder.createMessage((byte[]) payload, accessor.getMessageHeaders()); } else { message = (Message<byte[]>) getMessageConverter().toMessage(payload, accessor.getMessageHeaders()); accessor.updateStompHeadersFromSimpMessageHeaders(); if (message == null) { throw new MessageConversionException("Unable to convert payload with type='" + payload.getClass().getName() + "', contentType='" + accessor.getContentType() + "', converter=[" + getMessageConverter() + "]"); } } return message; }
Example #2
Source File: DefaultStompSession.java From spring4-understanding with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") private Message<byte[]> createMessage(StompHeaderAccessor accessor, Object payload) { accessor.updateSimpMessageHeadersFromStompHeaders(); Message<byte[]> message; if (payload == null) { message = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders()); } else if (payload instanceof byte[]) { message = MessageBuilder.createMessage((byte[]) payload, accessor.getMessageHeaders()); } else { message = (Message<byte[]>) getMessageConverter().toMessage(payload, accessor.getMessageHeaders()); accessor.updateStompHeadersFromSimpMessageHeaders(); if (message == null) { throw new MessageConversionException("Unable to convert payload with type='" + payload.getClass().getName() + "', contentType='" + accessor.getContentType() + "', converter=[" + getMessageConverter() + "]"); } } return message; }
Example #3
Source File: DefaultStompSessionTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void handleErrorFrameWithConversionException() { StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.ERROR); accessor.setContentType(MimeTypeUtils.APPLICATION_JSON); accessor.addNativeHeader("foo", "bar"); accessor.setLeaveMutable(true); byte[] payload = "{'foo':'bar'}".getBytes(StandardCharsets.UTF_8); StompHeaders stompHeaders = StompHeaders.readOnlyStompHeaders(accessor.getNativeHeaders()); when(this.sessionHandler.getPayloadType(stompHeaders)).thenReturn(Map.class); this.session.handleMessage(MessageBuilder.createMessage(payload, accessor.getMessageHeaders())); verify(this.sessionHandler).getPayloadType(stompHeaders); verify(this.sessionHandler).handleException(same(this.session), same(StompCommand.ERROR), eq(stompHeaders), same(payload), any(MessageConversionException.class)); verifyNoMoreInteractions(this.sessionHandler); }
Example #4
Source File: MessageMethodArgumentResolver.java From java-technology-stack with MIT License | 6 votes |
@Override public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception { Class<?> targetMessageType = parameter.getParameterType(); Class<?> targetPayloadType = getPayloadType(parameter); if (!targetMessageType.isAssignableFrom(message.getClass())) { throw new MethodArgumentTypeMismatchException(message, parameter, "Actual message type '" + ClassUtils.getDescriptiveType(message) + "' does not match expected type '" + ClassUtils.getQualifiedName(targetMessageType) + "'"); } Object payload = message.getPayload(); if (targetPayloadType.isInstance(payload)) { return message; } if (isEmptyPayload(payload)) { throw new MessageConversionException(message, "Cannot convert from actual payload type '" + ClassUtils.getDescriptiveType(payload) + "' to expected payload type '" + ClassUtils.getQualifiedName(targetPayloadType) + "' when payload is empty"); } payload = convertPayload(message, parameter, targetPayloadType); return MessageBuilder.createMessage(payload, message.getHeaders()); }
Example #5
Source File: DefaultStompSession.java From java-technology-stack with MIT License | 6 votes |
private void invokeHandler(StompFrameHandler handler, Message<byte[]> message, StompHeaders headers) { if (message.getPayload().length == 0) { handler.handleFrame(headers, null); return; } Type payloadType = handler.getPayloadType(headers); Class<?> resolvedType = ResolvableType.forType(payloadType).resolve(); if (resolvedType == null) { throw new MessageConversionException("Unresolvable payload type [" + payloadType + "] from handler type [" + handler.getClass() + "]"); } Object object = getMessageConverter().fromMessage(message, resolvedType); if (object == null) { throw new MessageConversionException("No suitable converter for payload type [" + payloadType + "] from handler type [" + handler.getClass() + "]"); } handler.handleFrame(headers, object); }
Example #6
Source File: DefaultStompSession.java From java-technology-stack with MIT License | 6 votes |
@SuppressWarnings("unchecked") private Message<byte[]> createMessage(StompHeaderAccessor accessor, @Nullable Object payload) { accessor.updateSimpMessageHeadersFromStompHeaders(); Message<byte[]> message; if (payload == null) { message = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders()); } else if (payload instanceof byte[]) { message = MessageBuilder.createMessage((byte[]) payload, accessor.getMessageHeaders()); } else { message = (Message<byte[]>) getMessageConverter().toMessage(payload, accessor.getMessageHeaders()); accessor.updateStompHeadersFromSimpMessageHeaders(); if (message == null) { throw new MessageConversionException("Unable to convert payload with type='" + payload.getClass().getName() + "', contentType='" + accessor.getContentType() + "', converter=[" + getMessageConverter() + "]"); } } return message; }
Example #7
Source File: DefaultStompSessionTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void handleErrorFrameWithConversionException() throws Exception { StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.ERROR); accessor.setContentType(MimeTypeUtils.APPLICATION_JSON); accessor.addNativeHeader("foo", "bar"); accessor.setLeaveMutable(true); byte[] payload = "{'foo':'bar'}".getBytes(UTF_8); StompHeaders stompHeaders = StompHeaders.readOnlyStompHeaders(accessor.getNativeHeaders()); when(this.sessionHandler.getPayloadType(stompHeaders)).thenReturn(Map.class); this.session.handleMessage(MessageBuilder.createMessage(payload, accessor.getMessageHeaders())); verify(this.sessionHandler).getPayloadType(stompHeaders); verify(this.sessionHandler).handleException(same(this.session), same(StompCommand.ERROR), eq(stompHeaders), same(payload), any(MessageConversionException.class)); verifyNoMoreInteractions(this.sessionHandler); }
Example #8
Source File: DefaultStompSessionTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void handleErrorFrameWithConversionException() { StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.ERROR); accessor.setContentType(MimeTypeUtils.APPLICATION_JSON); accessor.addNativeHeader("foo", "bar"); accessor.setLeaveMutable(true); byte[] payload = "{'foo':'bar'}".getBytes(StandardCharsets.UTF_8); StompHeaders stompHeaders = StompHeaders.readOnlyStompHeaders(accessor.getNativeHeaders()); given(this.sessionHandler.getPayloadType(stompHeaders)).willReturn(Map.class); this.session.handleMessage(MessageBuilder.createMessage(payload, accessor.getMessageHeaders())); verify(this.sessionHandler).getPayloadType(stompHeaders); verify(this.sessionHandler).handleException(same(this.session), same(StompCommand.ERROR), eq(stompHeaders), same(payload), any(MessageConversionException.class)); verifyNoMoreInteractions(this.sessionHandler); }
Example #9
Source File: DefaultPollableMessageSource.java From spring-cloud-stream with Apache License 2.0 | 6 votes |
/** * Receives Message from the source and converts its payload to a provided type. Can * return null * @param type type reference * @return received message */ private Message<?> receive(ParameterizedTypeReference<?> type) { Message<?> message = this.source.receive(); if (message != null && type != null && this.messageConverter != null) { Class<?> targetType = type == null ? Object.class : type.getType() instanceof Class ? (Class<?>) type.getType() : Object.class; Object payload = this.messageConverter.fromMessage(message, targetType, type); if (payload == null) { throw new MessageConversionException(message, "No converter could convert Message"); } message = MessageBuilder.withPayload(payload) .copyHeaders(message.getHeaders()).build(); } return message; }
Example #10
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 #11
Source File: MessageMethodArgumentResolver.java From spring-analysis-note with MIT License | 6 votes |
@Override public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception { Class<?> targetMessageType = parameter.getParameterType(); Class<?> targetPayloadType = getPayloadType(parameter, message); if (!targetMessageType.isAssignableFrom(message.getClass())) { throw new MethodArgumentTypeMismatchException(message, parameter, "Actual message type '" + ClassUtils.getDescriptiveType(message) + "' does not match expected type '" + ClassUtils.getQualifiedName(targetMessageType) + "'"); } Object payload = message.getPayload(); if (targetPayloadType.isInstance(payload)) { return message; } if (isEmptyPayload(payload)) { throw new MessageConversionException(message, "Cannot convert from actual payload type '" + ClassUtils.getDescriptiveType(payload) + "' to expected payload type '" + ClassUtils.getQualifiedName(targetPayloadType) + "' when payload is empty"); } payload = convertPayload(message, parameter, targetPayloadType); return MessageBuilder.createMessage(payload, message.getHeaders()); }
Example #12
Source File: DefaultStompSession.java From spring-analysis-note with MIT License | 6 votes |
private void invokeHandler(StompFrameHandler handler, Message<byte[]> message, StompHeaders headers) { if (message.getPayload().length == 0) { handler.handleFrame(headers, null); return; } Type payloadType = handler.getPayloadType(headers); Class<?> resolvedType = ResolvableType.forType(payloadType).resolve(); if (resolvedType == null) { throw new MessageConversionException("Unresolvable payload type [" + payloadType + "] from handler type [" + handler.getClass() + "]"); } Object object = getMessageConverter().fromMessage(message, resolvedType); if (object == null) { throw new MessageConversionException("No suitable converter for payload type [" + payloadType + "] from handler type [" + handler.getClass() + "]"); } handler.handleFrame(headers, object); }
Example #13
Source File: BusProtoStuffMessageConverter.java From hawkbit with Eclipse Public License 1.0 | 5 votes |
private static Object readContent(final EventType eventType, final byte[] content) { final Class<?> targetClass = eventType.getTargetClass(); if (targetClass == null) { LOG.error("Cannot read clazz header for given EventType value {}, missing mapping", eventType.getValue()); throw new MessageConversionException("Missing mapping of EventType for value " + eventType.getValue()); } @SuppressWarnings("unchecked") final Schema<Object> schema = (Schema<Object>) RuntimeSchema.getSchema(targetClass); final Object deserializeEvent = schema.newMessage(); ProtobufIOUtil.mergeFrom(content, deserializeEvent, schema); return deserializeEvent; }
Example #14
Source File: MethodJmsListenerEndpointTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void invalidPayloadType() throws JMSException { MessagingMessageListenerAdapter listener = createDefaultInstance(Integer.class); Session session = mock(Session.class); thrown.expect(ListenerExecutionFailedException.class); thrown.expectCause(Matchers.isA(MessageConversionException.class)); thrown.expectMessage(getDefaultListenerMethod(Integer.class).toGenericString()); // ref to method listener.onMessage(createSimpleJmsTextMessage("test"), session); // test is not a valid integer }
Example #15
Source File: ContentTypeTckTests.java From spring-cloud-stream with Apache License 2.0 | 5 votes |
@Test @Ignore public void _toStringDefaultContentTypePropertyUnknownContentType() { ApplicationContext context = new SpringApplicationBuilder( StringToStringStreamListener.class).web(WebApplicationType.NONE).run( "--spring.cloud.stream.default.contentType=foo/bar", "--spring.jmx.enabled=false"); InputDestination source = context.getBean(InputDestination.class); TestChannelBinder binder = context.getBean(TestChannelBinder.class); String jsonPayload = "{\"name\":\"oleg\"}"; source.send(new GenericMessage<>(jsonPayload.getBytes())); assertThat( binder.getLastError().getPayload() instanceof MessageConversionException) .isTrue(); }
Example #16
Source File: JmsMessagingTemplate.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Override public javax.jms.Message createMessage(Session session) throws JMSException { try { return this.messageConverter.toMessage(this.message, session); } catch (Exception ex) { throw new MessageConversionException("Could not convert '" + this.message + "'", ex); } }
Example #17
Source File: MessageSendingTemplateTests.java From java-technology-stack with MIT License | 5 votes |
@Test(expected = MessageConversionException.class) public void convertAndSendNoMatchingConverter() { MessageConverter converter = new CompositeMessageConverter( Arrays.<MessageConverter>asList(new MappingJackson2MessageConverter())); this.template.setMessageConverter(converter); this.headers.put(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_XML); this.template.convertAndSend("home", "payload", new MessageHeaders(this.headers)); }
Example #18
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 #19
Source File: JmsMessagingTemplate.java From spring4-understanding with Apache License 2.0 | 5 votes |
protected MessagingException convertJmsException(JmsException ex) { if (ex instanceof org.springframework.jms.support.destination.DestinationResolutionException || ex instanceof InvalidDestinationException) { return new DestinationResolutionException(ex.getMessage(), ex); } if (ex instanceof org.springframework.jms.support.converter.MessageConversionException) { return new MessageConversionException(ex.getMessage(), ex); } // Fallback return new MessagingException(ex.getMessage(), ex); }
Example #20
Source File: DefaultStompSession.java From spring4-understanding with Apache License 2.0 | 5 votes |
private void invokeHandler(StompFrameHandler handler, Message<byte[]> message, StompHeaders stompHeaders) { if (message.getPayload().length == 0) { handler.handleFrame(stompHeaders, null); return; } Type type = handler.getPayloadType(stompHeaders); Class<?> payloadType = ResolvableType.forType(type).getRawClass(); Object object = getMessageConverter().fromMessage(message, payloadType); if (object == null) { throw new MessageConversionException("No suitable converter, payloadType=" + payloadType + ", handlerType=" + handler.getClass()); } handler.handleFrame(stompHeaders, object); }
Example #21
Source File: TensorflowProcessorConfiguration.java From tensorflow with Apache License 2.0 | 5 votes |
@Bean @SuppressWarnings("unchecked") public TensorflowInputConverter tensorflowInputConverter() { return (input, processorContext) -> { if (input instanceof Map) { return (Map<String, Object>) input; } throw new MessageConversionException("Unsupported input format: " + input); }; }
Example #22
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 #23
Source File: MethodJmsListenerEndpointTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void invalidMessagePayloadType() throws JMSException { MessagingMessageListenerAdapter listener = createDefaultInstance(Message.class); Session session = mock(Session.class); this.thrown.expect(ListenerExecutionFailedException.class); this.thrown.expectCause(Matchers.isA(MessageConversionException.class)); listener.onMessage(createSimpleJmsTextMessage("test"), session); // Message<String> as Message<Integer> }
Example #24
Source File: MethodJmsListenerEndpointTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void invalidPayloadType() throws JMSException { MessagingMessageListenerAdapter listener = createDefaultInstance(Integer.class); Session session = mock(Session.class); this.thrown.expect(ListenerExecutionFailedException.class); this.thrown.expectCause(Matchers.isA(MessageConversionException.class)); this.thrown.expectMessage(getDefaultListenerMethod(Integer.class).toGenericString()); // ref to method listener.onMessage(createSimpleJmsTextMessage("test"), session); // test is not a valid integer }
Example #25
Source File: JmsMessagingTemplate.java From java-technology-stack with MIT License | 5 votes |
@Override public javax.jms.Message createMessage(Session session) throws JMSException { try { return this.messageConverter.toMessage(this.message, session); } catch (Exception ex) { throw new MessageConversionException("Could not convert '" + this.message + "'", ex); } }
Example #26
Source File: JmsMessagingTemplate.java From java-technology-stack with MIT License | 5 votes |
protected MessagingException convertJmsException(JmsException ex) { if (ex instanceof org.springframework.jms.support.destination.DestinationResolutionException || ex instanceof InvalidDestinationException) { return new DestinationResolutionException(ex.getMessage(), ex); } if (ex instanceof org.springframework.jms.support.converter.MessageConversionException) { return new MessageConversionException(ex.getMessage(), ex); } // Fallback return new MessagingException(ex.getMessage(), ex); }
Example #27
Source File: JmsMessagingTemplate.java From java-technology-stack with MIT License | 5 votes |
@Nullable protected Message<?> convertJmsMessage(@Nullable javax.jms.Message message) { if (message == null) { return null; } try { return (Message<?>) getJmsMessageConverter().fromMessage(message); } catch (Exception ex) { throw new MessageConversionException("Could not convert '" + message + "'", ex); } }
Example #28
Source File: MessageReceivingTemplateTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void receiveAndConvertNoConverter() { Message<?> expected = new GenericMessage<>("payload"); this.template.setDefaultDestination("home"); this.template.setReceiveMessage(expected); this.template.setMessageConverter(new GenericMessageConverter()); try { this.template.receiveAndConvert(Writer.class); } catch (MessageConversionException ex) { assertTrue("Invalid exception message '" + ex.getMessage() + "'", ex.getMessage().contains("payload")); assertSame(expected, ex.getFailedMessage()); } }
Example #29
Source File: MessageReceivingTemplateTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void receiveAndConvertFailed() { Message<?> expected = new GenericMessage<>("not a number test"); this.template.setReceiveMessage(expected); this.template.setMessageConverter(new GenericMessageConverter()); thrown.expect(MessageConversionException.class); thrown.expectCause(isA(ConversionFailedException.class)); this.template.receiveAndConvert("somewhere", Integer.class); }
Example #30
Source File: SmartMessageMethodArgumentResolver.java From spring-cloud-stream with Apache License 2.0 | 5 votes |
@Override public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception { Class<?> targetMessageType = parameter.getParameterType(); Class<?> targetPayloadType = getPayloadType(parameter); if (!targetMessageType.isAssignableFrom(message.getClass())) { throw new MethodArgumentTypeMismatchException(message, parameter, "Actual message type '" + ClassUtils.getDescriptiveType(message) + "' does not match expected type '" + ClassUtils.getQualifiedName(targetMessageType) + "'"); } Class<?> payloadClass = message.getPayload().getClass(); if (message instanceof ErrorMessage || conversionNotRequired(payloadClass, targetPayloadType)) { return message; } Object payload = message.getPayload(); if (isEmptyPayload(payload)) { throw new MessageConversionException(message, "Cannot convert from actual payload type '" + ClassUtils.getDescriptiveType(payload) + "' to expected payload type '" + ClassUtils.getQualifiedName(targetPayloadType) + "' when payload is empty"); } payload = convertPayload(message, parameter, targetPayloadType); return MessageBuilder.createMessage(payload, message.getHeaders()); }