gpt4 book ai didi

java - spring http请求将具有不同请求参数的单个uri映射到不同的方法是好的做法还是坏的做法

转载 作者:行者123 更新时间:2023-11-29 05:10:46 25 4
gpt4 key购买 nike

我的问题是我需要实现 rest api 来根据学生的属性搜索学生以下是我的网址。

    http/my system/vo1/students/search?firstname=ABC&responseType=summary    http/my system/vo1/students/search?age=ABC&responseType=summary    http/my system/vo1/students/search?id=ABC& responseType =summary    http/my system/vo1/students/search?mobile=ABC& responseType =summary    http/my system/vo1/students/search?lastname=ABC& responseType =summary    http/my system/vo1/students/search?education=ABC& responseType=summary    http/my system/vo1/students/search?working=ABC& responseType =summary    http/my system/vo1/students/search? responseType =summary

responseType can be summary,hierarchy or can more types will be added in feature.

So i have single URI "/my system/vo1/students/search?" with different parametersand user will send max two optional parameters and min is one is mandatory that is responseType

in spring we can map single uri with different request parameters to different method as shown below

@RequestMapping(value = “my system/vo1/students/search", method = RequestMethod.GET)
@ResponseBody
public Students searchStudentByName(
@RequestParam(value = “name",required = true) String name,
@RequestParam(value = “ responseType", required = true) String responseType)
{
//do student fetching works
}

@RequestMapping(value = “my system/vo1/students/search", method = RequestMethod.GET)
@ResponseBody
public Students searchStudentByAge(
@RequestParam(value = “age",required = true) int age,
@RequestParam(value = “ responseType", required = true) String responseType)
{
//do student fetching works
}

等等……我们可以为此编写 9 种不同的方法。

另一种方法是只创建一个方法。

 @RequestMapping(value = “my system/vo1/students/search", method = RequestMethod.GET)
@ResponseBody
public Students searchStudentByAge(
@RequestParam(value = “firstname",required = true) String age,
@RequestParam(value = “age",required = false) int age,
@RequestParam(value = “lastname",required = false) String lastname,
@RequestParam(value = “working",required = false) String working,
//add more for each field that will be present in url...........
@RequestParam(value = “ responseType", required = true) String responseType)
{
//do student fetching works
}

在第二种方法中,我们可以编写单独的 validator 来验证输入请求。

我有以下问题

  1. 哪种方法最好(考虑最佳设计原则和实践)
  2. 为什么这是最好的方法
  3. 针对此类问题最好的面向对象设计是什么

最佳答案

一个方法将满足响应类型的路径变量。您也可以将搜索参数移动到 SearchCriteria 类并通过 JSR303 执行验证

@RequestMapping(value ="/search/{responseType}", method =RequestMethod.GET)
@ResponseBody
public Students searchStudent(@Pathvariable String responseType, @RequestBody @valid SearchCriteria criteria, BindingResult result)
{
if(result.hasErrors()){
// deal with your validation errors here
}
// Result= SearchService.search(criteria);
}

你的搜索条件看起来像

Class SearchCriteria{
@NotEmpty
@Getter @Setter private String firstname;

@Min(1)
@Getter @Setter int age;

}

更多验证约束 here

Spring MVC 和 JSR303 示例 here

关于java - spring http请求将具有不同请求参数的单个uri映射到不同的方法是好的做法还是坏的做法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28634769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com