使用springwebflux依次消费两个api
Using spring webflux to consume two apis one after another
我需要编写一个新的 api,它将采用一些参数,消耗现有的 api,然后使用其结果消耗第二个现有的 api。
我之前没有使用过 spring 5,但我注意到 rest 模板将被弃用,我应该改用 webflux。
我已经完成了一些关于简单案例的教程,但不确定如何解决我的具体问题。
鉴于我有以下两个 apis:
@GetMapping("/foo/{id}")
public Foo getById(@PathVariable int id) {
return new Foo(id, "foo");
}
@PostMapping("/bar")
public Bar createBar(@RequestBody Bar bar) {
return bar;
}
我有一个新的 spring 启动应用程序,其中包含新的 api:
@SpringBootApplication
@RestController
public class ReactiveClientApplication {
@Bean
WebClient fooWebClient() {
return WebClient.builder()
.baseUrl("http://localhost:8080/foo")
.build();
}
@Bean
WebClient barWebClient() {
return WebClient.builder()
.baseUrl("http://localhost:8080/bar")
.build();
}
// This works fine
@PostMapping("/foo/{fooId}")
public Mono<Foo> foo(@PathVariable Integer fooId) {
return fooWebClient()
.get()
.uri("/{id}", fooId)
.retrieve()
.bodyToMono(Foo.class);
}
// This works fine
@PostMapping("/bar")
public Mono<Bar> bar() {
return barWebClient()
.post()
.body(Mono.just(new Bar("bar", new Date())), Bar.class)
.retrieve()
.bodyToMono(Bar.class);
}
// I cannot get this to work
@PostMapping("/foobar/{fooId}")
public Mono<Bar> fooBar(@PathVariable Integer fooId) {
Mono<Foo> fooMono = fooWebClient().get().uri("/{id}", fooId).retrieve().bodyToMono(Foo.class);
fooMono.flatMap(foo -> {
Mono<Bar> barMono = barWebClient().post().body(Mono.just(new Bar(foo.getFooStuff(), new Date())), Bar.class)
.retrieve().bodyToMono(Bar.class);
return barMono;
});
return null;
}
public static void main(String[] args) {
new SpringApplicationBuilder(ReactiveClientApplication.class)
.properties(Collections.singletonMap("server.port", "8081"))
.run(args);
}
}
foobar方法需要调用并等待foo的响应,用这个调用bar然后等待响应,return就可以了
我想我显然需要付出更多的努力来学习这一切是如何工作的,但我希望有人能够指出我应该如何做的正确方向。
谢谢!
你不应该 return null。下面的代码呢:
@PostMapping("/foobar/{fooId}")
public Mono<Bar> fooBar(@PathVariable Integer fooId) {
Mono<Foo> fooMono = fooWebClient().get().uri("/{id}", fooId).retrieve().bodyToMono(Foo.class);
return fooMono.flatMap(foo ->
barWebClient()
.post()
.body(Mono.just(new Bar(foo.getFooStuff(), new Date())), Bar.class)
.retrieve()
.bodyToMono(Bar.class)
);
}
我需要编写一个新的 api,它将采用一些参数,消耗现有的 api,然后使用其结果消耗第二个现有的 api。 我之前没有使用过 spring 5,但我注意到 rest 模板将被弃用,我应该改用 webflux。 我已经完成了一些关于简单案例的教程,但不确定如何解决我的具体问题。
鉴于我有以下两个 apis:
@GetMapping("/foo/{id}")
public Foo getById(@PathVariable int id) {
return new Foo(id, "foo");
}
@PostMapping("/bar")
public Bar createBar(@RequestBody Bar bar) {
return bar;
}
我有一个新的 spring 启动应用程序,其中包含新的 api:
@SpringBootApplication
@RestController
public class ReactiveClientApplication {
@Bean
WebClient fooWebClient() {
return WebClient.builder()
.baseUrl("http://localhost:8080/foo")
.build();
}
@Bean
WebClient barWebClient() {
return WebClient.builder()
.baseUrl("http://localhost:8080/bar")
.build();
}
// This works fine
@PostMapping("/foo/{fooId}")
public Mono<Foo> foo(@PathVariable Integer fooId) {
return fooWebClient()
.get()
.uri("/{id}", fooId)
.retrieve()
.bodyToMono(Foo.class);
}
// This works fine
@PostMapping("/bar")
public Mono<Bar> bar() {
return barWebClient()
.post()
.body(Mono.just(new Bar("bar", new Date())), Bar.class)
.retrieve()
.bodyToMono(Bar.class);
}
// I cannot get this to work
@PostMapping("/foobar/{fooId}")
public Mono<Bar> fooBar(@PathVariable Integer fooId) {
Mono<Foo> fooMono = fooWebClient().get().uri("/{id}", fooId).retrieve().bodyToMono(Foo.class);
fooMono.flatMap(foo -> {
Mono<Bar> barMono = barWebClient().post().body(Mono.just(new Bar(foo.getFooStuff(), new Date())), Bar.class)
.retrieve().bodyToMono(Bar.class);
return barMono;
});
return null;
}
public static void main(String[] args) {
new SpringApplicationBuilder(ReactiveClientApplication.class)
.properties(Collections.singletonMap("server.port", "8081"))
.run(args);
}
}
foobar方法需要调用并等待foo的响应,用这个调用bar然后等待响应,return就可以了
我想我显然需要付出更多的努力来学习这一切是如何工作的,但我希望有人能够指出我应该如何做的正确方向。
谢谢!
你不应该 return null。下面的代码呢:
@PostMapping("/foobar/{fooId}")
public Mono<Bar> fooBar(@PathVariable Integer fooId) {
Mono<Foo> fooMono = fooWebClient().get().uri("/{id}", fooId).retrieve().bodyToMono(Foo.class);
return fooMono.flatMap(foo ->
barWebClient()
.post()
.body(Mono.just(new Bar(foo.getFooStuff(), new Date())), Bar.class)
.retrieve()
.bodyToMono(Bar.class)
);
}