What happens when RestTemplate gets a null response from spring?
java, spring, spring-mvc
Solution
When you annotate your handler method with `@ResponseBody`, Spring uses `RequestResponseBodyMethodProcessor` to generate the response body. It does this in its `handleReturnValue` method which is implemented as
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType,
ModelAndViewContainer mavContainer, NativeWebRequest webRequest)
throws IOException, HttpMediaTypeNotAcceptableException {
mavContainer.setRequestHandled(true);
if (returnValue != null) {
writeWithMessageConverters(returnValue, returnType, webRequest);
}
}
So if the returned value is `null`, nothing will be written to the body.
The `RestTemplate` will therefore try to deserialize a `MyDto` from an empty `String`. `RestTemplate` uses an `HttpMessageConvertExtractor` to deserialize the response. The `extractData` method is implemented as
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public T extractData(ClientHttpResponse response) throws IOException {
if (!hasMessageBody(response)) {
return null;
}
MediaType contentType = getContentType(response);
for (HttpMessageConverter<?> messageConverter : this.messageConverters) {
if (messageConverter instanceof GenericHttpMessageConverter) {
GenericHttpMessageConverter<?> genericMessageConverter = (GenericHttpMessageConverter<?>) messageConverter;
if (genericMessageConverter.canRead(this.responseType, null, contentType)) {
if (logger.isDebugEnabled()) {
logger.debug("Reading [" + this.responseType + "] as \"" +
contentType + "\" using [" + messageConverter + "]");
}
return (T) genericMessageConverter.read(this.responseType, null, response);
}
}
if (this.responseClass != null) {
if (messageConverter.canRead(this.responseClass, contentType)) {
if (logger.isDebugEnabled()) {
logger.debug("Reading [" + this.responseClass.getName() + "] as \"" +
contentType + "\" using [" + messageConverter + "]");
}
return (T) messageConverter.read((Class) this.responseClass, response);
}
}
}
throw new RestClientException(
"Could not extract response: no suitable HttpMessageConverter found for response type [" +
this.responseType + "] and content type [" + contentType + "]");
}
In other words, if there is no body, it returns `null`.
So your `MyDto` object here
MyDto obj = restTemplate.getForObject(url, MyDto.class);
will be `null`.
Problem
I would like to know what's going on when a spring server returns a null object (instead of an "empty" object), on the client side. Does it raise an exception ? or simply mark it as `null` too ? Client code : ``` // Does it throw an exception or will I have obj==null or obj "empty" ? MyDto obj = restTemplate.getForObject(url, MyDto.class); ``` Server code : ``` @RequestMapping(value = "/urlpath", method = RequestMethod.GET) @ResponseBody public MyDto getObj() { return null; } ``` I have simplified the code of course. The reason why I am asking here is that it takes a long time to turn on my debug server and to create a simple basic environment with all this with pom.xml etc (I am new to spring).