简体   繁体   English

Micronaut地图为空Flowable至404

[英]Micronaut map empty Flowable to 404

Given: I have a service which produces a Flowable<T> . 鉴于:我有一个产生Flowable<T> This Flowable<T> can be empty. Flowable<T>可以为空。

I have a controller, which looks similar to this: 我有一个控制器,看起来与此类似:

@Controller("/api}")
class ApiController constructor( private val myService: MyService) {
    @Get("/")
    @Produces(MediaType.APPLICATION_JSON)
    fun getSomething(): Flowable<T> {
        return myService.get()
    }
}

What I want to achieve: when the flowable is empty -> throw a HttpStatusException(404) . 我想要实现的是:当flowable为空时->抛出HttpStatusException(404)

Otherwise return the flowable with the data inside. 否则,返回其中包含数据的flowable。

What I already tried 我已经尝试过的

I tried different combinations of the following RxJava Operators: 我尝试了以下RxJava运算符的不同组合:

  1. doOnError doOnError
  2. onErrorResumeNext onErrorResumeNext
  3. onErrorReturn onErrorReturn
  4. switchIfEmpty switchIfEmpty
  5. ... ...

What I experienced 我经历了什么

None of the options produced a 404 in the Browser/Postman. 没有一个选项在浏览器/邮递员中产生404。

A couple of options are just doing "nothing". 几个选项只是“无所事事”。 Which means, that the page is not loading in the browser. 这意味着该页面未在浏览器中加载。

Other options are creating "OK" (200) responses with empty bodies. 其他选项正在创建带有空主体的“确定”(200)响应。

And some are creating a CompositeException ... 有些正在创建CompositeException ...

Does someone have a hint for me? 有人对我有提示吗?

Update : as suggested: 更新 :如建议:

@Controller("/api")
class HelloController {
    @Get("/")
    @Produces(MediaType.APPLICATION_JSON)
    fun get(): Flowable<String> {
        return Flowable.empty<String>()
                .switchIfEmpty {
            it.onError(HttpStatusException(HttpStatus.NOT_FOUND,""))
        }
    }
}

This produces the following, when I call it with firefox: 当我用firefox调用它时,将产生以下内容:

HttpResponseStatus: 200
HttpContent: [
Yes, the closing bracet is missing!

A possible Solution is, to use Maybe instead of Flowable. 一个可能的解决方案是使用Maybe代替Flowable。

@Controller("/api")
class HelloController {
    @Get("/")
    @Produces(MediaType.APPLICATION_JSON)
    fun get(): Maybe<String> {
        return Flowable.empty<String>()
            .toList()
            .flatMapMaybe { x ->
                if (x.size == 0)
                    Maybe.empty<String>()
                else
                    Maybe.just(x)
            }           
        }
    }
}

It is not the best solution, but a working one. 这不是最佳解决方案,而是可行的解决方案。

I don't know Micronaut, but I think this may be what you want: 我不了解Micronaut,但我想这可能是您想要的:

Flowable.empty<Int>()
    .switchIfEmpty { it.onError(Exception()) } // or HttpStatusException(404) in your case
    .subscribe({
        println(it)
    }, {
        it.printStackTrace()
    })

The Flowable is empty, and what you get downstream is the Exception created inside switchIfEmpty . Flowable为空,而下游则是在switchIfEmpty内部创建的Exception Note that you have to call it.onError inside switchIfEmpty . 请注意,您必须在it.onError内部调用switchIfEmpty

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM