Code: Select all
"select labs.collected,biovals.value,biomark.high,biomark.low,biomark.name,biomark.unit from labs\n" + //
"inner join biovals on labs.id = biovals.lab_id\n" + //
"inner join biomark on biovals.biomar_id = biomark.id\n" + //
"where labs.collected='2010-10-29'"
< /code>
Die manuelle Abfrage gibt 76 Zeilen wie diese < /p>
zurückcollected value high low name unit
'2010-10-29', '4.4', '10.8', '3.4', 'WBC', 'x10E3/uL'
'2010-10-29', '5.1', '6', '3.77', 'RBC', 'x10E6/uL'
'2010-10-29', '16', '18', '11.1', 'Hemoglobin', 'g/dL'
'2010-10-29', '46.9', '55', '34', 'Hematocrit', '%'
< /code>
Ich möchte, dass Spring Boot in einem JsonArray ähnlich zurückgibt. Die erwartete Ausgabe ist: < /p>
[
{"collected":"2010-10-29", "value":"4.4", "high":"10.8", "low": "3.4", "name":"WBC", "unit":"x10E3/uL"}
...
]
Code: Select all
@GetMapping("/get-biovals-by-date")
public JSONArray getBiovalsByDate() {
JSONArray biovalsByDate = labService.getBiovalsByDate();
return biovalsByDate;
}
Code: Select all
@Service
public interface LabService {
JSONArray getBiovalsByDate();
}
Code: Select all
@Component
public class LabServiceImpl implements LabService {
@Autowired
BiovalRepo biovalRepo;
public JSONArray getBiovalsByDate() {
JSONArray jsonArray = new JSONArray();
try {
ResultSet rs = biovalRepo.getBiovalsByDate(); //error happens here
while (rs.next()) {
int columns = rs.getMetaData().getColumnCount();
JSONObject obj = new JSONObject();
for (int i = 0; i < columns; i++)
obj.put(rs.getMetaData().getColumnLabel(i + 1).toLowerCase(), rs.getObject(i + 1));
jsonArray.put(obj);
}
} catch (SQLException e) {
e.printStackTrace();
}
return jsonArray;
}
}
@Repository
public interface BiovalRepo extends JpaRepository {
@Query(value = "select labs.collected,biovals.value,biomark.high,biomark.low,biomark.name,biomark.panel,biomark.unit from labs\n" + //
"inner join biovals on labs.id = biovals.lab_id\n" + //
"inner join biomarkers on biovals.biomarker_id = biomarkers.id\n" + //
"where labs.date_collected='2010-10-29'", nativeQuery = true)
ResultSet getBiovalsByDate();
}
< /code>
Wenn dies ausgeführt wird, zeigt ein Fehler < /p>
Abfrage kein eindeutiges Ergebnis zurück: 76 Ergebnisse wurden zurückgegeben < /p>
< /blockquote>
Was mache ich falsch? < /p>