Commit 8e1bda87 authored by Darrick Yong's avatar Darrick Yong

moved api service to class

parent 835a937c
package com.student.details.services;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
public class APIService {
public static JSONObject get(String stringUrl) {
try {
URL url = new URL(stringUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
int responseCode = conn.getResponseCode();
if (responseCode != 200) {
throw new RuntimeException("" + responseCode);
} else {
String res = "";
Scanner scanner = new Scanner(url.openStream());
while (scanner.hasNext()) {
String nextVal = scanner.nextLine();
res += nextVal;
}
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(res);
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
return new JSONObject();
}
}
...@@ -16,37 +16,6 @@ public class StudentService { ...@@ -16,37 +16,6 @@ public class StudentService {
@Autowired @Autowired
StudentRepository studentRepository; StudentRepository studentRepository;
public JSONObject fetchService(String stringUrl) {
System.out.println("demo api");
try {
URL url = new URL(stringUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
int responseCode = conn.getResponseCode();
if (responseCode != 200) {
throw new RuntimeException("" + responseCode);
} else {
String res = "";
Scanner scanner = new Scanner(url.openStream());
while (scanner.hasNext()) {
String nextVal = scanner.nextLine();
res += nextVal;
}
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(res);
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
return new JSONObject();
}
public List<Student> findAllStudents() { public List<Student> findAllStudents() {
return studentRepository.findAll(); return studentRepository.findAll();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment