Java Code Examples for org.springframework.web.reactive.function.server.ServerRequest#bodyToMono()

The following examples show how to use org.springframework.web.reactive.function.server.ServerRequest#bodyToMono() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SubnetWebHandlers.java    From alcor with Apache License 2.0 6 votes vote down vote up
public Mono<ServerResponse> createSubnet(ServerRequest request) {

        final Mono<SubnetWebJson> subnetObj = request.bodyToMono(SubnetWebJson.class);
        final UUID projectId = UUID.fromString(request.pathVariable("projectId"));

        final UUID generatedSubnetId = UUID.randomUUID();
        Mono<SubnetWebJson> newSubnetObj = subnetObj.map(p ->
                p.getSubnet().getId().isEmpty() || !CommonUtil.isUUID(p.getSubnet().getId()) ?
                        new SubnetWebJson(p.getSubnet(), generatedSubnetId) : new SubnetWebJson(p.getSubnet()));

        Mono<SubnetWebJson> response = serviceProxy.createSubnet(projectId, newSubnetObj);

        return response.flatMap(od -> ServerResponse.ok()
                .contentType(APPLICATION_JSON)
                .body(fromObject(od)))
                .onErrorResume(SubnetNotFoundException.class, e -> ServerResponse.notFound().build());
    }
 
Example 2
Source File: VpcWebHandlers.java    From alcor with Apache License 2.0 6 votes vote down vote up
public Mono<ServerResponse> createVpc(ServerRequest request) {

        final Mono<VpcWebJson> vpcObj = request.bodyToMono(VpcWebJson.class);
        final UUID projectId = UUID.fromString(request.pathVariable("projectId"));

        final UUID generatedVpcId = UUID.randomUUID();
        Mono<VpcWebJson> newVpcObj = vpcObj.map(p ->
                p.getNetwork().getId().isEmpty() || !CommonUtil.isUUID(p.getNetwork().getId()) ?
                        new VpcWebJson(p.getNetwork(), generatedVpcId) : new VpcWebJson(p.getNetwork()));

        Mono<VpcWebJson> response = serviceProxy.createVpc(projectId, newVpcObj);

        return response.flatMap(od -> ServerResponse.ok()
                .contentType(APPLICATION_JSON)
                .body(fromObject(od)))
                .onErrorResume(VpcNotFoundException.class, e -> ServerResponse.notFound().build());
    }
 
Example 3
Source File: UserHandler.java    From springdoc-openapi with Apache License 2.0 5 votes vote down vote up
/**
 * PUT a User
 */
public Mono<ServerResponse> putUser(ServerRequest request) {
    // parse id from path-variable
    long customerId = Long.valueOf(request.pathVariable("id"));

    // get customer data from request object
    Mono<User> customer = request.bodyToMono(User.class);

    // get customer from repository 
    Mono<User> responseMono = customerRepository.putUser(customerId, customer);

    // build response
    return responseMono
        .flatMap(cust -> ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(fromObject(cust)));
}
 
Example 4
Source File: SubnetWebHandlers.java    From alcor with Apache License 2.0 5 votes vote down vote up
public Mono<ServerResponse> updateSubnet(ServerRequest request) {

        final Mono<SubnetWebJson> updatedSubnetObj = request.bodyToMono(SubnetWebJson.class);
        UUID projectId = UUID.fromString(request.pathVariable("projectId"));
        UUID subnetId = UUID.fromString(request.pathVariable("subnetId"));

        Mono<SubnetWebJson> response = serviceProxy.updateSubnetById(projectId, subnetId, updatedSubnetObj);

        return response.flatMap(od -> ServerResponse.ok()
                .contentType(APPLICATION_JSON)
                .body(fromObject(od)))
                .onErrorResume(SubnetNotFoundException.class, e -> ServerResponse.notFound().build());
    }
 
Example 5
Source File: EmployeeHandler.java    From webflux-rxjava2-jdbc-example with Apache License 2.0 5 votes vote down vote up
public Mono<ServerResponse> createNewEmployee(ServerRequest request) {
  Mono<Employee> employeeMono = request.bodyToMono(Employee.class);

  Mono<Employee> employee = repository.createNewEmployee(employeeMono);

  return ServerResponse.ok()
      .contentType(MediaType.APPLICATION_JSON)
      .body(employee, Employee.class);
}
 
Example 6
Source File: PersonHandler.java    From springdoc-openapi with Apache License 2.0 4 votes vote down vote up
public Mono<ServerResponse> save(ServerRequest request) {
    final Mono<Person> person = request.bodyToMono(Person.class);
    return ok()
            .contentType(MediaType.APPLICATION_JSON)
            .body(BodyInserters.fromPublisher(person.flatMap(personService::save), Person.class));
}
 
Example 7
Source File: UserHandler.java    From springdoc-openapi with Apache License 2.0 4 votes vote down vote up
/**
 * POST a User
 */
public Mono<ServerResponse> postUser(ServerRequest request) {
    Mono<User> customer = request.bodyToMono(User.class);
    return ServerResponse.ok().build(customerRepository.saveUser(customer));
}
 
Example 8
Source File: SoulTestHttpRouter.java    From soul with Apache License 2.0 4 votes vote down vote up
private Mono<ServerResponse> postHandler(final ServerRequest req) {
    final Mono<String> string = req.bodyToMono(String.class);
    //ResultBean resultBean = new ResultBean(1, "msg", "post hello world");
    return ok().body(string, String.class);
}
 
Example 9
Source File: UserHandler.java    From Building-RESTful-Web-Services-with-Spring-5-Second-Edition with MIT License 4 votes vote down vote up
public Mono<ServerResponse> createUser(ServerRequest request) {
	Mono<User> user = request.bodyToMono(User.class);
	return ServerResponse.ok().build(this.userRepository.saveUser(user));
}
 
Example 10
Source File: UserHandler.java    From Building-RESTful-Web-Services-with-Spring-5-Second-Edition with MIT License 4 votes vote down vote up
public Mono<ServerResponse> updateUser(ServerRequest request) {
	Mono<User> user = request.bodyToMono(User.class);
	return ServerResponse.ok().build(this.userRepository.updateUser(user));
}
 
Example 11
Source File: FnApplication.java    From spring-5-examples with MIT License 4 votes vote down vote up
public Mono<ServerResponse> createPerson(ServerRequest request) {
  Mono<Person> person = request.bodyToMono(Person.class);
  Mono<Person> saved = repository.savePerson(person);
  return ok().body(saved, Person.class);
}