Created
June 29, 2020 07:51
-
-
Save Francesco-itembase/b1f8b84889937a5d7d46af7c1ff8350e to your computer and use it in GitHub Desktop.
Small webflux application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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