Example usage for org.springframework.web.reactive.function.server ServerResponse ok

List of usage examples for org.springframework.web.reactive.function.server ServerResponse ok

Introduction

In this page you can find the example usage for org.springframework.web.reactive.function.server ServerResponse ok.

Prototype

static BodyBuilder ok() 

Source Link

Document

Create a builder with the status set to HttpStatus#OK 200 OK .

Usage

From source file:com.example.TestEndpoint.java

Mono<ServerResponse> test(final ServerRequest request) {
    final Optional<String> value = request.queryParam("value");
    return Mono.justOrEmpty(value).map(Wrapper::new)
            .flatMap(v -> ServerResponse.ok().body(Mono.just(v), Wrapper.class))
            .switchIfEmpty(ServerResponse.notFound().build());
}

From source file:com.example.hello.HelloEndpoint.java

Mono<ServerResponse> hello(final ServerRequest serverRequest) {
    return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON)
            .body(Mono.just(new HelloMessage("hello", OffsetDateTime.now(ZoneId.of("Z")))), HelloMessage.class);
}

From source file:top.zhacker.ms.reactor.spring.function.handler.PersonHandler.java

public Mono<ServerResponse> getPerson(ServerRequest request) {
    int personId = Integer.valueOf(request.pathVariable("id"));
    Mono<ServerResponse> notFound = ServerResponse.notFound().build();
    Mono<Person> personMono = this.repository.getPerson(personId);
    return personMono
            .flatMap(person -> ServerResponse.ok().contentType(APPLICATION_JSON).body(fromObject(person)))
            .switchIfEmpty(notFound);//from ww w  .j a v  a  2 s.c om
}

From source file:top.zhacker.ms.reactor.spring.function.handler.PersonHandler.java

public Mono<ServerResponse> createPerson(ServerRequest request) {
    Mono<Person> person = request.bodyToMono(Person.class);
    return ServerResponse.ok().build(this.repository.savePerson(person));
}

From source file:com.example.talk.TalkEndpoint.java

Mono<ServerResponse> getTalkRoom(final ServerRequest serverRequest) {
    final long talkId = Long.parseLong(serverRequest.pathVariable("talkId"));
    final Mono<TalkRoom> talkRoom = talkRoomRepository.getTalkRoomByTalkRoomId(talkId);
    return talkRoom.flatMap(tr -> ServerResponse.ok().contentType(MediaType.APPLICATION_JSON)
            .body(Mono.just(tr), TalkRoom.class)).switchIfEmpty(ServerResponse.notFound().build());
}

From source file:top.zhacker.ms.reactor.spring.function.handler.PersonHandler.java

public Mono<ServerResponse> listPeople(ServerRequest request) {
    Flux<Person> people = this.repository.allPeople();
    return ServerResponse.ok().contentType(APPLICATION_JSON).body(people, Person.class);
}

From source file:com.example.message.MessageEndpoint.java

private Mono<ServerResponse> getMessages(final ServerRequest request) {
    final GetMessageRequest getMessageRequest = GetMessageRequest.create(request);
    final Mono<PageResult<Message>> result = messageRepository.findAll().as(getMessageRequest::filterPages)
            .collectList().map(getMessageRequest::toResult);
    return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(result,
            new ParameterizedTypeReference<PageResult<Message>>() {
            });/*from   w w  w.j av  a  2s .  co  m*/
}

From source file:com.example.talk.TalkEndpoint.java

Mono<ServerResponse> all(final ServerRequest request) {
    return request.principal().cast(UsernamePasswordAuthenticationToken.class)
            .map(UsernamePasswordAuthenticationToken::getPrincipal).cast(UserAdapter.class)
            .map(UserAdapter::getUserId).map(talkRoomRepository::findTalkRoomByUserId)
            .flatMap(Flux::collectList)// ww w.j  a v  a 2  s. co  m
            .flatMap(list -> ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(Mono.just(list),
                    new ParameterizedTypeReference<List<TalkRoom>>() {
                    }))
            .switchIfEmpty(ServerResponse.notFound().build());
}