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
eaa6de41
Commit
eaa6de41
authored
Apr 12, 2021
by
ccottier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Methods created for getting address from other microservice
parent
d0603b01
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
81 additions
and
1 deletion
+81
-1
build.gradle
build.gradle
+1
-0
StudentService.java
...ain/java/com/student/details/services/StudentService.java
+80
-1
No files found.
build.gradle
View file @
eaa6de41
...
...
@@ -34,6 +34,7 @@ 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"
compile
'org.json:json:20210307'
}
dependencyManagement
{
...
...
src/main/java/com/student/details/services/StudentService.java
View file @
eaa6de41
...
...
@@ -2,9 +2,17 @@ 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.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
;
...
...
@@ -16,7 +24,6 @@ public class StudentService {
@Autowired
StudentRepository
studentRepository
;
public
List
<
Student
>
findAllStudents
()
{
return
studentRepository
.
findAll
();
}
...
...
@@ -55,4 +62,76 @@ public class StudentService {
response
.
put
(
"deleted"
,
true
);
return
response
;
}
public
Student
getStudentAddressFromEmail
(
Student
student
){
//get students email for api call
String
email
=
student
.
getEmail
();
String
apiUrl
=
String
.
format
(
"url/students/%s"
,
email
);
//make API call other microservice's Rest API
JSONObject
response
=
getJSONObjectFromAPICall
(
apiUrl
);
//make new address object with JSON response object
/*
Address address;
address.setStreet(res.get("street"));
...
*/
//add it to our student's address field!
/*
student.setAddress(address);
*/
return
student
;
}
public
JSONObject
getJSONObjectFromAPICall
(
String
apiUrl
){
HttpURLConnection
connection
;
StringBuffer
responseContent
=
new
StringBuffer
();
BufferedReader
reader
;
String
line
;
try
{
URL
url
=
new
URL
(
apiUrl
);
connection
=
(
HttpURLConnection
)
url
.
openConnection
();
//setup
connection
.
setRequestMethod
(
"GET"
);
connection
.
setConnectTimeout
(
5000
);
connection
.
setReadTimeout
(
5000
);
int
status
=
connection
.
getResponseCode
();
//read and write
if
(
status
>
299
)
{
reader
=
new
BufferedReader
(
new
InputStreamReader
(
connection
.
getErrorStream
()));
}
else
{
reader
=
new
BufferedReader
(
new
InputStreamReader
(
connection
.
getInputStream
()));
}
while
((
line
=
reader
.
readLine
())
!=
null
){
responseContent
.
append
(
line
);
}
reader
.
close
();
}
catch
(
MalformedURLException
e
)
{
e
.
printStackTrace
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
JSONObject
res
=
new
JSONObject
(
responseContent
.
toString
());
return
res
;
}
}
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