Spring Boot - Request Parsing - Guided Exercise: API Input Mastery: Unlocking Data from Every Corner! #197
Replies: 6 comments
-
// File: SmartUserApiApplication.java
package com.example.smartuser;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SmartUserApiApplication {
public static void main(String[] args) {
SpringApplication.run(SmartUserApiApplication.class, args);
}
}
// File: controller/UserController.java
package com.example.smartuser.controller;
import com.example.smartuser.model.User;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api")
public class UserController {
@GetMapping("/greet")
public String greetUser(@RequestParam String name) {
return "Hello, " + name + "! Welcome to SmartUserAPI.";
}
@GetMapping("/user/{id}")
public String getUserById(@PathVariable int id) {
return "Fetching user with ID: " + id;
}
@PostMapping("/user")
public String createUser(@RequestBody User user) {
return "User Created: " + user.toString();
}
@PostMapping("/user/{id}/update")
public String updateUser(
@PathVariable int id,
@RequestParam String city,
@RequestBody User user) {
return "Updated user " + id + " in city " + city + " with data: " + user;
}
@GetMapping("/user/info")
public ResponseEntity<String> getUserInfo(
@RequestParam(required = false, defaultValue = "Guest") String name,
@RequestParam(required = false, defaultValue = "Unknown") String city) {
return ResponseEntity.ok("User Info - Name: " + name + ", City: " + city);
}
}
// File: model/User.java
package com.example.smartuser.model;
import lombok.*;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class User {
private int id;
private String name;
private int age;
private String city;
}
// File: resources/application.properties
server.port=8082
spring.application.name=SmartUserAPI
# Enable pretty logs
logging.level.org.springframework=INFO
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n |
Beta Was this translation helpful? Give feedback.
-
|
Here’s a step-by-step Spring Boot project walkthrough to demonstrate the different ways to accept input in REST APIs using:
We’ll build an API to manage Customers, with multiple endpoints showcasing these parameter types. 🗂 Folder Structure⚙️ Step 1: Create a Spring Boot projectGo to https://start.spring.io/ and generate a project with:
Extract and import it into Eclipse. 📄 Step 2: Add Dependencies in
|
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
graph TD
subgraph Client
A[Postman/User Interface]
end
subgraph Controller Layer
B1[/POST /api/customers/]:::body
B2[/POST /api/customers/create-quick/]:::param
B3[/GET /api/customers/:id/]:::path
B4[/GET /api/customers?email=xyz/]:::query
end
subgraph Service Layer
S1[CustomerService Interface]
S2[CustomerServiceImpl]
end
subgraph Data Layer
R1[CustomerRepository]
DB[(H2 Database)]
end
A --> B1 --> S1 --> S2 --> R1 --> DB
A --> B2 --> S1
A --> B3 --> S1
A --> B4 --> S1
class B1 body;
class B2 param;
class B3 path;
class B4 query;
|
Beta Was this translation helpful? Give feedback.
-
|
PRASETHA N |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
🧩 Guided Exercise: "API Input Mastery: Unlocking Data from Every Corner!"
🧠 Concepts Covered
@RequestParam@PathVariable@RequestBody🎯 Goal
Build a Spring Boot mini-API called SmartUserAPI, which performs basic user operations using all types of input mechanisms.
🧱 Project Setup
Dependencies via
start.spring.io:Port:
8082Update in
application.properties:🗂️ Folder Structure
1️⃣ Step 1: Create the
UserModel2️⃣ Step 2: Expose GET with @RequestParam
Task: Fetch greeting message with a user’s name via query param.
🧩 Try this in Postman:
3️⃣ Step 3: Use @PathVariable to Get User Info by ID
Try:
4️⃣ Step 4: Post User Data using @RequestBody
🧪 Try this in Postman:
http://localhost:8082/user{ "id": 1, "name": "Riya", "age": 25, "city": "Bangalore" }5️⃣ Step 5: Mixed Usage: @RequestParam + @PathVariable + @RequestBody
🧪 Postman:
http://localhost:8082/user/1/update?city=Delhi{ "id": 1, "name": "Riya", "age": 26, "city": "Delhi" }🧠 Recap Questions
@RequestParamover@PathVariable?@RequestBodyonly work with POST/PUT methods?📌 Bonus Challenge
Implement validation:
Add:
🧭 Learning Outcome
By end of this, learners can:
Beta Was this translation helpful? Give feedback.
All reactions