如何在 Micronaut 中获取反应流?
How to get reactive streams in Micronaut?
在SpringBoot中,对于Webflux项目,我们通过在HTTP请求中发送一个header——“Accept:application/stream+json”来请求数据流。
如果我们发送“接受:application/json”,我们会得到一个有效的 Json。
然而,在 Micronaut 中,如果我发送“接受:application/stream+json”,它会抛出一个错误。
{
"message": "Specified Accept Types [application/x-json-stream] not supported. Supported types: [application/json]",
"_links": {
"self": {
"href": "/carts/reactive",
"templated": false
}
}
}
Micronaut 中的“接受:application/stream+json”等价于什么?
What is the equivalent of "Accept: application/stream+json" in Micronaut?
正如评论中已经提到的,它是 application/x-json-stream
。 (遗憾的是,没有人为流式传输的内容类型制定标准 json,至少现在还没有。)
The question here is to how the client can control the response type - Json/stream. You are using produces = {MediaType.APPLICATION_JSON_STREAM, which means the return type is always stream. In spring boot, we can use Accept header to control what response type we want. I was expecting the same behaviour from Micronaut.
Micronaut 也可以做到这一点 - 你可以将多个值传递给 produces
参数,它会 return 相应地流式传输或标准 JSON:
@Get(value = "/", produces = {MediaType.APPLICATION_JSON_STREAM, MediaType.APPLICATION_JSON})
public Flux<Foo> getFoos() {
return Flux.range(1, 3).map(i -> new Foo("Number " + i));
}
@Value
class Foo {
private String content;
}
然后我们可以使用 application/x-json-stream
查询端点以检索 JSON 流:
> curl localhost:8080 -H "Accept: application/x-json-stream"
{"content":"Number 1"}{"content":"Number 2"}{"content":"Number 3"}
...或普通 application/json
检索标准 JSON 数组:
> curl localhost:8080 -H "Accept: application/json"
[{"content":"Number 1"},{"content":"Number 2"},{"content":"Number 3"}]
如果您希望对每种接受类型的行为进行更多自定义控制 header,那么您可以完全定义单独的控制器方法:
@Get(value = "/", produces = MediaType.APPLICATION_JSON_STREAM)
public Flux<Foo> getFoosStream() {
return Flux.range(1, 3).map(i -> new Foo("Reactive " + i));
}
@Get(value = "/", produces = MediaType.APPLICATION_JSON)
public List<Foo> getFoosStandard() {
return List.of(new Foo("Standard 1"), new Foo("Standard 2"), new Foo("Standard 3"));
}
...并且根据您发送的 header,将执行不同的方法(如上所述,您将能够看到标准 curl 命令的区别。)
在SpringBoot中,对于Webflux项目,我们通过在HTTP请求中发送一个header——“Accept:application/stream+json”来请求数据流。
如果我们发送“接受:application/json”,我们会得到一个有效的 Json。
然而,在 Micronaut 中,如果我发送“接受:application/stream+json”,它会抛出一个错误。
{
"message": "Specified Accept Types [application/x-json-stream] not supported. Supported types: [application/json]",
"_links": {
"self": {
"href": "/carts/reactive",
"templated": false
}
}
}
Micronaut 中的“接受:application/stream+json”等价于什么?
What is the equivalent of "Accept: application/stream+json" in Micronaut?
正如评论中已经提到的,它是 application/x-json-stream
。 (遗憾的是,没有人为流式传输的内容类型制定标准 json,至少现在还没有。)
The question here is to how the client can control the response type - Json/stream. You are using produces = {MediaType.APPLICATION_JSON_STREAM, which means the return type is always stream. In spring boot, we can use Accept header to control what response type we want. I was expecting the same behaviour from Micronaut.
Micronaut 也可以做到这一点 - 你可以将多个值传递给 produces
参数,它会 return 相应地流式传输或标准 JSON:
@Get(value = "/", produces = {MediaType.APPLICATION_JSON_STREAM, MediaType.APPLICATION_JSON})
public Flux<Foo> getFoos() {
return Flux.range(1, 3).map(i -> new Foo("Number " + i));
}
@Value
class Foo {
private String content;
}
然后我们可以使用 application/x-json-stream
查询端点以检索 JSON 流:
> curl localhost:8080 -H "Accept: application/x-json-stream"
{"content":"Number 1"}{"content":"Number 2"}{"content":"Number 3"}
...或普通 application/json
检索标准 JSON 数组:
> curl localhost:8080 -H "Accept: application/json"
[{"content":"Number 1"},{"content":"Number 2"},{"content":"Number 3"}]
如果您希望对每种接受类型的行为进行更多自定义控制 header,那么您可以完全定义单独的控制器方法:
@Get(value = "/", produces = MediaType.APPLICATION_JSON_STREAM)
public Flux<Foo> getFoosStream() {
return Flux.range(1, 3).map(i -> new Foo("Reactive " + i));
}
@Get(value = "/", produces = MediaType.APPLICATION_JSON)
public List<Foo> getFoosStandard() {
return List.of(new Foo("Standard 1"), new Foo("Standard 2"), new Foo("Standard 3"));
}
...并且根据您发送的 header,将执行不同的方法(如上所述,您将能够看到标准 curl 命令的区别。)