这没关系:
@RequestMapping(value = "/foos/{id}", method = RequestMethod.GET)
public ResponseEntity<Foo> _findOne(@PathVariable("id") Integer id) {
Foo foo = findOne(id);
if (foo == null) {
return new ResponseEntity<Foo>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<Foo>(foo, HttpStatus.OK);
}
由于没有项目,尝试发生错误时异步版本失败)
服务器异常是:
java.lang.IllegalArgumentException: No converter found for return value of type: class org.springframework.http.ResponseEntity$DefaultBuilder
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:178) ~[spring-webmvc-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:153) ~[spring-webmvc-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:165) ~[spring-webmvc-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
这是DeferredResult版本
@RequestMapping(value = "/foos/{id}", method = RequestMethod.GET)
public DeferredResult<ResponseEntity<Foo>> _findOne(@PathVariable("id") Integer id) {
final DeferredResult<ResponseEntity<Foo>> deferred = new DeferredResult<>();
findOne(id).singleOrDefault(null).timeout(1, TimeUnit.SECONDS)
.subscribe(item -> {
if (item == null) {
deferred.setErrorResult(ResponseEntity.status(HttpStatus.NOT_FOUND));
} else {
deferred.setResult(ResponseEntity.ok(item));
}
}, t -> {
deferred.setErrorResult(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR));
}
);
return deferred;
}
我已阅读Spring Boot Application: No converter found for return value of type但我认为我的情况有所不同,因为只有当我返回NOT_FOUND
时答案 0 :(得分:0)
使用this SO answer和this example 我看到deferred.setErrorResult 需要一个异常所以我现在有:
deferred.setErrorResult(new FooNotFoundException());
其中:
@ResponseStatus(HttpStatus.EXPECTATION_FAILED)
static class FooNotFoundException extends Exception {
}
保留此问题而不是将其删除为java.lang.IllegalArgumentException
而不是链接的SO问题,并且非常令人困惑。