springBoot Controller的使用

xiaoxiao2021-02-28  108

注解作用@Controller处理http请求@RestControllerSpring4之后新加的注解,作用和原来的@ResponseBody配合@Controller相同。用于返回Json@PathVariable获取url中的参数@RequestParam获取请求参数的值@RequestMapping配置url映射,需要多个时可以将value写成一个集合@GetMapping组合注解,相当于@RequestMapping(method = RequestMethod.GET)@PostMapping组合注解,相当于@RequestMapping(method = RequestMethod.POST)

示例:

@PathVariable

获取url中所带的参数,如http://127.0.0.1:8081/girl/hello/say/12

@RequestMapping(value = "/say/{id}" ,method = RequestMethod.GET) public Integer say(@PathVariable("id") Integer id){ return id; }

@RequestParam

获取url后所带的请求参数,如http://127.0.0.1:8081/girl/hello/say?id=12

@RequestMapping(value = "/say" ,method = RequestMethod.GET) public Integer say(@RequestParam(value = "id",required = false,defaultValue ="1") Integer myId){ return myId; }

注:@RequestParam中‘required ’值表示时候可不传入,‘defaultValue ’值表示当不传入是的默认值,必须为String类型

@RequestMapping @RequestMapping(value = "/say" ,method = RequestMethod.GET)

等价于

@GetMapping(value = "/say")
转载请注明原文地址: https://www.6miu.com/read-29000.html

最新回复(0)