Here is the Spring Boot project structure based on the CRUD application using DTO, Controller, Service, Repository, Entity, and H2 Database:
spring-boot-crud/ │── src/ │ ├── main/ │ │ ├── java/com/example/demo/ │ │ │ ├── controller/ │ │ │ │ ├── UserController.java │ │ │ ├── dto/ │ │ │ │ ├── UserRequestDTO.java │ │ │ │ ├── UserResponseDTO.java │ │ │ ├── entity/ │ │ │ │ ├── User.java │ │ │ ├── repository/ │ │ │ │ ├── UserRepository.java │ │ │ ├── service/ │ │ │ │ ├── UserService.java │ │ │ ├── DemoApplication.java │ ├── resources/ │ │ ├── application.properties │ │ ├── data.sql (Optional: Preload Data) │── pom.xml │── README.md
Handles HTTP requests.
import com.preparationzone.learnspringboot.dto.UserRequestDTO; import com.preparationzone.learnspringboot.dto.UserResponseDTO; import com.preparationzone.learnspringboot.service.UserService; import jakarta.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/api/users") public class UserController { @Autowired private UserService userService; // Create User @PostMapping public UserResponseDTO createUser(@Valid @RequestBody UserRequestDTO userRequestDTO) { return userService.createUser(userRequestDTO); } // Get All Users @GetMapping public ListgetAllUsers() { return userService.getAllUsers(); } // Get User by ID @GetMapping("/{id}") public UserResponseDTO getUserById(@PathVariable Long id) { return userService.getUserById(id); } // Update User @PutMapping("/{id}") public UserResponseDTO updateUser(@PathVariable Long id, @Valid @RequestBody UserRequestDTO userRequestDTO) { return userService.updateUser(id, userRequestDTO); } // Delete User @DeleteMapping("/{id}") public void deleteUser(@PathVariable Long id) { userService.deleteUser(id); } }
Used to transfer request and response data.
UserRequestDTOimport jakarta.validation.constraints.Email; import jakarta.validation.constraints.NotBlank; import lombok.Getter; import lombok.Setter; @Getter @Setter public class UserRequestDTO { @NotBlank(message = "Name is required") private String name; @NotBlank(message = "Email is required") @Email(message = "Invalid email format") private String email; }
import lombok.AllArgsConstructor; import lombok.Getter; import lombok.Setter; @Getter @Setter @AllArgsConstructor public class UserResponseDTO { private Long id; private String name; private String email; }
Defines the database structure.
import jakarta.persistence.*; import lombok.*; @Entity @Table(name = "users") @Getter @Setter @NoArgsConstructor @AllArgsConstructor public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(nullable = false) private String name; @Column(nullable = false, unique = true) private String email; }
Handles database operations.
import com.preparationzone.learnspringboot.entity.User; import org.springframework.data.jpa.repository.JpaRepository; import java.util.Optional; public interface UserRepository extends JpaRepository{ Optional findByEmail(String email); }
Implements business logic.
import com.preparationzone.learnspringboot.dto.UserRequestDTO; import com.preparationzone.learnspringboot.dto.UserResponseDTO; import com.preparationzone.learnspringboot.entity.User; import com.preparationzone.learnspringboot.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; import java.util.stream.Collectors; @Service public class UserService { @Autowired private UserRepository userRepository; // Create User public UserResponseDTO createUser(UserRequestDTO userRequestDTO) { User user = new User(); user.setName(userRequestDTO.getName()); user.setEmail(userRequestDTO.getEmail()); user = userRepository.save(user); return new UserResponseDTO(user.getId(), user.getName(), user.getEmail()); } // Get All Users public List<UserResponseDTO> getAllUsers() { return userRepository.findAll() .stream() .map(user -> new UserResponseDTO(user.getId(), user.getName(), user.getEmail())) .collect(Collectors.toList()); } // Get User by ID public UserResponseDTO getUserById(Long id) { User user = userRepository.findById(id) .orElseThrow(() -> new RuntimeException("User not found")); return new UserResponseDTO(user.getId(), user.getName(), user.getEmail()); } // Update User public UserResponseDTO updateUser(Long id, UserRequestDTO userRequestDTO) { User user = userRepository.findById(id) .orElseThrow(() -> new RuntimeException("User not found")); user.setName(userRequestDTO.getName()); user.setEmail(userRequestDTO.getEmail()); userRepository.save(user); return new UserResponseDTO(user.getId(), user.getName(), user.getEmail()); } // Delete User public void deleteUser(Long id) { userRepository.deleteById(id); } }
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class LearnspringbootApplication { public static void main(String[] args) { SpringApplication.run(LearnspringbootApplication.class, args); } }
# H2 Database Configuration spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password= spring.jpa.database-platform=org.hibernate.dialect.H2Dialect spring.jpa.hibernate.ddl-auto=update spring.h2.console.enabled=true # Show SQL Queries spring.jpa.show-sql=true server.port=8081
$ mvn spring-boot:run
POST /api/users GET /api/users PUT /api/users/{id} DELETE /api/users/{id}