多个参数时,使用 restful 风格的弊端,如:@GetMapping (“/list2/{page}/{size}“)

Restful风格的接口在处理多个参数时,要求所有路径变量都必须提供,这可能导致无法仅传递部分参数。例如,无法在查询时不指定页码或数量,而通常需要默认值。虽然Restful提供了清晰的URL映射,但可能导致过多的请求。文章强调技术选择应根据实际应用场景来决定,过度依赖任何一种技术都可能使问题复杂化。

多个参数时,使用 restful 风格的弊端,如:@GetMapping ("/list2/{page}/{size}") 

弊端就是参数 1,参数 2:page,size, 不可以只传其中一个,或都不传。

比方说查询,要是都不传可以默认查首页固定条数的记录。

要是 page 不传,可以默认 page=1,

但 restful 这种风格就很难做到。 

每一项技术都不可能打遍天下,过度迷信某个技术,只会将问题复杂化。

restful 的好处就是清楚,每个请求对应一个 url; 但可能会导致请求过多。所以还是要看自己的应用场景。

具体可参考:
https://www.jb51.net/article/226821.htm

测试例子:

//	为什么不使用这种方式:
////	https://www.jb51.net/article/226821.htm
//	@GetMapping("/list2/{page}/{size}")
//	public Result list2(Users users,
//		@PathVariable( value = "page", required = false) int page, 
//		@PathVariable(value = "size", required = false) int size) {	
//	  Result  result =new Result();
//	  try{
//		  System.err.println("------------users------list2-----------------");
//		  System.err.println("page: "+ page +   "  size: "+size);
//		  int total = objSQLRichService.count(users);
//		  List<Users> list=objSQLRichService.select(users, (page-1)*size, size);
//		  result.setRows(list);
//		  result.setTotal(total);
//	  } catch (BeeSQLException e) {
//		  result.setErrorMsg(e.getMessage());
//	  }
//		
//	   return result;
//	}
	
//	@RequestMapping(value = "/add/{a}/{b}",method = RequestMethod.GET)
//	public String test(@PathVariable int a,@PathVariable int b, Model model){
//	int res = a+b;
//	model.addAttribute("msg","结果为"+res);
//	return "test";
//	}

### RESTful GET请求路径参数映射的正确法 在Spring MVC中,可以通过`@RequestMapping`或更具体的`@GetMapping`注解来实现RESTful风格GET请求路径参数映射。以下是正确的法及其实现方式: #### 使用@RequestMapping 当需要处理GET请求,可以结合`@RequestMapping`注解的`method`属性指定HTTP方法为`RequestMethod.GET`。同,通过路径中的占位符(如`{id}`)捕获路径参数,并使用`@PathVariable`注解将其绑定到方法参数上[^1]。 ```java package com.lq.controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/chapter14/user") public class UserController { @RequestMapping(value = "/{id}", method = RequestMethod.GET) public String getUserById(@PathVariable("id") Long id) { return "User ID: " + id; } } ``` 上述代码中,`/chapter14/user/{id}`路径中的`{id}`会被捕获并传递给`getUserById`方法中的`id`参数[^1]。 #### 使用@GetMappingSpring 4.3开始,引入了更简洁的`@GetMapping`注解,它等价于`@RequestMapping(method = RequestMethod.GET)`。这种方式使得代码更加直观和易读[^3]。 ```java package com.lq.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/chapter14/user") public class UserController { @GetMapping("/{id}") public String getUserById(@PathVariable("id") Long id) { return "User ID: " + id; } } ``` 在此示例中,`@GetMapping("/{id}")`直接指定了GET请求的路径,同`@PathVariable("id")`将路径中的`id`参数绑定到方法参数上[^3]。 ### 注意事项 - 路径参数必须与`@PathVariable`注解中的名称一致,或者显式指定名称映射关系。 - 如果路径参数可能为空或不存在,应考虑添加校验逻辑以避免潜在的异常[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值