I am a newbie on react, so please bear with me.
I have a rest endpoint in springboot using StreamingResponseBody to emit data.
@GetMapping("/3Dpoints")
public ResponseEntity<StreamingResponseBody> getPoints() {
StreamingResponseBody stream = out -> {
service.getPoints(out);
};
return ResponseEntity.ok()
.contentType(MediaType.TEXT_PLAIN)
.body(stream);
}
service:getPoints is:
public void streamPointCloud(java.io.OutputStream out) throws Exception{
String path= ....;
File file = new File(path);
PointReader reader = new PointReader (file);
for (Point p : reader.getPoints()) {
MyPoint mp = new MyPoint(p.getX(), p.getY(), p.getZ());
out.write((mp.toString()+"n").getBytes());
}
}
Rest endpoint streams the point data as strings.
Client is a react app, with consuming code as:
import logo from './logo.svg';
import './App.css';
import React, { Component } from "react";
class App extends Component {
state = {
points: []
};
async componentDidMount() {
const response = await fetch('/3Dpoints');
const body = await response.text();
this.setState({points: body});
}
render() {
const {points} = this.state;
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<div className="App-intro">
<h2>Points</h2>
{points.map(point =>
<div>
{point.body}
</div>
)}
</div>
</header>
</div>
);
}
}
export default App;
When I launch react app, I can see rest endpoint being invoked.
But I don’t see any data printed on react UI.
What mistake am I making in this whole flow?