get user api
This commit is contained in:
@@ -0,0 +1,16 @@
|
|||||||
|
package com.example.restfulwebservice.user;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class User {
|
||||||
|
private Integer id;
|
||||||
|
private String name;
|
||||||
|
private Date joinDate;
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package com.example.restfulwebservice.user;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class UserController {
|
||||||
|
private final UserDaoService userDaoService;
|
||||||
|
|
||||||
|
public UserController(UserDaoService userDaoService) {
|
||||||
|
this.userDaoService = userDaoService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/users")
|
||||||
|
public List<User> retrieveAllUsers() {
|
||||||
|
return userDaoService.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/users/{id}")
|
||||||
|
public User retrieveUser(@PathVariable int id) {
|
||||||
|
return userDaoService.findOne(id);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package com.example.restfulwebservice.user;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class UserDaoService {
|
||||||
|
private static List<User> users = new ArrayList<>();
|
||||||
|
private static int usersCount = 3;
|
||||||
|
|
||||||
|
static {
|
||||||
|
users.add(new User(1, "Kenneth", new Date()));
|
||||||
|
users.add(new User(2, "Kenneth", new Date()));
|
||||||
|
users.add(new User(3, "Kenneth", new Date()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<User> findAll() {
|
||||||
|
return users;
|
||||||
|
}
|
||||||
|
|
||||||
|
public User save(User user) {
|
||||||
|
if(user.getId() == null) {
|
||||||
|
user.setId(++usersCount);
|
||||||
|
}
|
||||||
|
users.add(user);
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public User findOne(int id) {
|
||||||
|
for(User user: users) {
|
||||||
|
if(user.getId() == id) {
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,2 +1,6 @@
|
|||||||
server:
|
server:
|
||||||
port: 8088
|
port: 8088
|
||||||
|
|
||||||
|
logging:
|
||||||
|
level:
|
||||||
|
org.springframework: DEBUG
|
||||||
Reference in New Issue
Block a user