springboot annotation and GET, POST interface writing
2024-04-02 11:41:31 Author: Cow right knife face
I. Comments
springboot provides @Contrller and @RestController.
@Controller: Returns the page and data
@RestController: Returns data
@RestMapping Note: Mainly do path mapping url
value: indicates the path to the requested URL.
method: indicates the HTTP request method.
@RestMapping(value="user", method= RequestMethod.GET)
1.1 GET
parameter-free
@RequestMapping (value="/hello", method= RequestMethod.GET) public String hello(String name){ return "123"+name; }
Parameter passing
@RequestMapping (value="/hello", method= RequestMethod.GET) public String hello(String name){ return "123"+name; }
Parameter mapping
The @RequestParam annotation represents the parameter mapping and maps the nickname that came in to name
@RequestMapping (value="/hello2", method= RequestMethod.GET) public String hello2(@RequestParam(value ="nickname",required = false) String name){ return "123"+name; }
1.2 POST
parameter-free
@RequestMapping(value = "/post1", method = RequestMethod.POST) public String post1(){ return "hello post"; }
Tape parameter
@RequestMapping(value = "/post2", method = RequestMethod.POST) public String post2(String username, String password){ return username+"-"+password; }
Bean encapsulation
@RequestMapping(value = "/post3",method = RequestMethod.POST) public String post3(User user){ System.out.println(user); return "post"; }
json
To add a @RequestBody annotation to the parameter, pass in the same parameter name as the class's private variable
@RequestMapping(value = "/post34",method = RequestMethod.POST) public String post4(@RequestBody User user){ System.out.println(user); return "post"; }
1.3 Errors
- 404: The road is not strong
- 405: Method not allowed
To this article about springboot annotations and GET, POST interface writing is introduced to this, more related to springboot get post interface writing content please search script home previous articles or continue to browse the following related articles hope that you will support script home in the future!
Articles you may be interested in:
- springboot International multi-language configuration
- SpringBoot integration I18n internationalization file takes effect outside the jar package
- Flexible configuration of SpringBoot multi-environment switching detailed tutorial
- How SpringBoot integrates with i18n(Multilingual)
- IDEA springboot message java: Symbol not found Symbol: variable log problem
- How does SpringBoot customize thread pool configuration classes
- The difference and description between Spring and Spring Boot