Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
student-personal-details-spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
student-details-microservice
student-personal-details-spring-boot
Commits
835a937c
Commit
835a937c
authored
Apr 12, 2021
by
Darrick Yong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add fetch service
parent
d0603b01
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
44 additions
and
8 deletions
+44
-8
build.gradle
build.gradle
+2
-1
DetailsController.java
...va/com/student/details/controllers/DetailsController.java
+7
-1
StudentService.java
...ain/java/com/student/details/services/StudentService.java
+35
-6
No files found.
build.gradle
View file @
835a937c
...
...
@@ -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'
}
dependencyManagement
{
...
...
src/main/java/com/student/details/controllers/DetailsController.java
View file @
835a937c
...
...
@@ -3,10 +3,10 @@ package com.student.details.controllers;
import
com.student.details.exceptions.ResourceNotFoundException
;
import
com.student.details.models.Student
;
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 +18,12 @@ public class DetailsController {
@Autowired
StudentService
studentService
;
@GetMapping
(
"/students/demo"
)
public
JSONObject
fetchDemo
()
{
return
studentService
.
fetchService
(
"https://opentdb.com/api.php?amount=10"
);
}
@GetMapping
(
"/students"
)
public
List
<
Student
>
getAllStudents
()
{
// System.out.println("This is the students index route.");
...
...
src/main/java/com/student/details/services/StudentService.java
View file @
835a937c
...
...
@@ -2,13 +2,13 @@ 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.simple.JSONObject
;
import
org.json.simple.parser.JSONParser
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Optional
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
import
java.util.*
;
@Service
public
class
StudentService
{
...
...
@@ -16,12 +16,41 @@ public class StudentService {
@Autowired
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
()
{
return
studentRepository
.
findAll
();
}
public
Student
addStudent
(
Student
student
)
{
return
studentRepository
.
save
(
student
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment