@RequestParam和@PathVariable在处理特殊字符时有什么区别?

+被@RequestParam接受为空格。

在@PathVariable的情况下,+被接受为+。


当前回答

@PathVariable - must be placed in the endpoint uri and access the query parameter value from the request
@RequestParam - must be passed as method parameter (optional based on the required property)
 http://localhost:8080/employee/call/7865467

 @RequestMapping(value=“/call/{callId}", method = RequestMethod.GET)
 public List<Calls> getAgentCallById(
            @PathVariable(“callId") int callId,
            @RequestParam(value = “status", required = false) String callStatus) {

    }

http://localhost:8080/app/call/7865467?status=Cancelled

@RequestMapping(value=“/call/{callId}", method = RequestMethod.GET)
public List<Calls> getAgentCallById(
            @PathVariable(“callId") int callId,
            @RequestParam(value = “status", required = true) String callStatus) {

}

其他回答

1) @RequestParam用于提取查询参数

http://localhost:3000/api/group/test?id=4

@GetMapping("/group/test")
public ResponseEntity<?> test(@RequestParam Long id) {
    System.out.println("This is test");
    return ResponseEntity.ok().body(id);
}

而@PathVariable用于从URI中提取数据:

http://localhost:3000/api/group/test/4

@GetMapping("/group/test/{id}")
public ResponseEntity<?> test(@PathVariable Long id) {
    System.out.println("This is test");
    return ResponseEntity.ok().body(id);
}

2) @RequestParam在传统的web应用程序中更有用,其中数据主要通过查询参数传递,而@PathVariable更适合rest式web服务,其中URL包含值。

3)如果查询参数不存在或使用defaultValue属性为空,@RequestParam注释可以指定默认值,前提是所需的属性为false:

@RestController
@RequestMapping("/home")
public class IndexController {

    @RequestMapping(value = "/name")
    String getName(@RequestParam(value = "person", defaultValue = "John") String personName) {
        return "Required element of request param";
    }

}
@PathVariable - must be placed in the endpoint uri and access the query parameter value from the request
@RequestParam - must be passed as method parameter (optional based on the required property)
 http://localhost:8080/employee/call/7865467

 @RequestMapping(value=“/call/{callId}", method = RequestMethod.GET)
 public List<Calls> getAgentCallById(
            @PathVariable(“callId") int callId,
            @RequestParam(value = “status", required = false) String callStatus) {

    }

http://localhost:8080/app/call/7865467?status=Cancelled

@RequestMapping(value=“/call/{callId}", method = RequestMethod.GET)
public List<Calls> getAgentCallById(
            @PathVariable(“callId") int callId,
            @RequestParam(value = “status", required = true) String callStatus) {

}

可能是application/x-www-form-urlencoded midia类型将空格转换为+,而接收方将通过将+转换为空格来解码数据。更多信息请查看网址:http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1

这两个注释的行为完全相同。

只有2个特殊字符!'和'@'被注释@ pathvariable和@ requestparam接受。

为了检查和确认行为,我创建了一个只包含1个控制器的spring引导应用程序。

 @RestController 
public class Controller 
{
    @GetMapping("/pvar/{pdata}")
    public @ResponseBody String testPathVariable(@PathVariable(name="pdata") String pathdata)
    {
        return pathdata;
    }

    @GetMapping("/rpvar")
    public @ResponseBody String testRequestParam(@RequestParam("param") String paramdata)
    {
        return paramdata;
    }
}

点击以下请求,我得到了相同的响应:

localhost: 7000 / pvar /!@#$%^&*()_+-=[]{}|;':",./<>? localhost: 7000 / rpvar ?停止 =!@#$%^&*()_+-=[]{}|;':",./<>?

在两个请求中都收到了!@作为响应

@PathVariable从URI中获取一些占位符(Spring称之为URI模板) -参见Spring参考第16.3.2.2章URI模板模式 @RequestParam也可以从URI中获取一个参数——参见Spring参考16.3.3.3使用@RequestParam将请求参数绑定到方法参数

如果URL http://localhost:8080/MyApp/user/1234/invoices?date=12-05-2013在2013年12月5日获得用户1234的发票,控制器方法将如下所示:

@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/invoice是否提供用户null的发票或ID为“发票”的用户的详细信息?