How to make a REST API with Spring Boot 4
Project Setup
- Open Spring Tool Suite. You can get it Here.
- Go to File > New > Spring Starter Project.
-
Leave the Service URL as is, and enter a project name. For the Type field, use Maven. For the Java Version, use 8. For the Group, use the name of your
organization or domain name.
Example here. Package can be same as group if you want.
Skip the Working Sets section, and click Next.
-
Add the web dependency. In the search box at the top of the Available field, type web. Then select the check box for it. When you mouse over Web, you should
see Servlet web application with Spring MVC and Tomcat.
Click Next.
- You can leave the base URL for the site as the default video. Click Finish.
Alternate Project Setup
- Go to start.spring.io.
- Make sure the dropdown menus at the top of the page say that you want to generate a Maven project with Java and Spring Boot 2.0.0.
-
In the Group field, enter the same organization name that you used when starting your Spring project. Use the same artifact from your Spring project as well. In
the Dependencies section, choose Web. You can also use the same package name and group.
- Click Generate Project.
-
Download the project and unzip it. You can now open the project with Netbeans. In Netbeans, click File > Open Project and pick the
unzipped project. Click Open.
This should be the same thing that you made with the Spring Tool Suite.
How to create a RESTful API in a Spring starter project
-
In Spring Tool Suite, right-click the package name under
src/main/java . (The package you are right-clicking should be named something like
com.appdeveloperblog.app.ws.) Click New > Package.
-
In the Name field, use the existing app name and append .ui.controller to it. So it should look like:
com.appsdeveloperblog.app.ws.ui.controller .
- Click Finish.
-
Right-click com.appsdeveloperblog.app.ws.ui.controller. Click New > Class. In the Name field, type
UserController. Click Finish.
-
In the new file that has been created,
UserController.java , add two annotations above the class, and add import
statements for the annotations, so your file should look like this:
package com.appsdeveloperblog.app.ws.ui.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("users")
public class UserController { }
This controller is intended to handle the user-related actions, so the URL should reflect that. When you add users to the
RequestMapping annotation, you are mapping the controller to a URL that ends in /users . When you run your server locally,
this API will be available at:
http://localhost:8080/users
How to add CRUD functionality/How to add methods that handle POST, GET, UPDATE, and DELETE requests
-
First, add stub methods to
UserController.java so that the file looks like this:
package com.catherineluse.demo.ui.controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("users")
public class UserController {
@GetMapping
public String getUser() {
return "get user was called";
}
@PostMapping
public String createUser() {
return "create user was called";
}
@PutMapping
public String updateUser() {
return "update user was called";
}
@DeleteMapping
public String deleteUser() {
return "delete user was called";
}
}
-
Then create a new run configuration. Click on the drop-down arrow next to the run button (the green circular button). Click
Run Configurations... Then from the left side panel, click Spring Boot App and click on the new icon in the upper left
corner to create a new run configuration.
-
In the Name field, enter the name of your project. In the Project field, select your project. Next to the
Main Type field, click the Search... button. Click your main package file in the Matching Items field.
Click Apply and Run. When you click Run, it should start TomCat and run your application.
- Then you can open Postman and try GET, PUT, POST, and DELETE calls to localhost:8080/users. You should be able to see the response.
How to connect to SQL
-
Add these dependencies to your
pom.xml :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data.jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
-
In the Resources folder, open the
application.properties file. Add the following lines to your file with your own username
and database name:
spring.datasource.username=john
spring.datasource.password=bonifas
spring.datasource.url=jdbc:mysql://localhost:3306/your_database_name
spring.jpa.hibernate.ddl-auto=update
|