0% found this document useful (0 votes)
8 views6 pages

Spring Boot Controller Basics

In Spring Boot, a controller is a Java class that processes HTTP requests and responses, acting as a bridge between the frontend and backend. Routes define the URL paths that controllers listen to, with various routing concepts such as static, dynamic, grouped, query parameters, request payloads, and wildcard routes. The document provides a basic example of a Spring Boot controller and outlines the different types of routing along with their implementations.

Uploaded by

ashikap.23bcr
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Spring Boot Controller Basics

In Spring Boot, a controller is a Java class that processes HTTP requests and responses, acting as a bridge between the frontend and backend. Routes define the URL paths that controllers listen to, with various routing concepts such as static, dynamic, grouped, query parameters, request payloads, and wildcard routes. The document provides a basic example of a Spring Boot controller and outlines the different types of routing along with their implementations.

Uploaded by

ashikap.23bcr
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

🔹 What is a Controller?

In Spring Boot, a controller is a Java class that handles incoming HTTP requests (like GET,
POST) and sends back responses.
It acts as the middle layer between the frontend (client) and the backend logic or database.

🔹 What is a Route?

A route is the path in the URL that the controller listens to.
Example:
If you visit [Link] the /hello is the route.

✅ Basic Example

Let’s build a small Spring Boot controller that returns:

 a greeting at /hello

 user info at /user

✅ Step 1: Create a Spring Boot Project

You can do this via Spring Initializr


Add these dependencies:

 Spring Web

✅ Step 2: Create Controller

// File: [Link]

package [Link];

import [Link].*;

@RestController

@RequestMapping("/api/users")

public class HelloController {

@GetMapping("/{id}")

public String getUser(@PathVariable int id) {

return "User ID: " + id;


}

@PostMapping

public String createUser(@RequestBody String name) {

return "User " + name + " created!";

@PutMapping("/{id}")

public String updateUser(@PathVariable int id, @RequestBody String name) {

return "User " + id + " updated to " + name;

@PatchMapping("/{id}")

public String patchUser(@PathVariable int id, @RequestBody String field) {

return "User " + id + " partially updated: " + field;

@DeleteMapping("/{id}")

public String deleteUser(@PathVariable int id) {

return "User " + id + " deleted!";

🧠 Extra Notes

 @RestController: Combines @Controller and @ResponseBody (returns data, not a


view).

 @RequestMapping: Sets a base path for all methods.

 @GetMapping, @PostMapping: Handle specific HTTP methods.

 @RequestParam: For query parameters.

 @RequestBody: To get POST data (like JSON).

DIFFERENT TYPES OF ROUTING CONCEPTS in Java Spring Boot including:


 ✅ Static Routing

 ✅ Dynamic Routing (Path Variables)

 ✅ Grouped Routing (via base path)

 ✅ Query Parameters

 ✅ Request Payload (Body)

 ✅ Wildcard Routing

🔹 1. Static Routing

Static routes map to a fixed URL and do not change.

@GetMapping("/about")

public String about() {

return "This is a static route.";

📌 URL to access: [Link]

🔹 2. Dynamic Routing (Path Variables)

Dynamic routes accept variable values in the URL.

@GetMapping("/user/{id}")

public String getUserById(@PathVariable int id) {

return "User ID: " + id;

📌 URL to access: [Link]

🔸 Here 5 is dynamically passed as id.


🔹 3. Grouped Routing (Base Path)

You can group routes using a @RequestMapping base path.

@RestController

@RequestMapping("/api/products")

public class ProductController {

@GetMapping

public String getAll() {

return "All products";

@GetMapping("/{id}")

public String getProduct(@PathVariable int id) {

return "Product ID: " + id;

📌 Routes:

 [Link]

 [Link]

🔹 4. Query Parameters (?key=value in URL)

Used to send optional data in URL.

@GetMapping("/search")

public String search(@RequestParam String keyword) {

return "Search keyword: " + keyword;

}
📌 URL to access: [Link]

➡️You can also set default or optional values:

@GetMapping("/search")

public String search(@RequestParam(defaultValue = "default") String keyword) {

return "Search keyword: " + keyword;

🔹 5. Request Payload (Body) – Typically with POST, PUT, or PATCH

@PostMapping("/add-user")

public String addUser(@RequestBody User user) {

return "User " + [Link]() + " added!";

class User {

private String name;

private int age;

// Getters and setters

📌 Send a POST request with JSON body like:

"name": "Ashika",

"age": 21

}
🔹 6. Wildcard Routes (* or **)

Used to match any value or nested paths.

@GetMapping("/files/*")

public String matchSingleLevel() {

return "Matched a single path segment";

@GetMapping("/admin/**")

public String matchAllSubPaths() {

return "Matched multiple sub-paths under /admin";

📌 Matches:

 /files/[Link]

 /admin/section/page

You might also like