What is the difference between method overloading and method overriding?

To create a RESTful web service using Spring Boot, you follow a few simple steps that take advantage of Spring Boot’s powerful and easy-to-use features. Below is a step-by-step guide to creating a REST API:

Step 1: Set Up the Spring Boot Project

Using Spring Initializr (Recommended):

  1. Go to the Spring Initializr website.
  2. Configure your project settings:
  3. Project: Maven Project (or Gradle Project).
  4. Language: Java (or Kotlin/Groovy).
  5. Spring Boot Version: Choose the latest stable version.
  6. Group: Set the group (e.g., com.example).
  7. Artifact: Set the artifact name (e.g., restful-service).
  8. Packaging: Jar (or War).
  9. Java Version: Select your preferred Java version (e.g., 8, 11, or 17).
  10. Add Dependencies:
  11. Spring Web: Provides the essential tools for building web applications and REST APIs.
  12. Spring Boot DevTools: (Optional) Adds tools like hot reload for faster development.
  13. H2 Database: (Optional) Add this for an in-memory database if needed.
  14. Click Generate to download the project, then extract the ZIP file to your workspace.

Alternative: Manual Setup Using Maven:

If you're setting up the project manually using Maven, you can add the following dependencies to your pom.xml:

<dependencies>
    <!-- Spring Web Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

<!-- DevTools for Hot Reload -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>


</dependencies>  

Step 2: Create the Main Application Class

The main class is where the Spring Boot application starts. It should be annotated with @SpringBootApplication.

package com.example.restfulservice;
import org.springframework.boot.SpringApplication;  

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class RestfulServiceApplication {

public static void main(String args) {

SpringApplication.run(RestfulServiceApplication.class, args);

}

}

  • @SpringBootApplication: This annotation marks the class as the entry point of the Spring Boot application. It includes @Configuration, @EnableAutoConfiguration, and @ComponentScan.

Step 3: Create a REST Controller

A REST Controller is responsible for handling HTTP requests and sending responses. Use the @RestController annotation to mark a class as a RESTful web service.

  1. Create a Controller Class:

Create a new class in the src/main/java/com/example/restfulservice folder.

package com.example.restfulservice;
import org.springframework.web.bind.annotation.GetMapping;  

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

@RequestMapping("/api")

public class GreetingController {

@GetMapping("/greeting")
public String greet() {
return "Hello, World!";
}


}  

  • @RestController: This annotation combines @Controller and @ResponseBody. It indicates that this class handles HTTP requests and returns the response body directly (usually as JSON).
  • @GetMapping("/greeting"): Maps HTTP GET requests to the /greeting endpoint.

Step 4: Run the Application

You can run the application in several ways:

Using an IDE (IntelliJ, Eclipse):

  • Locate the RestfulServiceApplication class in your IDE.
  • Right-click the file and select Run or Debug.

Using Maven:

  • Navigate to the project folder in the terminal and run:

bash mvn spring-boot:run

Packaging as a JAR:

  • Build the project using Maven:

bash mvn clean package * Run the JAR file:

bash java -jar target/restful-service-0.0.1-SNAPSHOT.jar

Once the application is running, the Spring Boot application starts an embedded Tomcat server (by default on port 8080).

Step 5: Test the RESTful Service

Open a web browser or use a tool like Postman or cURL to test your REST endpoint.

Using a Web Browser:

  • Visit http://localhost:8080/api/greeting to see the message: "Hello, World!".

Using cURL:

  • Run the following cURL command in your terminal:

bash curl http://localhost:8080/api/greeting

Step 6: Creating a REST API with Path and Query Parameters

You can enhance the API by accepting path variables or query parameters to make it more dynamic.

  1. Path Variables:

java @GetMapping("/greeting/{name}") public String greetWithName(@PathVariable String name) { return "Hello, " + name + "!"; }
2. In this example, /greeting/{name} accepts a path variable (name). If you visit http://localhost:8080/api/greeting/John, it will return: "Hello, John!".
3. Query Parameters:

java @GetMapping("/personalized-greeting") public String personalizedGreet(@RequestParam(defaultValue = "World") String name) { return "Hello, " + name + "!"; }
4. This maps a query parameter (name) to the greeting. For example, http://localhost:8080/api/personalized-greeting?name=John will return "Hello, John!", while the default is "Hello, World!".

Step 7: Returning JSON Response (Using POJO)

Instead of returning plain strings, you can return a Java object (POJO), which will automatically be serialized into JSON by Spring Boot.

  1. Create a POJO Class:

```java

public class Greeting {

private String message;

public Greeting(String message) {

this.message \= message;

}

public String getMessage() {

return message;

}

public void setMessage(String message) {

this.message \= message;

}

}

```
2. Update the Controller to Return a POJO:

java @GetMapping("/json-greeting") public Greeting greetWithJson() { return new Greeting("Hello, World!"); }
3. Now, when you visit http://localhost:8080/api/json-greeting, you'll get a JSON response:

json { "message": "Hello, World!" }

Step 8: Add POST, PUT, and DELETE Endpoints

In addition to GET, you can also create POST, PUT, and DELETE endpoints for a complete RESTful service.

Example of a POST Method:

@PostMapping("/greeting")
public Greeting createGreeting(@RequestBody Greeting greeting) {
    return new Greeting("Hello, " + greeting.getMessage() + "!");
}

  • @PostMapping: Handles HTTP POST requests.
  • @RequestBody: Maps the incoming JSON request to a Java object.

Step 9: Exception Handling (Optional)

You can add global exception handling to return meaningful error messages to the client using @ControllerAdvice.

@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> handleException(Exception e) {
        return new ResponseEntity<>("An error occurred: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

Summary:

  1. Set Up: Create a Spring Boot project with the necessary dependencies.
  2. Main Class: Define a @SpringBootApplication main class.
  3. Controller: Create a @RestController to handle HTTP requests.
  4. Run: Run the application using an IDE, Maven, or a JAR file.
  5. Test: Use a browser, Postman, or cURL to test your RESTful API.
  6. Enhance: Add path variables, query parameters, and return JSON objects.

With these steps, you can create and deploy a full-fledged RESTful web service using Spring Boot.