Raise MessageConversionException in @Payload resolver

If a payload is present but conversion returns null (meaning no
converter knows how to convert), raise a MessageConversionException
that provides information about the type we were trying to convert
to and the message itself whose headers (namely content-type) contain
crucial information required to debug the problem.

Issue: SPR-11577
This commit is contained in:
Rossen Stoyanchev
2014-03-21 08:52:31 -04:00
parent 26309838ba
commit 929e9ca401
2 changed files with 40 additions and 33 deletions

View File

@@ -29,6 +29,7 @@ import org.junit.rules.ExpectedException;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.core.MethodParameter;
import org.springframework.messaging.Message;
import org.springframework.messaging.converter.MessageConversionException;
import org.springframework.messaging.converter.StringMessageConverter;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.support.MessageBuilder;
@@ -129,9 +130,8 @@ public class PayloadArgumentResolverTests {
public void resolveNonConvertibleParam() throws Exception {
Message<?> notEmptyMessage = MessageBuilder.withPayload(123).build();
// Could not convert from int to Locale so will be "empty" after conversion
thrown.expect(MethodArgumentNotValidException.class);
thrown.expectMessage(Locale.class.getName()); // reference to the type that could not be converted
thrown.expect(MessageConversionException.class);
thrown.expectMessage("No converter found");
this.resolver.resolveArgument(this.paramAnnotatedRequired, notEmptyMessage);
}
@@ -183,8 +183,9 @@ public class PayloadArgumentResolverTests {
// See testValidator()
Message<?> message = MessageBuilder.withPayload("invalidValue".getBytes()).build();
assertEquals("invalidValue",
this.resolver.resolveArgument(paramValidatedNotAnnotated, message));
thrown.expect(MethodArgumentNotValidException.class);
thrown.expectMessage("invalid value");
assertEquals("invalidValue", this.resolver.resolveArgument(this.paramValidatedNotAnnotated, message));
}
private Validator testValidator() {