Wednesday, 1 January 2025

Java Spring Boot - interview

 We’ll build a UserController to: 

  • Create a user (POST) 

  • Get user by ID (GET) 

  • Update user (PUT) 

  • Delete user (DELETE) 

 

✅ 1. MyApp.java – Main Class 

package com.example; 
 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
 
@SpringBootApplication 
public class MyApp { 
   public static void main(String[] args) { 
       SpringApplication.run(MyApp.class, args); 
   } 
} 
 

 

✅ 2. User.java – Model Class 

package com.example; 
 
public class User { 
   private int id; 

 
   @NotNull(message = "Name must not be null") 

    @Size(min = 2, max = 20, message = "Name must be between 2 and 20 characters") 

   private String name; 

 
   private String email; 
 
   public User() {}  // required for @RequestBody 
 
   public User(int id, String name, String email) { 
       this.id = id; 
       this.name = name; 
       this.email = email; 
   } 
 
   // Getters and setters 
   public int getId() { return id; } 
   public void setId(int id) { this.id = id; } 
 
   public String getName() { return name; } 
   public void setName(String name) { this.name = name; } 
 
   public String getEmail() { return email; } 
   public void setEmail(String email) { this.email = email; } 
} 
 

 

✅ 3. UserService.java – Business Logic Layer 

package com.example; 
 
import org.springframework.stereotype.Component; 
 
import java.util.*; 
 
@Component 
public class UserService { 
 
   private final Map<Integer, User> users = new HashMap<>(); 
 
   public User getUser(int id) { 
       return users.get(id); 
   } 
 
   public List<User> getAllUsers() { 
       return new ArrayList<>(users.values()); 
   } 
 
   public User createUser(User user) { 
       users.put(user.getId(), user); 
       return user; 
   } 
 
   public User updateUser(int id, User user) { 
       user.setId(id); 
       users.put(id, user); 
       return user; 
   } 
 
   public boolean deleteUser(int id) { 
       return users.remove(id) != null; 
   } 
} 
 

 

✅ 4. UserController.java – REST API with Full Annotations 

package com.example; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.bind.annotation.*; 
 
import java.util.List; 
 
@RestController 
@RequestMapping("/api/users") // Base path for all endpoints 
public class UserController { 
 
   @Autowired 
   private UserService userService; 
 
   // GET all users 
   @GetMapping 
   public List<User> getAll() { 
       return userService.getAllUsers(); 
   } 
 
   // GET user by ID 
   @GetMapping("/{id}") 
   public User getById(@PathVariable int id) { 
       return userService.getUser(id); 
   } 
 
   // POST create new user 
   @PostMapping 
   public User create(@RequestBody User user) { 
       return userService.createUser(user); 
   } 
 
   // PUT update user by ID 
   @PutMapping("/{id}") 
   public User update(@PathVariable int id, @RequestBody User user) { 
       return userService.updateUser(id, user); 
   } 
 
   // DELETE user by ID 
   @DeleteMapping("/{id}") 
   public String delete(@PathVariable int id) { 
       boolean deleted = userService.deleteUser(id); 
       return deleted ? "Deleted" : "User not found"; 
   } 
} 
 

 

✅ Example API Calls 

Action 

Method 

URL 

Body (JSON) 

Get all users 

GET 

/api/users 

 

Get user by ID 

GET 

/api/users/1 

 

Create new user 

POST 

/api/users 

{ "id": 1, "name": "testuser", "email": "t@x.com" } 

Update user 

PUT 

/api/users/1 

{ "name": "newname", "email": "new@x.com" } 

Delete user 

DELETE 

/api/users/1 

 

 

🔄 Summary of Annotations Used 

Annotation 

Purpose 

@SpringBootApplication 

Boots the Spring app 

@RestController 

Creates REST endpoints 

@RequestMapping 

Sets base path 

@GetMapping 

Maps HTTP GET request 

@PostMapping 

Maps HTTP POST request 

@PutMapping 

Maps HTTP PUT request 

@DeleteMapping 

Maps HTTP DELETE request 

@PathVariable 

Extracts path variable from URL 

@RequestBody 

Binds JSON payload to Java object 

@Component 

Registers business logic class as Spring bean 

@Autowired 

Injects component automatically 

 

No comments:

Post a Comment