This article will introduce several common annotations in SpringBoot
Among them, the functions of each annotation are:
@PathVaribale Get data in url
@RequestParam Get the value of the request parameter
@GetMapping Combination Annotation is the abbreviation of @RequestMapping(method = RequestMethod.GET)
@RestController is a combination annotation of @ResponseBody and @Controller.
@PathVaribale Get data in url
Looking at an example, if we need to get the id value in Url=localhost:8080/hello/id, the implementation code is as follows:
@RestControllerpublic class HelloController { @RequestMapping(value="/hello/{id}",method= RequestMethod.GET) public String saysHello(@PathVariable("id") Integer id){ return "id:"+id; }}@RequestParam Get the value of the request parameter
Take a look at an example directly, as follows
@RestControllerpublic class HelloController { @RequestMapping(value="/hello",method= RequestMethod.GET) public String saysHello(@RequestParam("id") Integer id){ return "id:"+id; }}Enter the address in the browser: localhost:8080/hello?id=1000, and you can see the following results:
When we enter the address: localhost:8080/hello?id in the browser, that is, we do not enter the specific value of id, the result returned is null. The specific test results are as follows:
@GetMapping Combination Annotation
@GetMapping is a combination annotation, which is the abbreviation of @RequestMapping(method = RequestMethod.GET) . This annotation maps HTTP Get to a specific processing method.
That is, you can use @GetMapping(value = “/hello”) instead of @RequestMapping(value=”/hello”,method= RequestMethod.GET) . That is, let us streamline the code.
example
@RestControllerpublic class HelloController { //@RequestMapping(value="/hello",method= RequestMethod.GET) @GetMapping(value = "/hello") //required=false means that the id parameter can be entered in the url, and the default parameter will be used at this time. Public String saysHello(@RequestParam(value="id",required = false,defaultValue = "1") Integer id){ return "id:"+id; }}@RestController
The newly added annotation after Spring 4 turns out that returning json requires @ResponseBody and @Controller to cooperate.
That is, @RestController is a combination annotation of @ResponseBody and @Controller .
@RestControllerpublic class HelloController { @RequestMapping(value="/hello",method= RequestMethod.GET) public String saysHello(){ return "hello"; }}The same as the following code
@Controller@ResponseBodypublic class HelloController { @RequestMapping(value="/hello",method= RequestMethod.GET) public String saysHello(){ return "hello"; }}Annotation of the difference between @RequestParam and @PathVarible
@RequestParam is the parameter in the request. Such as get? id=1
@PathVarible is a variable in the request path such as get/id=1
Summarize
The above is the commonly used annotations and various annotations in SpringBoot introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!