Spring Boot REST API

Spring Boot makes it easy to create RESTful APIs using Spring Web. It simplifies the development of web services by providing built-in features like auto-configuration, embedded servers, and annotation-based configuration.


Creating a Simple REST Controller

Create a Spring Boot REST API that manages a list of users.


1. Define the Model (User.java)

package com.example.model;

public class User {
private int id;
private String name;
private String email;

public User() {}

public User(int id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}

// Getters & 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; }
}

2. Create the REST Controller (UserController.java)

package com.example.demo.controller;

import com.example.demo.model.User;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {

private List<User> userList = new ArrayList<>();

// Add some sample users
public UserController() {
userList.add(new User(1, "John Doe", "john@example.com"));
userList.add(new User(2, "Jane Smith", "jane@example.com"));
}

// 1️⃣ Get All Users
@GetMapping
public List<User> getAllUsers() {
return userList;
}

// 2️⃣ Get a Single User by ID
@GetMapping("/{id}")
public User getUserById(@PathVariable int id) {
return userList.stream()
.filter(user -> user.getId() == id)
.findFirst()
.orElse(null);
}

// 3️⃣ Create a New User
@PostMapping
public String createUser(@RequestBody User user) {
userList.add(user);
return "User added successfully!";
}

// 4️⃣ Update an Existing User
@PutMapping("/{id}")
public String updateUser(@PathVariable int id, @RequestBody User updatedUser) {
for (User user : userList) {
if (user.getId() == id) {
user.setName(updatedUser.getName());
user.setEmail(updatedUser.getEmail());
return "User updated successfully!";
}
}
return "User not found!";
}

// 5️⃣ Delete a User
@DeleteMapping("/{id}")
public String deleteUser(@PathVariable int id) {
userList.removeIf(user -> user.getId() == id);
return "User deleted successfully!";
}
}

Running the Spring Boot Application

Run the application using your IDE or with the following Maven command:

mvn spring-boot:run

Spring Boot starts an embedded Tomcat server (default: http://localhost:8080).


Testing the REST API with Postman:

Once the application is running, test the endpoints:


Get All Users

Get a Single User by ID

Create a New User

Update a User

Delete a User


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.