How to get JSON data from REST API in Java code example
Example 1: how to read a .json from web api java
try {
URL url = new URL("url");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
if(conn.getResponseCode() == 200) {
Scanner scan = new Scanner(url.openStream());
while(scan.hasNext()) {
String temp = scan.nextLine();
}
}
}
Example 2: java rest client response json
Create the Consumer class.
src/main/java/io/openliberty/guides/consumingrest/Consumer.java