GOOD ZIP file:
This is the file structure 👇
GoodApplication.java 👇
package com.example.good;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class GoodApplication {
public static void main(String[] args) {
SpringApplication.run(GoodApplication.class, args);
}
2. Usercontroller.java 👇
package com.example.good.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.good.entity.User;
import com.example.good.exception.ResourceNotFoundException;
import com.example.good.repository.UserRepository;
@RestController
@RequestMapping("/api/users")
public class UserController {
@Autowired
private UserRepository userRepository;
// Get all users
@GetMapping
public List<User> getAllUsers() {
return this.userRepository.findAll();
// Get user by id
@GetMapping("/{id}")
public User getUserById(@PathVariable long id) {
return this.userRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("User not found with id :" + id));
// Create user
@PostMapping
public User createUser(@RequestBody User user) {
return this.userRepository.save(user);
// Update user
@PutMapping("/{id}")
public User updateUser(@RequestBody User user, @PathVariable long id) {
User existingUser = this.userRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("User not found with id :" + id));
existingUser.setFirstName(user.getFirstName());
existingUser.setLastName(user.getLastName());
existingUser.setEmail(user.getEmail());
return this.userRepository.save(existingUser);
// Delete user by id
@DeleteMapping("/{id}")
public ResponseEntity<User> deleteUser(@PathVariable long id) {
User existingUser = this.userRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("User not found with id :" + id));
this.userRepository.delete(existingUser);
return ResponseEntity.ok().build();
3. user.java 👇
package com.example.good.entity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email")
private String email;
public User() {
public User(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
public long getId() {
return id;
public void setId(long id) {
this.id = id;
public String getFirstName() {
return firstName;
public void setFirstName(String firstName) {
this.firstName = firstName;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
public String getEmail() {
return email;
public void setEmail(String email) {
this.email = email;
4.ResourceNotfoundexception.java 👇
package com.example.good.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException {
private static final long serialVersionUID = 1L;
public ResourceNotFoundException(String message) {
super(message);
}
5.userrepositories.java(interface) 👇
package com.example.good.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.good.entity.User;
public interface UserRepository extends JpaRepository<User, Long> {