Commit acfd1df3 authored by Darrick Yong's avatar Darrick Yong

Merge branch 'feat/config-server' into 'master'

Feat/config server

See merge request !2
parents cbf770ae 7bdabdfd
......@@ -33,7 +33,8 @@ dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-config'
implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap:3.0.2'
implementation "io.springfox:springfox-boot-starter:3.0.0"
implementation 'io.springfox:springfox-boot-starter:3.0.0'
implementation group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
compile 'org.json:json:20210307'
annotationProcessor 'org.projectlombok:lombok'
compileOnly 'org.projectlombok:lombok:1.18.16'
......
......@@ -2,11 +2,12 @@ package com.student.details.controllers;
import com.student.details.exceptions.ResourceNotFoundException;
import com.student.details.models.Student;
import com.student.details.services.APIService;
import com.student.details.services.StudentService;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
......@@ -18,6 +19,12 @@ public class DetailsController {
@Autowired
StudentService studentService;
@GetMapping("/students/demo")
public JSONObject fetchDemo() {
return APIService.get("https://opentdb.com/api.php?amount=10");
}
@GetMapping("/students")
public List<Student> getAllStudents() {
// System.out.println("This is the students index route.");
......
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();
}
}
......@@ -2,22 +2,21 @@ package com.student.details.services;
import com.student.details.exceptions.ResourceNotFoundException;
import com.student.details.models.Student;
import com.student.details.repositories.StudentRepository;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.MalformedParameterizedTypeException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@Service
public class StudentService {
......@@ -28,7 +27,6 @@ public class StudentService {
return studentRepository.findAll();
}
public Student addStudent(Student student) {
return studentRepository.save(student);
}
......
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