有人可以幫助我并告訴我如何在春季啟動時從此 URL https://hccs-advancejava.s3.amazonaws.com/student_course.json讀取 JSON 檔案嗎?謝謝!
uj5u.com熱心網友回復:
它非常簡單...
- 使用某些 HttpClient 類將檔案下載為字串。例如,您可以使用 RestTemplate
- 使用 ObjectMapper 類將 json 決議為物件
uj5u.com熱心網友回復:
RestTemplate 可用。
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class MyController {
private final RestTemplate restTemplate;
public MyController(final RestTemplateBuilder builder) {
this.restTemplate = builder.build();
}
@GetMapping
public List<Map<String, ?>> index() throws URISyntaxException {
final URI url = new URI("https://hccs-advancejava.s3.amazonaws.com/student_course.json");
final ResponseEntity<List> resp = restTemplate.getForEntity(url, List.class);
final List<Map<String, ?>> list = resp.getBody();
return list;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/352149.html
下一篇:JPA多重關系-一個資料庫列
