#004: Building REST APIs with Spring Boot and Spring MVC
In this post, we'll learn how to build REST APIs with Spring Boot and Spring MVC.
We'll start by creating a simple Spring Boot application. Then, we'll add a controller to expose some REST endpoints. Finally, we'll test our API using Postman.
##Creating a Spring Boot Application
First, we'll need to create a Spring Boot application. We can use the Spring Initializr to do this.
Go to http://start.spring.io/ and select the following options:
Click Generate Project to download the project.
Extract the zip file and open the project in your favorite IDE.
##Adding a Controller
Next, we'll add a controller to expose some REST endpoints.
First, we'll create a new package called com.example.demo.controller
. Then, we'll create a new class called DemoController
within that package.
Our controller will have the following endpoints:
GET /
: A welcome messageGET /hello
: A greetingPOST /greeting
: A greeting with a custom messageHere's the code for our controller:
package com.example.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
@GetMapping("/")
public String index() {
return "Welcome to the Spring Boot REST API!";
}
@GetMapping("/hello")
public String hello() {
return "Hello, Spring Boot!";
}
@PostMapping("/greeting")
public String greeting(@RequestParam(value="name", defaultValue="World") String name) {
return "Hello, " + name + "!";
}
}
Let's break down what's happening in the code above.
First, we're using the @RestController
annotation to designate this class as a controller.
Then, we're using the @GetMapping
and @PostMapping
annotations to map HTTP GET and POST requests to specific methods in our controller.
Finally, we're using the @RequestParam
annotation to extract query parameters from the request. In this case, we're extracting the name
parameter. If the name
parameter is not specified, we're using the default value of World
.
##Testing the API
Now that we have our controller set up, let's test our API using Postman.
First, we'll need to start our application. We can do this by running the DemoApplication
class as a Java application.
Once the application has started, we can send requests to our API using Postman.
Let's send a GET request to the /
endpoint:
As you can see, we get a welcome message in response.
Let's send a GET request to the /hello
endpoint:
As you can see, we get a greeting in response.
Finally, let's send a POST request to the /greeting
endpoint:
As you can see, we get a greeting with a custom message in response.
##Conclusion
In this post, we learned how to build REST APIs with Spring Boot and Spring MVC.
We started by creating a simple Spring Boot application. Then, we added a controller to expose some REST endpoints. Finally, we tested our API using Postman.