mirror of
https://github.com/OtusTeam/Spring.git
synced 2026-11-18 20:07:12 +00:00
Пример к 20 заданию (WebFlux)
This commit is contained in:
1 parent
6695572d6a
commit
d66cbc23b1
3 files changed
+42
-24
No files matched your search
@@ -22,44 +22,57 @@ import static org.springframework.web.reactive.function.server.ServerResponse.ok
|
||||
@SpringBootApplication
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ApplicationContext context = SpringApplication.run(Main.class);
|
||||
PersonRepository repository = context.getBean(PersonRepository.class);
|
||||
public static void main( String[] args ) {
|
||||
ApplicationContext context = SpringApplication.run( Main.class );
|
||||
PersonRepository repository = context.getBean( PersonRepository.class );
|
||||
|
||||
repository.saveAll(Arrays.asList(
|
||||
new Person("Pushkin", 22),
|
||||
new Person("Lermontov", 22),
|
||||
new Person("Tolstoy", 60)
|
||||
)).subscribe(p -> System.out.println(p.getLastName()));
|
||||
repository.saveAll( Arrays.asList(
|
||||
new Person( "Pushkin", 22 ),
|
||||
new Person( "Lermontov", 22 ),
|
||||
new Person( "Tolstoy", 60 )
|
||||
) ).subscribe( p -> System.out.println( p.getLastName() ) );
|
||||
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RouterFunction<ServerResponse> composedRoutes(PersonRepository repository) {
|
||||
public RouterFunction<ServerResponse> composedRoutes( PersonRepository repository ) {
|
||||
|
||||
PersonHandler handler = new PersonHandler(repository);
|
||||
PersonHandler handler = new PersonHandler( repository );
|
||||
|
||||
RouterFunction<ServerResponse> route = route()
|
||||
.GET("/func/person", accept(APPLICATION_JSON), handler::list)
|
||||
.GET("/func/person/{id}", accept(APPLICATION_JSON),
|
||||
request -> repository.findById(request.pathVariable("id"))
|
||||
.flatMap(person -> ok().contentType(APPLICATION_JSON).body(fromObject(person)))
|
||||
.GET( "/func/person", accept( APPLICATION_JSON ), handler::list )
|
||||
.GET( "/func/person/{id}", accept( APPLICATION_JSON ),
|
||||
request -> repository.findById( request.pathVariable( "id" ) )
|
||||
.flatMap( person -> ok().contentType( APPLICATION_JSON ).body( fromObject( person ) ) )
|
||||
)
|
||||
.GET( "/func/person/age/{age}", accept( APPLICATION_JSON ),
|
||||
serverRequest -> ok().contentType( APPLICATION_JSON )
|
||||
.body( repository.findAllByAge( Integer.valueOf( serverRequest.pathVariable( "age" ) ) ), Person.class ) )
|
||||
.GET( "/func/person/find", accept( APPLICATION_JSON ),
|
||||
serverRequest -> ok().contentType( APPLICATION_JSON )
|
||||
.body( repository.findAllByAge( Integer.valueOf( serverRequest.queryParam( "age" ).get() ) ), Person.class ) )
|
||||
.build();
|
||||
|
||||
return route;
|
||||
}
|
||||
|
||||
|
||||
static class PersonHandler {
|
||||
|
||||
private PersonRepository repository;
|
||||
|
||||
PersonHandler(PersonRepository repository) {
|
||||
PersonHandler( PersonRepository repository ) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
Mono<ServerResponse> list(ServerRequest request) {
|
||||
return ok().contentType(APPLICATION_JSON).body(repository.findAll(), Person.class);
|
||||
Mono<ServerResponse> list( ServerRequest request ) {
|
||||
return ok().contentType( APPLICATION_JSON ).body( repository.findAll(), Person.class );
|
||||
}
|
||||
|
||||
Mono<ServerResponse> listAge( ServerRequest request ) {
|
||||
System.out.println( "I'm here" );
|
||||
return ok().contentType( APPLICATION_JSON )
|
||||
.body( repository.findAllByAge( Integer.valueOf( request.queryParam( "age" ).get() ) ), Person.class );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+7
-7
@@ -13,21 +13,21 @@ public class AnnotatedController {
|
||||
|
||||
@GetMapping("/flux/one")
|
||||
public Mono<String> one() {
|
||||
return Mono.just("one");
|
||||
return Mono.just( "one" );
|
||||
}
|
||||
|
||||
@GetMapping("/flux/ten")
|
||||
public Flux<Integer> list() {
|
||||
return Flux.range(1, 10);
|
||||
return Flux.range( 1, 10 ).delayElements( Duration.ofSeconds( 1 ) );
|
||||
}
|
||||
|
||||
@GetMapping(path = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
||||
public Flux<String> stream() {
|
||||
return Flux.generate(() -> 0, (state, emitter) -> {
|
||||
emitter.next(state);
|
||||
return Flux.generate( () -> 0, ( state, emitter ) -> {
|
||||
emitter.next( state );
|
||||
return state + 1;
|
||||
})
|
||||
.delayElements(Duration.ofSeconds(1L))
|
||||
.map(i -> "" + i);
|
||||
} )
|
||||
.delayElements( Duration.ofSeconds( 1L ) )
|
||||
.map( i -> "" + i );
|
||||
}
|
||||
}
|
||||
+5
@@ -29,4 +29,9 @@ public class PersonController {
|
||||
public Mono<Person> save(@RequestBody Mono<Person> dto) {
|
||||
return repository.save(dto);
|
||||
}
|
||||
|
||||
@GetMapping("/person/find")
|
||||
public Flux<Person> byName(@RequestParam("name") String name){
|
||||
return repository.findAllByLastName( name );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user