-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd130db
commit da48134
Showing
2 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
src/main/java/com/spring/crud/demo/config/RouterConfig.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.spring.crud.demo.config; | ||
|
||
import com.spring.crud.demo.handler.SuperHeroHandler; | ||
//import com.spring.crud.demo.service.ReactiveSuperHeroService; | ||
import lombok.AllArgsConstructor; | ||
//import org.springdoc.core.annotations.RouterOperation; | ||
//import org.springdoc.core.annotations.RouterOperations; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
//import org.springframework.web.reactive.function.server.RequestPredicates; | ||
import org.springframework.web.reactive.function.server.RouterFunction; | ||
import org.springframework.web.reactive.function.server.RouterFunctions; | ||
import org.springframework.web.reactive.function.server.ServerResponse; | ||
|
||
@Configuration | ||
@AllArgsConstructor | ||
public class RouterConfig { | ||
|
||
// private final ReactiveSuperHeroService reactiveSuperHeroService; | ||
// | ||
// @RouterOperations({ @RouterOperation(path = "/route/super-heroes", beanClass = SuperHeroHandler.class, beanMethod = "findAll"), | ||
// @RouterOperation(path = "/route/super-heroes/{id}", beanClass = SuperHeroHandler.class, beanMethod = "findById"), | ||
// @RouterOperation(path = "/route/super-heroes", beanClass = SuperHeroHandler.class, beanMethod = "save"), | ||
// @RouterOperation(path = "/route/super-heroes/{id}", beanClass = SuperHeroHandler.class, beanMethod = "update"), | ||
// @RouterOperation(path = "/route/super-heroes{id}", beanClass = SuperHeroHandler.class, beanMethod = "delete") | ||
// }) | ||
// @Bean | ||
// public RouterFunction<ServerResponse> routerFunction() { | ||
// | ||
// return SpringdocRouteBuilder.route() | ||
// .GET("/route/super-heroes", findAll(), findAllOpenAPI()) | ||
// .build(); | ||
// } | ||
// | ||
// | ||
// private HandlerFunction<ServerResponse> findAll() { | ||
// return req -> ServerResponse.ok().body(reactiveSuperHeroService.findAll(), SuperHero.class); | ||
// } | ||
// | ||
// | ||
// private Consumer<Builder> findAllOpenAPI() { | ||
// return ops -> ops.beanClass(ReactiveSuperHeroService.class).beanMethod("findAll"); | ||
// } | ||
|
||
|
||
|
||
private final SuperHeroHandler superHeroHandler; | ||
|
||
@Bean | ||
public RouterFunction<ServerResponse> routerFunction() { | ||
|
||
return RouterFunctions.route() | ||
.GET("/route/super-heroes", superHeroHandler::findAll) | ||
.GET("/route/super-heroes/{id}", superHeroHandler::findById) | ||
.POST("/route/super-heroes", superHeroHandler::save) | ||
.PUT("/route/super-heroes/{id}", superHeroHandler::update) | ||
.DELETE("/route/super-heroes/{id}", superHeroHandler::delete) | ||
.build(); | ||
} | ||
|
||
|
||
// @Bean | ||
// public RouterFunction<ServerResponse> routerFunction() { | ||
// | ||
// return RouterFunctions.route(RequestPredicates.GET("/route/super-heroes"), superHeroHandler::findAll) | ||
// .andRoute(RequestPredicates.GET("/route/super-heroes/{id}"), superHeroHandler::findById) | ||
// .andRoute(RequestPredicates.POST("/route/super-heroes"), superHeroHandler::save) | ||
// .andRoute(RequestPredicates.PUT("/route/super-heroes/{id}"), superHeroHandler::update) | ||
// .andRoute(RequestPredicates.DELETE("/route/super-heroes/{id}"), superHeroHandler::delete); | ||
// } | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/com/spring/crud/demo/handler/SuperHeroHandler.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.spring.crud.demo.handler; | ||
|
||
|
||
import com.spring.crud.demo.model.SuperHero; | ||
import com.spring.crud.demo.service.ReactiveSuperHeroService; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.reactive.function.server.ServerRequest; | ||
import org.springframework.web.reactive.function.server.ServerResponse; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
@Service | ||
@AllArgsConstructor | ||
public class SuperHeroHandler { | ||
|
||
private final ReactiveSuperHeroService reactiveSuperHeroService; | ||
|
||
|
||
public Mono<ServerResponse> findAll(ServerRequest serverRequest) { | ||
Flux<?> list = reactiveSuperHeroService.findAll(); | ||
return ServerResponse.ok() | ||
.contentType(MediaType.TEXT_EVENT_STREAM) | ||
.body(list, SuperHero.class); | ||
} | ||
|
||
|
||
public Mono<ServerResponse> findById(ServerRequest serverRequest) { | ||
int id = Integer.parseInt(serverRequest.pathVariable("id")); | ||
Mono<SuperHero> superHeroMono = reactiveSuperHeroService.findById(id); | ||
return ServerResponse.ok().body(superHeroMono, SuperHero.class); | ||
} | ||
|
||
|
||
public Mono<ServerResponse> save(ServerRequest serverRequest) { | ||
Mono<SuperHero> superHero = serverRequest.bodyToMono(SuperHero.class); | ||
Mono<SuperHero> superHeroMono = reactiveSuperHeroService.save(superHero.block()); | ||
return ServerResponse.ok().body(superHeroMono, SuperHero.class); | ||
} | ||
|
||
|
||
public Mono<ServerResponse> update(ServerRequest serverRequest) { | ||
int id = Integer.parseInt(serverRequest.pathVariable("id")); | ||
Mono<SuperHero> superHero = serverRequest.bodyToMono(SuperHero.class); | ||
Mono<SuperHero> superHeroMono = reactiveSuperHeroService.update(id, superHero.block()); | ||
return ServerResponse.ok().body(superHeroMono, SuperHero.class); | ||
} | ||
|
||
|
||
public Mono<ServerResponse> delete(ServerRequest serverRequest) { | ||
int id = Integer.parseInt(serverRequest.pathVariable("id")); | ||
Mono<Void> superHeroMono = reactiveSuperHeroService.delete(id); | ||
return ServerResponse.ok().body("Deleted successfully...!", String.class); | ||
} | ||
|
||
} |