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

spring http request mapping single uri with different request parameters to different method is good practice or bad practice

我的问题是我需要实现 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/mysystem/vo1/students/search?响应类型 = 摘要

responseType 可以是 summary,hierarchy 或者可以在功能中添加更多类型。

所以我只有一个 URI“/my system/vo1/students/search?”具有不同的参数 并且用户将发送最多两个可选参数,最小是一个必填参数,即 responseType

在spring中我们可以将具有不同请求参数的单个uri映射到不同的方法,如下所示

@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
    }

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

我有以下问题

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

在这种情况下,您绝对应该使用一个 RequestMapping(第二个选项)。如果您稍后想要组合查询参数怎么办?组合爆炸!

确保您了解 JPA 标准 API,或查看 Jooq 和 QueryDSL。这将使您的生活更轻松。

一个方法将满足响应类型的路径变量。您还可以将搜索参数移动到 SearchCriteria class 并通过 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