Preface
In the previous article, we mainly introduced the quick startup of SpringBoot. In this chapter, we mainly explain how SpringBoot can quickly develop a Restful interface and its commonly used tags.
Knowledge preparation
-
What is REST?
REST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状态转移。
Although I don’t quite understand it. In my humble opinion, using the HTTP protocol and using JSON or XML as the interface in the form of data interaction can be called a Restful interface. Intuitively, its common request method ishttps://github.com/git/git/pulls?state=closed
-
Types and types of HTTP interfaces
HTTP interface request types are mainly divided into four typesGET
:POST
, , ·DELETE·. There are also other types. In fact, because of the early evolution from , we often use only the and method. Personally think , which is inseparable from the methods of the two interfaces .PUT
XX
Servlet
GET
POST
doGet()
doPost()
-
Common HTTP objects
Request. Response. Cookie. Session. JSP should have 9 built-in objects. In our current back-end development process, the first 4 are mainly commonly used.
Interface implementation
Get method
@RequestMapping(path = "/get", method = RequestMethod.GET)
@ResponseBody
public String testGetMethod() {
return "hello";
}
POST method - @RequestParam passes parameters
@RequestMapping(path = "/post1", method = RequestMethod.POST)
@ResponseBody
public String testPostMethod(@RequestParam("params1") String params1, @RequestParam("params2") String params2) {
return "params1" + params1 + " ; " + "params2" + params2;
}
POST method - @RequestBody passes parameters
@RequestMapping(path = "/post2", method = RequestMethod.POST)
@ResponseBody
public String testPostMethod2(@RequestBody String params1, @RequestBody(required = false) String params2) {
return "params1" + params1 + " ; " + "params2" + params2;
}
Tag parsing
- @RestController & @Controller
- @RequestMapping
- @ResponseBody
- @RequestBody
- @RequestParams
- @PathVariable
full code
- Application.java
package com.yanxml.arsenal.springboot.quickstart;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = {"com.yanxml.arsenal.springboot"})
public class Application {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(Application.class);
application.run(args);
}
}
- Controller.java
package com.yanxml.arsenal.springboot.quickstart.controller;
import org.springframework.web.bind.annotation.*;
/**
* HelloWorld Controller.
*
* @author seanyanxml
* @date 2023-01-31
*/
@RestController
@RequestMapping(path = "/helloworld")
public class HelloWorldController {
@RequestMapping(path = "/get", method = RequestMethod.GET)
@ResponseBody
public String testGetMethod() {
return "hello";
}
@RequestMapping(path = "/post1", method = RequestMethod.POST)
@ResponseBody
public String testPostMethod(@RequestParam("params1") String params1, @RequestParam("params2") String params2) {
return "params1" + params1 + " ; " + "params2" + params2;
}
@RequestMapping(path = "/post2", method = RequestMethod.POST)
@ResponseBody
public String testPostMethod2(@RequestBody String params1, @RequestBody(required = false) String params2) {
return "params1" + params1 + " ; " + "params2" + params2;
}
}
Reference
[1]. Detailed explanation of RESTful architecture
[2]. RESTful-Baidu Encyclopedia
Postscript - Random Thoughts
This week is my first week at work after the New Year, but I always feel inexplicably tired... I don’t know if it’s because of my heart or my body. But I have adjusted my biological clock and went to bed early, which does have a greater impact on people’s mood and efficiency. Big improvement.