Support HTTP GET of single value value Function

E.g. a Function<Long, Foo> can be used to fetch a single entity of
type Foo with a long ID., via /{function}/{id}
This commit is contained in:
Dave Syer
2017-03-10 16:33:34 +00:00
parent 17b644f563
commit 2ee97721ab
3 changed files with 76 additions and 25 deletions

View File

@@ -79,4 +79,15 @@ public class FunctionController {
Flux<String> result = (Flux<String>) supplier.get();
return debug ? result.log() : result;
}
@GetMapping(path = "/{name}/{value}")
public Flux<String> value(@PathVariable String name, @PathVariable String value) {
Function<Flux<?>, Flux<?>> function = functions.lookupFunction(name);
if (function != null) {
@SuppressWarnings({ "unchecked" })
Flux<String> result = (Flux<String>) function.apply(Flux.just(value));
return debug ? result.log() : result;
}
throw new IllegalArgumentException("no such function: " + name);
}
}

View File

@@ -150,6 +150,16 @@ public class RestApplicationTests {
.isEqualTo("[FOO][BAR]");
}
@Test
public void uppercaseGet() {
assertThat(rest.getForObject("/uppercase/foo", String.class)).isEqualTo("[FOO]");
}
@Test
public void convertGet() {
assertThat(rest.getForObject("/wrap/123", String.class)).isEqualTo("..123..");
}
@Test
public void uppercaseJsonArray() throws Exception {
assertThat(rest.exchange(
@@ -193,6 +203,11 @@ public class RestApplicationTests {
.map(value -> "[" + value.trim().toUpperCase() + "]");
}
@Bean
public Function<Flux<Integer>, Flux<String>> wrap() {
return flux -> flux.log().map(value -> ".." + value + "..");
}
@Bean
public Function<Flux<HashMap<String, String>>, Flux<Map<String, String>>> maps() {
return flux -> flux.map(value -> {