Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

68.5. Newline Delimited JSON (NDJSON)

	
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
public class DataController {
    @GetMapping(value = "/stream-data", produces = MediaType.APPLICATION_NDJSON_VALUE)
    public Flux<DataRecord> streamData() {
        // 返回流式 JSON 数据,每行一个 DataRecord 对象
        return Flux.just(
            new DataRecord("Alice", 30),
            new DataRecord("Bob", 25),
            new DataRecord("Charlie", 35)
        );
    }

    record DataRecord(String name, int age) {}
}