Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
L
Lab4-Examples
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
Michele Fiori
Lab4-Examples
Commits
da26ca18
You need to sign in or sign up before continuing.
Commit
da26ca18
authored
May 08, 2025
by
Michele Fiori
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added missing REST files
parent
499f59e6
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
179 additions
and
0 deletions
+179
-0
Application.java
src/main/java/REST/Application.java
+14
-0
ClientExample.java
src/main/java/REST/ClientExample.java
+46
-0
User.java
src/main/java/REST/User.java
+33
-0
UserService.java
src/main/java/REST/UserService.java
+38
-0
UsersController.java
src/main/java/REST/UsersController.java
+48
-0
No files found.
src/main/java/REST/Application.java
0 → 100644
View file @
da26ca18
package
REST
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
@SpringBootApplication
public
class
Application
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
Application
.
class
,
args
);
System
.
out
.
println
(
"Server running on http://localhost:8080"
);
}
}
src/main/java/REST/ClientExample.java
0 → 100644
View file @
da26ca18
package
REST
;
import
org.springframework.http.*
;
import
org.springframework.web.client.RestTemplate
;
public
class
ClientExample
{
public
static
void
main
(
String
[]
args
)
{
RestTemplate
client
=
new
RestTemplate
();
String
serverAddress
=
"http://localhost:8080"
;
// POST EXAMPLE
String
postPath
=
"/users/add"
;
User
user
=
new
User
(
"John"
,
"Cena"
);
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setContentType
(
MediaType
.
APPLICATION_JSON
);
HttpEntity
<
User
>
request
=
new
HttpEntity
<>(
user
,
headers
);
ResponseEntity
<
Void
>
postResponse
=
client
.
postForEntity
(
serverAddress
+
postPath
,
request
,
Void
.
class
);
System
.
out
.
println
(
"POST Response: "
+
postResponse
.
getStatusCode
());
// GET REQUEST #1 (get all users)
String
getPath
=
"/users"
;
ResponseEntity
<
User
[]>
getAllResponse
=
client
.
getForEntity
(
serverAddress
+
getPath
,
User
[].
class
);
System
.
out
.
println
(
"GET All Response: "
+
getAllResponse
.
getStatusCode
());
User
[]
users
=
getAllResponse
.
getBody
();
if
(
users
!=
null
)
{
System
.
out
.
println
(
"Users List:"
);
for
(
User
u
:
users
)
{
System
.
out
.
println
(
"Name: "
+
u
.
getName
()
+
", Surname: "
+
u
.
getSurname
());
}
}
// GET REQUEST #2 (get by name)
String
getUserPath
=
"/users/get/john"
;
ResponseEntity
<
User
>
userResponse
=
client
.
getForEntity
(
serverAddress
+
getUserPath
,
User
.
class
);
User
u
=
userResponse
.
getBody
();
if
(
u
!=
null
)
{
System
.
out
.
println
(
"Name: "
+
u
.
getName
()
+
", Surname: "
+
u
.
getSurname
());
}
else
{
System
.
out
.
println
(
"User not found."
);
}
}
}
src/main/java/REST/User.java
0 → 100644
View file @
da26ca18
package
REST
;
public
class
User
{
private
String
name
;
private
String
surname
;
//Remember to always add an empty constructor
public
User
()
{
}
public
User
(
String
name
,
String
surname
)
{
this
.
name
=
name
;
this
.
surname
=
surname
;
}
public
String
getName
()
{
return
name
;
}
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
}
public
String
getSurname
()
{
return
surname
;
}
public
void
setSurname
(
String
surname
)
{
this
.
surname
=
surname
;
}
}
src/main/java/REST/UserService.java
0 → 100644
View file @
da26ca18
package
REST
;
import
org.springframework.stereotype.Service
;
import
java.util.ArrayList
;
import
java.util.List
;
@Service
//Spring automatically makes this class a singleton bean
public
class
UserService
{
private
final
List
<
User
>
usersList
=
new
ArrayList
<>();
public
synchronized
List
<
User
>
getUsersList
()
{
return
new
ArrayList
<>(
usersList
);
}
public
synchronized
void
setUsersList
(
List
<
User
>
users
)
{
usersList
.
clear
();
usersList
.
addAll
(
users
);
}
public
synchronized
void
add
(
User
user
)
{
usersList
.
add
(
user
);
}
public
synchronized
User
getByName
(
String
name
)
{
System
.
out
.
println
(
"called"
);
for
(
User
user
:
usersList
)
{
System
.
out
.
println
(
user
.
getName
());
System
.
out
.
println
(
name
);
if
(
user
.
getName
()
!=
null
&&
user
.
getName
().
equalsIgnoreCase
(
name
))
{
return
user
;
}
}
return
null
;
// Not found
}
}
src/main/java/REST/UsersController.java
0 → 100644
View file @
da26ca18
package
REST
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.*
;
import
java.util.List
;
@RestController
@RequestMapping
(
"/users"
)
public
class
UsersController
{
private
final
UserService
userService
;
@Autowired
//tells Spring to automatically resolve and inject the required bean (object) into the class where it's used
public
UsersController
(
UserService
userService
)
{
this
.
userService
=
userService
;
}
// Return list of users
@GetMapping
public
ResponseEntity
<
List
<
User
>>
getUsersList
()
{
return
ResponseEntity
.
ok
(
userService
.
getUsersList
());
}
// Add a new user
@PostMapping
(
"/add"
)
public
ResponseEntity
<
Void
>
addUser
(
@RequestBody
User
user
)
{
userService
.
add
(
user
);
return
ResponseEntity
.
ok
().
build
();
}
// Get user by name
@GetMapping
(
"/get/{name}"
)
public
ResponseEntity
<
User
>
getByName
(
@PathVariable
String
name
)
{
User
user
=
userService
.
getByName
(
name
);
System
.
out
.
println
(
name
);
if
(
user
!=
null
)
return
ResponseEntity
.
ok
(
user
);
else
return
ResponseEntity
.
notFound
().
build
();
}
}
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