如何在 Postman 中查看 Flux<Object> 的结果?

How to see result of Flux<Object> in Postman?

我模仿 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) 的教程。邮递员 v9.8.0 , Windows 10 x64.

我的控制器

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();
    }

}

在调试控制台中,我看到了结果

响应为空的原因是 Employee 实体 class 中没有定义 getter 方法。添加以下内容应该可以正常工作:

public int getId() {
    return id;
}

public String getName() {
    return name;
}

public long getSalary() {
    return salary;
}

附带说明一下,考虑将您的实体映射到 EmployeeDTO 个实例。