oktatas:programozas:java:java_rest_api_kliens:metodusok
Tartalomjegyzék
Java REST API metódusokra bontva
- Szerző: Sallai András
- Copyright © 2022, Sallai András
- Szerkesztve: 2022, 2023, 2024
- Licenc: CC BY-SA 4.0
- Web: https://szit.hu
Függvényes változat
Ebben a leírásban egy példát látunk a JSON.simple használatával REST API elérésre.
Az alkalmazás
- App.java
import java.util.ArrayList; import models.EmployeeModel; import models.RestClient; public class App { public static void main(String[] args) throws Exception { System.out.println("REST API lekérés"); RestClient restClient = new RestClient(); ArrayList<EmployeeModel> employees = restClient.getEmployees(); for(EmployeeModel emp : employees) { System.out.printf( "%20s %14s %14.2f\n", emp.name, emp.city, emp.salary ); } } }
- EmployeeModel.java
package models; public class EmployeeModel { public String name; public String city; public double salary; public EmployeeModel(String name, String city, double salary) { this.name = name; this.city = city; this.salary = salary; } }
- RestClient.java
package models; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.Scanner; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; public class RestClient { private String host = "http://localhost:8000/api/"; public InputStream getDataAsStream() { InputStream inputStream = null; try { inputStream = tryGetDataAsStream(); } catch (MalformedURLException e) { System.err.println("Hiba! Az url nem jó"); }catch (IOException e) { System.err.println("Hiba! A lekérés sikertelen"); } return inputStream; } public InputStream tryGetDataAsStream() throws MalformedURLException, IOException{ String endpoint = "employees"; String urlStr = host + endpoint; URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.connect(); int response = conn.getResponseCode(); if(response != HttpURLConnection.HTTP_OK) { throw new RuntimeException("HttpResponseCode:" + response); }else { return url.openStream(); } } public String getDataAsString(InputStream inputStream) { Scanner sc = new Scanner(inputStream, "UTF-8"); String content = ""; while(sc.hasNext()) { content += sc.nextLine(); } sc.close(); return content; } public ArrayList<EmployeeModel> getEmployeeAsArray(String content) { ArrayList<EmployeeModel> list = null; try { list = tryGetEmployeeAsArray(content); }catch(ParseException e) { System.err.println("Hiba! Az értelmezés sikertelen!"); } return list; } public ArrayList<EmployeeModel> tryGetEmployeeAsArray(String content) throws ParseException { JSONParser parser = new JSONParser(); Object obj = parser.parse(content); JSONArray ja = (JSONArray) obj; ArrayList<EmployeeModel> list = new ArrayList<>(); for(int i=0; i<ja.size(); i++) { JSONObject jobj = (JSONObject) ja.get(i); String name = (String) jobj.get("name"); String city = (String) jobj.get("city"); double salary = Double.parseDouble((String)jobj.get("salary")); EmployeeModel employeeModel = new EmployeeModel(name, city, salary); list.add(employeeModel); } return list; } public ArrayList<EmployeeModel> getEmployees() { InputStream inputStream = this.getDataAsStream(); String content = this.getDataAsString(inputStream); return getEmployeeAsArray(content); } }
oktatas/programozas/java/java_rest_api_kliens/metodusok.txt · Utolsó módosítás: 2024/10/29 14:53 szerkesztette: admin