Ad

How To See Result Of Flux In Postman?

I mimic tutorial at https://howtodoinjava.com/spring-webflux/spring-webflux-tutorial/ . My MongoDB (My version https://fastdl.mongodb.org/windows/mongodb-windows-x86_64-5.0.5-signed.msi). Postman v9.8.0 , Windows 10 x64.

enter image description here

My controller

package com.howtodoinjava.demo.controller;

import com.howtodoinjava.demo.model.Employee;
import com.howtodoinjava.demo.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.Duration;

@RestController
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @RequestMapping(value = {"/create", "/"}, method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    @ResponseBody
    public void create(@RequestBody Employee e) {
        employeeService.create(e);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<Mono<Employee>> findById(@PathVariable("id") Integer id) {
        Mono<Employee> e = employeeService.findById(id);
        HttpStatus status = e != null ? HttpStatus.OK : HttpStatus.NOT_FOUND;
        return new ResponseEntity<Mono<Employee>>(e, status);
    }

    @RequestMapping(value = "/name/{name}", method = RequestMethod.GET)
    @ResponseBody
    public Flux<Employee> findByName(@PathVariable("name") String name) {
        return employeeService.findByName(name);
    }

    @RequestMapping(method = RequestMethod.GET, produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    @ResponseBody
    public Flux<Employee> findAll() {
        // Flux<Employee> emps = employeeService.findAll();
        Flux<Employee> emps = employeeService.findAll().log().delayElements(Duration.ofSeconds(1));

        emps.subscribe();

        return emps;
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ResponseStatus(HttpStatus.OK)
    public Mono<Employee> update(@RequestBody Employee e) {
        return employeeService.update(e);
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @ResponseStatus(HttpStatus.OK)
    public void delete(@PathVariable("id") Integer id) {
        employeeService.delete(id).subscribe();
    }

}

In debug console, I see the result

enter image description here

enter image description here

Ad

Answer

The reason why the responses are empty is that no getter methods are defined in the Employee entity class. Adding the following should make it work:

public int getId() {
    return id;
}

public String getName() {
    return name;
}

public long getSalary() {
    return salary;
}

As a side note, consider mapping your entities into EmployeeDTO instances.

Ad
source: stackoverflow.com
Ad