Here are the steps to create a RESTful API using Spring Boot in Java:
1. Create Spring Boot Project
- Open an IDE (such as IntelliJ IDEA or Eclipse).
- Select Create a new Spring Boot project.
- In the Project Creation Wizard, select Spring Web Dependencies. This will include the basic dependencies required to create a RESTful API, such as Spring MVC, etc.
2. Create a Controller Class
Create a new Java class in the src/main/java directory, for example.
import ; import ; import ; @RestController @RequestMapping("/api/users") public class UserController { @GetMapping("/") public String getUsers() { return "Hello, Users!"; } }
Code explanation:
-
@RestController
Annotation marks this class as a controller, and the data returned by methods in that class will be directly used as the content of the HTTP response, not the view name. -
@RequestMapping("/api/users")
Map a basic path for all requests in this controller/api/users
。 -
@GetMapping("/")
Indicates that this method will handleGET
Request, and the path to the request is/api/users/
(because@RequestMapping
The basic path has been set in ).
III. Run the project
The main class that runs Spring Boot application, usually with@SpringBootApplication
Annotated classes, such as:
import ; import ; @SpringBootApplication public class Application { public static void main(String[] args) { (, args); } }
Code explanation:
-
@SpringBootApplication
is a combination annotation that contains@Configuration
、@EnableAutoConfiguration
and@ComponentScan
. It enables Spring's automatic configuration feature and scans for the current package and its components under its subpackage. -
(, args);
Start the Spring Boot application,It is the class name of the startup class.
args
is a command line parameter.
4. Test API
Open a browser or use a tool (such as Postman) to accesshttp://localhost:8080/api/users/
, you will seeHello, Users!
news.
5. Add more API endpoints
You canUserController
Add more methods to for example:
import ; import ; import ; import ; import ; import ; import ; import ; @RestController @RequestMapping("/api/users") public class UserController { @GetMapping("/") public String getUsers() { return "Hello, Users!"; } @GetMapping("/{id}") public String getUserById(@PathVariable Long id) { return "User with id: " + id; } @PostMapping("/") public String createUser(@RequestBody String user) { return "Creating user: " + user; } @PutMapping("/{id}") public String updateUser(@PathVariable Long id, @RequestBody String user) { return "Updating user with id: " + id + " with " + user; } @DeleteMapping("/{id}") public String deleteUser(@PathVariable Long id) { return "Deleting user with id: " + id; } }
Code explanation:
-
@GetMapping("/{id}")
:deal withGET
Request, in the path{id}
is a path variable, using@PathVariable
Annotation binds it to method parametersid
superior. -
@PostMapping("/")
:deal withPOST
ask,@RequestBody
Annotation binds the data in the request body to method parametersuser
superior. -
@PutMapping("/{id}")
:deal withPUT
Request, can be used to update resources. -
@DeleteMapping("/{id}")
:deal withDELETE
Request, can be used to delete resources.
6. Configure application properties (optional)
You cansrc/main/resources/
orConfigure the properties of the application in the file, such as setting the server port:
:
=8081
:
server: port: 8081
7. Add service layer and data access layer (optional)
In order to make the application more perfect, you can add a service layer and a data access layer (Repository). Here is a simple example:
:
import ; @Service public class UserService { public String getUserById(Long id) { return "User with id: " + id; } }
:
import ; import ; import ; import ; import ; @RestController @RequestMapping("/api/users") public class UserController { @Autowired private UserService userService; @GetMapping("/{id}") public String getUserById(@PathVariable Long id) { return (id); } }
Code explanation:
- The @Service annotation marks UserService as a service component.
- @Autowired annotation injects UserService into UserController so that the controller can call the service layer methods.
Through the above steps, you can familiarize yourself with Java's Spring Boot to create a basic RESTful API. Have you learned how to learn? Follow Wei Ge Ai Programming and you can develop the full stack.
This is the end of this article about the detailed steps of creating a RESTful API using SpringBoot. For more related content on creating a RESTful API, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!