Browse Source

Relax generic type detection for ResponseEntity

Before this change the getHttpEntityType method in
HttpEntityMethodProcessor raised an ISE if the generic type cannot be
detected. That made sense for resolving a controller method argument
where the target body type is crucial. However for a return value
the generic type should not be required since we either have an
actual body or no body at all in which case it doesn't even matter.

This change relaxes the checks and defaults to Object.class for the
ResponseEntity generic type on the return value side.

Issue: SPR-14799
pull/1202/head
Rossen Stoyanchev 8 years ago
parent
commit
71201e1a43
  1. 10
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessor.java

10
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessor.java

@ -121,6 +121,10 @@ public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodPro
ServletServerHttpRequest inputMessage = createInputMessage(webRequest); ServletServerHttpRequest inputMessage = createInputMessage(webRequest);
Type paramType = getHttpEntityType(parameter); Type paramType = getHttpEntityType(parameter);
if (paramType == null) {
throw new IllegalArgumentException("HttpEntity parameter '" + parameter.getParameterName() +
"' in method " + parameter.getMethod() + " is not parameterized");
}
Object body = readWithMessageConverters(webRequest, parameter, paramType); Object body = readWithMessageConverters(webRequest, parameter, paramType);
if (RequestEntity.class == parameter.getParameterType()) { if (RequestEntity.class == parameter.getParameterType()) {
@ -146,8 +150,9 @@ public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodPro
else if (parameterType instanceof Class) { else if (parameterType instanceof Class) {
return Object.class; return Object.class;
} }
throw new IllegalArgumentException("HttpEntity parameter '" + parameter.getParameterName() + else {
"' in method " + parameter.getMethod() + " is not parameterized"); return null;
}
} }
@Override @Override
@ -243,6 +248,7 @@ public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodPro
} }
else { else {
Type type = getHttpEntityType(returnType); Type type = getHttpEntityType(returnType);
type = (type != null ? type : Object.class);
return ResolvableType.forMethodParameter(returnType, type).resolve(Object.class); return ResolvableType.forMethodParameter(returnType, type).resolve(Object.class);
} }
} }

Loading…
Cancel
Save