【发布时间】:2017-07-31 12:34:54
【问题描述】:
我想知道 RxJava 和 spring REST API 最好的是什么?
我有一个简单的 REST 服务,并且在存储库中,如果出现错误,我想将特定的自定义错误传播给客户端。但我不确定如何使用 RxJava 映射不同的自定义异常。
这是对后端的调用:
private Single<Customer> findCustomerById(long customerId) {
return Single.fromCallable(() -> getRestTemplate().getForObject(
MyBackendService.SEARCH_CUSTOMER_BY_ID.getUrl(),
Customer.class, customerId))
.onErrorResumeNext(ex -> Single.error(new BackendException(ex)));
}
我的例外:
public class BackendException extends Exception {
public BackendException(String message) {
super(message);
}
public BackendException(Throwable cause) {
super(cause);
}
所以问题是如何用 RxJava 这个BackendException 映射/传播到让我们说NotFound (404) 或InternalServerError (500)?
【问题讨论】: