|
| 1 | +package com.baeldung.reactive.functional; |
| 2 | + |
| 3 | +import static org.springframework.web.reactive.function.BodyExtractors.toMono; |
| 4 | +import static org.springframework.web.reactive.function.server.RequestPredicates.GET; |
| 5 | +import static org.springframework.web.reactive.function.server.RequestPredicates.POST; |
| 6 | +import static org.springframework.web.reactive.function.server.RouterFunctions.route; |
| 7 | +import static org.springframework.web.reactive.function.server.ServerResponse.ok; |
| 8 | + |
| 9 | +import org.springframework.context.annotation.Bean; |
| 10 | +import org.springframework.context.annotation.Configuration; |
| 11 | +import org.springframework.security.config.web.server.ServerHttpSecurity; |
| 12 | +import org.springframework.security.web.server.SecurityWebFilterChain; |
| 13 | +import org.springframework.web.reactive.function.server.RouterFunction; |
| 14 | +import org.springframework.web.reactive.function.server.ServerResponse; |
| 15 | + |
| 16 | +import com.baeldung.webflux.Employee; |
| 17 | +import com.baeldung.webflux.EmployeeRepository; |
| 18 | + |
| 19 | +@Configuration |
| 20 | +public class EmployeeFunctionalConfig { |
| 21 | + |
| 22 | + @Bean |
| 23 | + EmployeeRepository employeeRepository() { |
| 24 | + return new EmployeeRepository(); |
| 25 | + } |
| 26 | + |
| 27 | + @Bean |
| 28 | + RouterFunction<ServerResponse> getAllEmployeesRoute() { |
| 29 | + return route(GET("/employees"), |
| 30 | + req -> ok().body( |
| 31 | + employeeRepository().findAllEmployees(), Employee.class)); |
| 32 | + } |
| 33 | + |
| 34 | + @Bean |
| 35 | + RouterFunction<ServerResponse> getEmployeeByIdRoute() { |
| 36 | + return route(GET("/employees/{id}"), |
| 37 | + req -> ok().body( |
| 38 | + employeeRepository().findEmployeeById(req.pathVariable("id")), Employee.class)); |
| 39 | + } |
| 40 | + |
| 41 | + @Bean |
| 42 | + RouterFunction<ServerResponse> updateEmployee() { |
| 43 | + return route(POST("/employees/update"), |
| 44 | + req -> req.body(toMono(Employee.class)) |
| 45 | + .doOnNext(employeeRepository()::updateEmployee) |
| 46 | + .then(ok().build())); |
| 47 | + } |
| 48 | + |
| 49 | + @Bean |
| 50 | + RouterFunction<ServerResponse> composedRoutes() { |
| 51 | + return |
| 52 | + route(GET("/employees"), |
| 53 | + req -> ok().body( |
| 54 | + employeeRepository().findAllEmployees(), Employee.class)) |
| 55 | + |
| 56 | + .and(route(GET("/employees/{id}"), |
| 57 | + req -> ok().body( |
| 58 | + employeeRepository().findEmployeeById(req.pathVariable("id")), Employee.class))) |
| 59 | + |
| 60 | + .and(route(POST("/employees/update"), |
| 61 | + req -> req.body(toMono(Employee.class)) |
| 62 | + .doOnNext(employeeRepository()::updateEmployee) |
| 63 | + .then(ok().build()))); |
| 64 | + } |
| 65 | + |
| 66 | + @Bean |
| 67 | + public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { |
| 68 | + http.csrf() |
| 69 | + .disable() |
| 70 | + .authorizeExchange() |
| 71 | + .anyExchange() |
| 72 | + .permitAll(); |
| 73 | + return http.build(); |
| 74 | + } |
| 75 | +} |
0 commit comments