Spring RestTemplate处理状态为NO_CONTENT的响应时的行为 [英] Spring RestTemplate Behavior when handling responses with a status of NO_CONTENT
问题描述
好的,我有一个名为NamedSystems的类,它的唯一字段是一组NamedSystem。
Okay, I have a class NamedSystems, that has as its only field a Set of NamedSystem.
我有一种按特定标准查找NamedSystems的方法。那不是很重要。当它得到结果时,一切正常。但是,当它找不到任何东西,从而返回null(或空 - 我已尝试过两种方式)设置时,我就会遇到问题。让我解释一下。
I have a method to find NamedSystems by certain criteria. That's not really important. When it gets results, everything works fine. However, when it can't find anything, and thus returns a null (or empty -- I've tried both ways) set, I get problems. Let me explain.
我正在使用Spring RestTemplate类,我在单元测试中进行这样的调用:
I'm using the Spring RestTemplate class and I'm making a call like this in a unit test:
ResponseEntity<?> responseEntity = template.exchange(BASE_SERVICE_URL + "?
alias={aliasValue}&aliasAuthority={aliasAssigningAuthority}",
HttpMethod.GET, makeHttpEntity("xml"), NamedSystems.class,
alias1.getAlias(), alias1.getAuthority());
现在,因为这通常会返回200,但我想返回204,我有一个我的服务中的拦截器,用于确定ModelAndView是否为NamedSystem,以及其set是否为null。如果是,我然后将状态代码设置为NO_CONTENT(204)。
Now, since this would normally return a 200, but I want to return a 204, I have an interceptor in my service that determines if a ModelAndView is a NamedSystem and if its set is null. If so, I then the set the status code to NO_CONTENT (204).
当我运行junit测试时,我收到此错误:
When I run my junit test, I get this error:
org.springframework.web.client.RestClientException: Cannot extract response: no Content-Type found
设置NO_CONTENT的状态似乎擦除了内容类型字段(当我考虑它时这确实有意义)。那么为什么还要看呢?
Setting the status to NO_CONTENT seems to wipe the content-type field (which does make sense when I think about it). So why is it even looking at it?
Spring的HttpMessageConverterExtractor extractData方法:
Spring's HttpMessageConverterExtractor extractData method:
public T extractData(ClientHttpResponse response) throws IOException {
MediaType contentType = response.getHeaders().getContentType();
if (contentType == null) {
throw new RestClientException("Cannot extract response: no Content-Type found");
}
for (HttpMessageConverter messageConverter : messageConverters) {
if (messageConverter.canRead(responseType, contentType)) {
if (logger.isDebugEnabled()) {
logger.debug("Reading [" + responseType.getName() + "] as \"" + contentType
+"\" using [" + messageConverter + "]");
}
return (T) messageConverter.read(this.responseType, response);
}
}
throw new RestClientException(
"Could not extract response: no suitable HttpMessageConverter found for response type [" +
this.responseType.getName() + "] and content type [" + contentType + "]");
}
稍微上链以找出提取器设置的位置,I来到我在测试中使用的RestTemplate的exchange()方法:
Going up the chain a bit to find out where that Extractor is set, I come to RestTemplate's exchange() method that I used in the test:
public <T> ResponseEntity<T> exchange(String url, HttpMethod method,
HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) throws RestClientException {
HttpEntityRequestCallback requestCallback = new HttpEntityRequestCallback(requestEntity, responseType);
ResponseEntityResponseExtractor<T> responseExtractor = new ResponseEntityResponseExtractor<T>(responseType);
return execute(url, method, requestCallback, responseExtractor, uriVariables);
}
因此,由于提供的响应类型,它正在尝试转换为什么来自交换电话。如果我将responseType从NamedSystems.class更改为null,它将按预期工作。它不会尝试转换任何东西。如果我试图将状态代码设置为404,它也可以正常执行。
So, it's trying to convert what amounts to nothing because of the supplied response type from the exchange call. If I change the responseType from NamedSystems.class to null, it works as expected. It doesn't try to convert anything. If I had tried to set the status code to 404, it also executes fine.
我是误入歧途,还是看起来像RestTemplate中的一个缺陷?当然,我现在正在使用junit所以我知道会发生什么,但如果有人使用RestTemplate来调用它并且不知道服务调用的结果,他们自然会将NamedSystems作为响应类型。但是,如果他们尝试了没有元素的标准搜索,他们会有这个令人讨厌的错误。
Am I misguided, or does this seem like a flaw in RestTemplate? Sure, I'm using a junit right now so I know what's going to happen, but if someone is using RestTemplate to call this and doesn't know the outcome of the service call, they would naturally have NamedSystems as a response type. However, if they tried a criteria search that came up with no elements, they'd have this nasty error.
有没有办法解决这个问题而不会覆盖任何RestTemplate的东西?我是否错误地查看了这种情况?请帮忙,因为我有点困惑。
Is there a way around this without overriding any RestTemplate stuff? Am I viewing this situation incorrectly? Please help as I'm a bit baffled.
推荐答案
现在应该在Spring 3.1 RC1中修复。
This should now be fixed in Spring 3.1 RC1.
https://jira.spring.io/browse/SPR-7911
这篇关于Spring RestTemplate处理状态为NO_CONTENT的响应时的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!