Skip to content

Instantly share code, notes, and snippets.

@alexcheng1982
Created November 6, 2017 09:02
Show Gist options
  • Save alexcheng1982/ece6a063de6c7cfe175c5a3ecd53b036 to your computer and use it in GitHub Desktop.
Save alexcheng1982/ece6a063de6c7cfe175c5a3ecd53b036 to your computer and use it in GitHub Desktop.
@Configuration
public class Config {
@Bean
@Autowired
public RouterFunction<ServerResponse> routerFunction(final CalculatorHandler calculatorHandler) {
return RouterFunctions.route(RequestPredicates.path("/calculator"), request ->
request.queryParam("operator").map(operator ->
Mono.justOrEmpty(
ReflectionUtils.findMethod(CalculatorHandler.class, operator, ServerRequest.class))
.flatMap(method -> (Mono<ServerResponse>) ReflectionUtils
.invokeMethod(method, calculatorHandler, request))
.switchIfEmpty(ServerResponse.badRequest().build())
.onErrorResume(
ex -> ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR).build()))
.orElse(ServerResponse.badRequest().build()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment