我试图从 Spring RestController 返回一个 Observable,但没有成功。我的代码如下:
@RestController
public class HystrixCommentController {
@GetMapping(value = "/com1/{id}")
public Observable<Comment> getComment1(@PathVariable int id) {
return Observable.just(new Comment());
}
}
Run Code Online (Sandbox Code Playgroud)
在邮递员中运行请求时,我总是收到以下错误:
{
"timestamp": "2018-07-08T16:07:36.809+0000",
"status": 500,
"error": "Internal Server Error",
"message": "No converter found for return value of type: class rx.internal.util.ScalarSynchronousObservable",
"path": "/com1/1"
}
Run Code Online (Sandbox Code Playgroud)
SpringBoot 2 不也像 Mono/Flux 一样支持 RestController 中的 rx.Observable 吗?
我需要手动将 Observable 转换为 Mono/Flux 吗?
问候
注意:spring-boot-starter-webflux 包含在 pom 中
我试过这个:
@RestController
public class HystrixCommentController {
@GetMapping(value = "/com1/{id}", produces = "application/json")
public …Run Code Online (Sandbox Code Playgroud) blockingSingle我永远无法理解 RxJava 中和blockingFirston之间的区别Observables。任何帮助将不胜感激。
如何让我的视图模型在不使用回调并且实际不发送数据的情况下调用我的活动或片段中的函数。LiveData 用于将数据从视图模型发送到视图,但我没有数据。我只是想通知用户界面一些事情。这应该使用 RxJava 来完成还是太过分了?
我想发送带有多部分数据的数组对象。我尝试了很多方法但不起作用。我的贡献者参数问题。服务器说。列表中的其余项目需要contributor.0.id和contributor.0.role,依此类推。服务器读取到有一个贡献者数组并且它有项目,但由于某种原因他无法提取它。
有什么帮助吗?
@Multipart
@POST("project/create")
fun createProject(
@Header("Authorization") token: String,
@Part("title") title: String,
@Part img: MultipartBody.Part,
@Part("release_date") releaseDate: String,
@Part("contributors[]") contributors: MutableList<Contributor>
): Single<Response<String>>
Run Code Online (Sandbox Code Playgroud)
班级贡献者
class Contributor : Serializable{
@SerializedName("id")
@Expose
var id: Int = 0
@SerializedName("role")
@Expose
var role: String = ""
Run Code Online (Sandbox Code Playgroud)
}
我有一个来自 RxJava 的 Single,并希望继续使用来自 Kotlin 的延迟协程。如何做到这一点?
fun convert(data: rx.Single<String>): kotlinx.coroutines.Deferred<String> = ...
Run Code Online (Sandbox Code Playgroud)
我会对一些库(如果有的话?)以及自己做这件事感兴趣......到目前为止,我自己做了这个手工实现:
private fun waitForRxJavaResult(resultSingle: Single<String>): String? {
var resultReceived = false
var result: String? = null
resultSingle.subscribe({
result = it
resultReceived = true
}, {
resultReceived = true
if (!(it is NoSuchElementException))
it.printStackTrace()
})
while (!resultReceived)
Thread.sleep(20)
return result
}
Run Code Online (Sandbox Code Playgroud) 我们在处理 Android 网络请求时遇到问题,更准确地说是接收随机请求
\nSocketException:\njava.net.SocketException: socket is closed\n at com.android.org.conscrypt.ConscryptFileDescriptorSocket$SSLInputStream.read(ConscryptFileDescriptorSocket.java:551)\nRun Code Online (Sandbox Code Playgroud)\n请求似乎没有超时,异常抛出得很快,而且似乎只在发布模式下出现,我们猜测这可能与 ProGuard 有关。
\n我们\xe2\x80\x99使用Retrofit、Moshi和RxAndroid来处理我们的网络请求。有人遇到过这样的问题吗?
\n我们的依赖项及其版本:
\nimplementation \xe2\x80\x98io.reactivex.rxjava2:rxandroid:2.1.1\xe2\x80\x99\nimplementation \xe2\x80\x98io.reactivex.rxjava2:rxjava:2.2.9\xe2\x80\x99\nimplementation \xe2\x80\x98com.squareup.retrofit2:adapter-rxjava2:2.5.0\xe2\x80\x99\n//Moshi\ndef moshiVersion = \xe2\x80\x9c1.12.0\xe2\x80\x9d\nimplementation \xe2\x80\x9ccom.squareup.moshi:moshi:$moshiVersion\xe2\x80\x9d\nkapt \xe2\x80\x9ccom.squareup.moshi:moshi-kotlin-codegen:$moshiVersion\xe2\x80\x9d\nimplementation \xe2\x80\x9ccom.squareup.retrofit2:converter-moshi:2.9.0"\nimplementation \xe2\x80\x9ccom.squareup.moshi:moshi-kotlin:1.8.0\xe2\x80\x9d\n//Networking\nimplementation \xe2\x80\x98com.squareup.okhttp3:logging-interceptor:3.12.1\xe2\x80\x99\nimplementation \xe2\x80\x98com.squareup.retrofit2:retrofit:2.6.4\xe2\x80\x99\nimplementation \xe2\x80\x98com.squareup.retrofit2:converter-scalars:2.1.0\xe2\x80\x99\nRun Code Online (Sandbox Code Playgroud)\n我们所有的模型都带有 @Keep 注释:
\n@Keep\nclass ModelDTO(\n @field:Json(name = \xe2\x80\x9cfield1")\n var field1: String? = null,\n @field:Json(name = \xe2\x80\x9cfield2\xe2\x80\x9d)\n var field2: List<String>? = null\n)\nHttpClient + Retrofit:\nval client = OkHttpClient.Builder()\n .connectTimeout(300, TimeUnit.SECONDS)\n .writeTimeout(300, TimeUnit.SECONDS)\n .readTimeout(300, TimeUnit.SECONDS)\nclient.retryOnConnectionFailure(true)\n .addNetworkInterceptor { chain ->\n val request = chain.request().newBuilder().addHeader(\xe2\x80\x9cConnection\xe2\x80\x9d, \xe2\x80\x9cclose\xe2\x80\x9d).build()\n chain.proceed(request)\n }\nval …Run Code Online (Sandbox Code Playgroud) 我有一个问题,Reactive Extensions似乎特别适合.我有一个事件源,它在短时间内创建事件,中间有相对较长的空闲时间.我想分批对这些事件进行分组,其中(理想情况下)每一系列事件最终会在一批中结束.使用RxJava,有一个很好的方法吗?Observable.buffer(Observable)或Observable.buffer(Func0)似乎很有希望,但可以使用Observable.window()或Observable.groupByUntil().
我有以下代码,Observable使用该Observable.create(OnSubscribe)方法创建自定义:
public class Main {
public static void main(String[] args) {
Subscription subscription = Observable
.create(subscriber -> {
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
subscriber.onNext("tick! tack!");
}
};
timer.scheduleAtFixedRate(task, 0L, 1000L);
})
.subscribe(System.out::println);
new Scanner(System.in).nextLine();
System.err.println("finishing");
subscription.unsubscribe();
}
}
Run Code Online (Sandbox Code Playgroud)
Observable每秒使用一个计时器发出一个字符串.当用户按下回车键时,订阅将被取消.
但是,计时器仍然执行.我该如何取消计时器?我想某处必须有一个钩子,但我找不到它.
在.NET上,该create方法将返回一个IDisposable我可以作为我的实现来停止计时器.我不知道如何将它映射到RxJava,因为它的subscribe方法是void.
我是RxJava的新手,我正在努力弄清楚如何正确地关闭资源,特别是在处理多个订阅者时.
我有一个Observable<T>地方T是一些Closeable资源(例如说一个Android数据库Cursor.
我可能在observable上有多个订阅者.close()在每个订阅者完成处理之后我想要资源.换句话说,在新资源交付/发出后关闭旧资源,最后在最后一个订阅者取消订阅时关闭最后一个资源.
我试图使用我调用的自定义操作符使其工作AutoCloseOperator,并且它几乎正常工作,但不太正确.即我仍然是竞争条件和/或泄漏,例如资源没有关闭.
在RxJava中执行此操作的正确方法是什么?
说我有这个代码:
final AutoCloseOperator<MyResource> autoClose = new AutoCloseOperator<MyResource>();
Subject<MyResource, MyResource> subject = PublishSubject.create();
Observable<MyResource> o = subject.lift(autoClose);
Subscription s1 = o.subscribe(new Action1<MyResource>() {
public void call(MyResource myObj) {
System.out.println("s1 handling " + myObj);
}
});
subject.onNext(new MyResource(1));
subject.onNext(new MyResource(2)); // This should close Resource #1 after Resource #2 is delivered
Subscription s2 = o.subscribe(new Action1<MyResource>() {
public void call(MyResource myObj) {
System.out.println("s2 handling " …Run Code Online (Sandbox Code Playgroud) 我想链接一个连续的数据流并创建一个包含过去结果的结果列表.我可以用以下代码完成.有没有办法在rx链外没有变量?谢谢!
- [0]
- [0, 1]
- [0, 1, 2]
- [0, 1, 2, 3]
- [0, 1, 2, 3, 4]
- [0, 1, 2, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)
final List<Long> list = new ArrayList<>();
Observable
.interval(1, TimeUnit.SECONDS)
.subscribe(new Action1<Long>() {
@Override
public void call(Long number) {
list.add(number);
System.out.println("- " + list);
}
});
Thread.sleep(100000000L);
Run Code Online (Sandbox Code Playgroud)
↓↓↓↓↓↓↓↓↓
Observable
.interval(1, TimeUnit.SECONDS)
.addToPastResultList() // <--- something like this?
.subscribe(new Action1<List<Long>>() {
@Override
public void call(List<Long> list) {
System.out.println("- " + list);
}
});
Thread.sleep(100000000L);
Run Code Online (Sandbox Code Playgroud) rx-java ×10
android ×4
kotlin ×3
java ×2
retrofit ×2
rx-android ×2
coroutine ×1
hystrix ×1
observable ×1
proguard ×1
reactive ×1
spring ×1
spring-boot ×1
spring-mvc ×1