1. Notes
springboot provides @Contrller and @RestController.
@Controller: Returns page and data
@RestController: Return data
@RestMapping annotation:Mainly do path mapping url
value: The path to request the URL.
method: HTTP request method.
@RestMapping(value="user", method= )
1.1 GET
No parameters
@RequestMapping (value="/hello", method= ) public String hello(String name){ return "123"+name; }
Parameter pass
@RequestMapping (value="/hello", method= ) public String hello(String name){ return "123"+name; }
Parameter mapping
@RequestParam annotation represents parameter mapping, mapping the incoming nickname to name
@RequestMapping (value="/hello2", method= ) public String hello2(@RequestParam(value ="nickname",required = false) String name){ return "123"+name; }
1.2 POST
No parameters
@RequestMapping(value = "/post1", method = ) public String post1(){ return "hello post"; }
With parameters
@RequestMapping(value = "/post2", method = ) public String post2(String username, String password){ return username+"-"+password; }
Bean Packaging
@RequestMapping(value = "/post3",method = ) public String post3(User user){ (user); return "post"; }
json
To add annotation @RequestBody before the parameter, the parameter name passed in should be consistent with the private variables of the class.
@RequestMapping(value = "/post34",method = ) public String post4(@RequestBody User user){ (user); return "post"; }
1.3 Error
- 404: The road is wrong
- 405: Method not allowed
This is the article about springboot annotations and writing GET and POST interfaces. For more related content on springboot get post interfaces, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!