【问题标题】:How to receive parameters in GET request from Angular Application如何在 Angular 应用程序的 GET 请求中接收参数
【发布时间】:2018-06-02 07:54:10
【问题描述】:

我尝试使用 Querydsl 实现搜索过滤器。我的搜索过滤器功能运行良好。但我通过在控制器中提供硬编码值来测试这一点。

现在我想从 Angular 应用程序中获取该搜索参数。在get request中如何从angularsClientAcctId, sAcctDesc,sInvestigatorName,sClientDeptId接收这个参数

谁能告诉我怎么做?

AccountController.java

@GetMapping("/findAccountData")
public ResponseEntity<List<Tuple>> populateGridViews(String sClientAcctId, String sAcctDesc,String sInvestigatorName,String sClientDeptId) throws Exception {                  

    return  ResponseEntity.ok(accService.populateGridViews(sClientAcctId, sAcctDesc,sInvestigatorName,sClientDeptId));

}

我使用@PathVariable 尝试过这样的操作,但在这里我需要传递所有参数然后只有请求匹配,否则我得到 404

我的功能是,如果我没有传递任何参数,那么我想要所有数据,如果我只传递一个参数,然后像这样根据那个参数值进行搜索

 @GetMapping("/findAccountData/{clientAcctId}/{acctDesc}/{investigatorName}/{clientDeptId}")
    public ResponseEntity<List<Tuple>>  populateGridViews(
   @PathVariable("clientAcctId") String sClientAcctId,
   @PathVariable("acctDesc") String sAcctDesc,
   @PathVariable("investigatorName") String sInvestigatorName ,
   @PathVariable("clientDeptId") String sClientDeptId) throws Exception { 

【问题讨论】:

标签: java spring-mvc


【解决方案1】:

使用其他类型或参数,@RequestParam

控制器看起来像这样

@GetMapping("/findAccountData/")
public ResponseEntity<List<Tuple>>
populateGridViews(@RequestParam("clientAcctId") String
                          sClientAcctId, @RequestParam("acctDesc") String sAcctDesc, @RequestParam("investigatorName") String sInvestigatorName, @RequestParam("clientDeptId") String
                          sClientDeptId) throws Exception {
}

您可以使用/findAccountData/?clientAcctId=1&amp;acctDesc=desc 传递想要的变量。

【讨论】:

  • 如果我的视图不在同一个应用程序中,那么我也可以使用@RequestParam
  • 您正在提供休息端点。仅当您调用控制器方法时,或者所有请求都来自休息端点,无论调用者是否在同一个应用程序中。
【解决方案2】:

使用@PathVariable 注解从 GET 请求中获取任何查询参数值。

例如:

@GetMapping("/findAccountData/{clientAcctId}/{acctDesc}/{investigatorName}/{clientDeptId}")
public ResponseEntity<List<Tuple>> 
populateGridViews(@PathVariable("clientAcctId") String 
sClientAcctId,@PathVariable("acctDesc") String sAcctDesc,@PathVariable("investigatorName") String sInvestigatorName,@PathVariable("clientDeptId") String 
sClientDeptId) throws Exception { 

【讨论】:

  • 我试过这样但这里的问题是所有参数都需要传递然后只有请求是映射
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-30
  • 2021-12-25
  • 1970-01-01
  • 2022-01-26
相关资源
最近更新 更多