【问题标题】:spring http request mapping single uri with different request parameters to different method is good practice or bad practicespring http请求将具有不同请求参数的单个uri映射到不同的方法是好的做法还是不好的做法
【发布时间】:2015-02-21 02:52:27
【问题描述】:

我的问题是我需要实现 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/我的系统/vo1/students/search? responseType =总结

responseType可以是summary,hierarchy,也可以在feature中添加更多类型。

所以我只有一个 URI“/my system/vo1/students/search?”有不同的参数 并且用户将发送 max 两个可选参数,min 是一个强制参数,即 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. 针对此类问题的最佳面向对象设计是什么

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复。所有标准都不是通过 post 方法调用来的,它们是请求参数。@RequestBody 注释将与 HTTP Get 方法一起使用吗?。
    • 是的,会的,但那是一个不同的问题,如果当前问题得到解决,您可以接受答案并打开一个新问题
    • 我已经在上面编写了代码,但它给了我 org.springframework.http.converter.HttpMessageNotReadableException: 无法读取 JSON:当我提交 Http 时,由于输入结束,没有要映射的内容获取 http/my system/vo1/students /search/summary?firstname=abc&age=4
    • 打开一个不同的问题
    • 抱歉,您的解决方案对我不起作用。我正在寻找解决问题的最佳方案。如上所述,我已经有了两个解决方案。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-24
      • 2023-03-19
      • 1970-01-01
      相关资源
      最近更新 更多