本文介绍了@RequestParam vs @PathVariable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

处理特殊字符时, @RequestParam @PathVariable 之间有什么区别?

What is the difference between @RequestParam and @PathVariable while handling special characters?

+ @RequestParam 接受为空格。

@PathVariable 的情况下, + 是接受为 +

In the case of @PathVariable, + was accepted as +.

推荐答案


  • @PathVariable 是从URI获取一些占位符(Spring称之为URI模板)
    - 参见

  • @RequestParam 也是从URI获取参数 - 请参阅

    • @PathVariable is to obtain some placeholder from the URI (Spring call it an URI Template)— see Spring Reference Chapter 16.3.2.2 URI Template Patterns
    • @RequestParam is to obtain an parameter from the URI as well — see Spring Reference Chapter 16.3.3.3 Binding request parameters to method parameters with @RequestParam
    • 如果URL http:// localhost:8080 / MyApp的/用户/ 1234 /发票?日期= 12-05- 2013 在2013年12月5日获取用户1234的发票,控制器方法如下:

      If URL http://localhost:8080/MyApp/user/1234/invoices?date=12-05-2013 gets the invoices for user 1234 on December 5th, 2013, the controller method would look like:

      @RequestMapping(value="/user/{userId}/invoices", method = RequestMethod.GET)
      public List<Invoice> listUsersInvoices(
                  @PathVariable("userId") int user,
                  @RequestParam(value = "date", required = false) Date dateOrNull) {
        ...
      }
      

      此外,请求参数可以是可选的,从Spring 4.3.3开始,路径变量。请注意:这可能会更改URL路径层次结构并引入请求映射冲突。例如, / user / invoices 会为用户 null 提供发票,或者提供ID为发票的用户的详细信息?

      Also, request parameters can be optional, and as of Spring 4.3.3 path variables can be optional as well. Beware though: this might change the URL path hierarchy and introduce request mapping conflicts. For example, would /user/invoices provide the invoices for user null or details about a user with ID "invoices"?

      这篇关于@RequestParam vs @PathVariable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 03:09