Skip to content

Instantly share code, notes, and snippets.

@Francesco-itembase
Created June 29, 2020 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Francesco-itembase/b1f8b84889937a5d7d46af7c1ff8350e to your computer and use it in GitHub Desktop.
Save Francesco-itembase/b1f8b84889937a5d7d46af7c1ff8350e to your computer and use it in GitHub Desktop.
Small webflux application
// AppRouter.java
@Configuration
public class AppRouter {
// works for a single bean
@Bean
public RouterFunction<ServerResponse> routes(ResouceHandler resourceHandler) {
return RouterFunctions.route(GET("/resource/{id}").and(accept(APPLICATION_JSON)), resourceHandler::get)
.andRoute(GET("/resource/filter/{filterQuery}").and(accept(APPLICATION_JSON)), resourceHandler::getFiltered);
}
}
// AppHandler.java
@Component
public class BookHandler {
private final AppService appService;
public BookHandler(AppService appService) {
this.appService = appService;
}
public Mono<ServerResponse> get(ServerRequest request) {
String id = request.pathVariable("id");
return ok()
.contentType(MediaType.APPLICATION_JSON)
.body(appService.getById(id), AppModel.class);
}
}
// AppRepository.java
public interface AppRepository extends ReactiveCrudRepository<AppModel, String> {
Mono<AppModel> findById(Mono<String> id);
@Query("{ 'filter': ?0 }")
Flux<AppModel> findByFilter(String filter);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment