Controller层参数注解

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

@PathVariable
	@RequestMapping("/getuserid/{id}")
	public AjaxMessage getUser(@PathVariable("id") int id) {
		User user = userService.querUserById(id);
		if (user != null) {
			return new AjaxMessage(true, "", user);
		}else {
			return new AjaxMessage(false, "没有数据", null);
		}
	}

在这里插入图片描述

@RequestParam
	@RequestMapping("/getuserpwd")
	public AjaxMessage getUserPwd(@RequestParam("pwd") String password) {
		List<User> userList = userService.queryUserByPwd(password);
		if (userList != null) {
			return new AjaxMessage(true,"",userList);
		}else {
			return new AjaxMessage(false, "没有数据", new ArrayList<User>());
		}
	}

在这里插入图片描述

@RequestBody
@PostMapping(path = "/demo1")
public void demo1(@RequestBody Person person) {
    System.out.println(person.toString());
}

也可以是

@PostMapping(path = "/demo1")
public void demo1(@RequestBody Map<String, String> person) {
    System.out.println(person.get("name"));
}

在这里插入图片描述

可选参数

属性required = true/false 代表这个参数选填

  • 在@PathVariable注解中,在@RequestMapping中,需列出各个url格式
	@RequestMapping(value = { "/checkidpv/{item}/{area}/{location}", "/checkidpv/{item}/{area}", "/checkidpv/{item}",
	"/checkidpv" }, method = RequestMethod.GET)
	public Object getSpotByIdPV(@PathVariable(value = "item", required = false) String item,
			@PathVariable(value = "area", required = false) String area,
			@PathVariable(value = "location", required = false) String location) {
		// function body
	}

在这里插入图片描述

  • 在@RequestParam中,该属性写在注解中。另外,通过defaultValue属性设置默认值
	@RequestMapping(value = "/checkidrq", method = RequestMethod.GET)
	public Object getSpotByIdRP(@RequestParam(value = "item",required = false) String item, @RequestParam(value = "area",required = false) String area,
			@RequestParam(value = "location",required = false) String location) {
	  // function body
}

在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值