Look at the following REST-API created in Node.js
NodeJS:
app.get("/stockData/:name", (req, res) => {
// Get All Data
db.query(
"SELECT date, close FROM stocks WHERE name ='"
req.params.name
"'ORDER BY date AND name",
(err, results) => {
if (err) {
res.send(err);
} else {
res.json(results);
}
}
);
});
If I do a request i get the following response structure:
Now i need to build the same in REST-API and SQL-Query in Spring Boot. So that I get the same response. I tried it with this code:
Spring Boot:
//Controller
@RestController
public class ApiController {
@Autowired
private StockRepository stockRepository;
@CrossOrigin(origins = "*")
@GetMapping("/stockData/{name}")
public double[] getStockData(@PathVariable String name) {
return stockRepository.getStockData(name);
}
//Repo
@Query(value = "SELECT a.date, a.close FROM stocks AS a WHERE a.name = ?1 ORDER BY a.date AND a.name", nativeQuery = true)
double[] getStockData(String name);
But it doesnt work... Does somebody know how i can implement the same in Spring Boot?
CodePudding user response:
Your json shows an array of objects with attributes date and close but you are returning a double[] from getSockData(). You need to create a class that contains attributes date and close and return a list of these objects instead. Use json formatting annotations for the date if needed.