Only change UserService interface, UserServiceImpl class, and UserController class
UserService interface
UserService interface
package com.sb.basic.service;
import java.util.List;
import java.util.Optional;
import com.sb.basic.model.User;
public interface UserService {
List<User> userList();
Optional<User> findOne(Long id);
User addUser(User user);
String deleteUser(Long id);
}
UserServiceImpl class
package com.sb.basic.service.impl;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sb.basic.model.User;
import com.sb.basic.repository.UserRepository;
import com.sb.basic.service.UserService;
@Service
public class UserServiceImpl implements UserService {
private UserRepository userRepository;
//Better go with constructor injection so no need to put @Autwired again and again
@Autowired
public UserServiceImpl(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public List<User> userList() {
return userRepository.findAll();
}
@Override
public Optional<User> findOne(Long id) {
return userRepository.findById(id);
}
@Override
public User addUser(User user) {
return userRepository.save(user);
}
@Override
public String deleteUser(Long id) {
userRepository.deleteById(id);;
return "{'message':'User Deleted'}";
}
}
Create UserController class
package com.sb.basic.coontroller;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sb.basic.model.User;
import com.sb.basic.service.UserService;
@RestController
@RequestMapping("/user")
public class UserController {
private UserService userService;
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
@RequestMapping("/list")
public List<User> userList(){
return userService.userList();
}
@RequestMapping("/id/{id}")
public Optional<User> findOne(@PathVariable Long id){
return userService.findOne(id);
}
@RequestMapping("/add")
public User addUser(@RequestBody User user){
return userService.addUser(user);
}
@RequestMapping("/delete/{id}")
public String deleteUser(@PathVariable Long id){
return userService.deleteUser(id);
}
}
Check URL
01. User List -
Method - Get
Link - http://localhost:9080/springbootbasic/user/list
02. Delete User -
Method - Get
Link- http://localhost:9080/springbootbasic/user/delete/3
03. Find User By Id -
Method - Get
Link - http://localhost:9080/springbootbasic/user/id/1
04. Add User -
Method - Post
Link - http://localhost:9080/springbootbasic/user/add
{
"userId":"167",
"userName":"Perera",
"password":"dser"
}
04.1 User Update
Now we don't need to update method. We can use add method. Request body must identified with id.
Method -Post
Link-http://localhost:9080/springbootbasic/user/add
{
"id","3"
"userId":"167",
"userName":"Perera",
"password":"dser"
}
Priyantha.samaraweera
01. User List -
Method - Get
Link - http://localhost:9080/springbootbasic/user/list
02. Delete User -
Method - Get
Link- http://localhost:9080/springbootbasic/user/delete/3
03. Find User By Id -
Method - Get
Link - http://localhost:9080/springbootbasic/user/id/1
04. Add User -
Method - Post
Link - http://localhost:9080/springbootbasic/user/add
{
"userId":"167",
"userName":"Perera",
"password":"dser"
}
04.1 User Update
Now we don't need to update method. We can use add method. Request body must identified with id.
Method -Post
Link-http://localhost:9080/springbootbasic/user/add
{
"id","3"
"userId":"167",
"userName":"Perera",
"password":"dser"
}
Priyantha.samaraweera
No comments:
Post a Comment