简体   繁体   English

将多个参数从角度传递到休息服务

[英]Passing multiple parameters from angular to rest service

I am trying to pass the data to my rest services through $http.get() using URL.There are 10-12 parameters which I need to pass.我正在尝试使用 URL 通过 $http.get() 将数据传递给我的休息服务。我需要传递 10-12 个参数。

Angular script.js角脚本.js

$http.get('rest/test/getAllData/?filter1='$scope.filter1 + 
           '&filter2='$scope.filter2 + '&filter3='$scope.filter3 + 
           '&filter4='$scope.filter4)

The scope variables would come as undefined sometimes.But I guess that will not be a problem.I checked the console, the URL is getting formed correctly with all 12-13 parameters.范围变量有时会以未定义的形式出现。但我想这不会有问题。我检查了控制台,使用所有 12-13 个参数正确形成了 URL。

Now I am trying to get this in rest service using @QueryParam like below -现在我正在尝试使用@QueryParam 在休息服务中获得它,如下所示 -

@path("/getAllData")
@GET
@Produces({..}
public response getAllData(@QueryParam("filter1") final String filter1,
                           @QueryParam("filter2") final String filter2,
                           @QueryParam("filter3") final String filter3){
}

The problem is that the @QueryParam is working for 2 parameters but when I am increasing the number of parameters, it is failing.问题是@QueryParam 为 2 个参数工作,但是当我增加参数数量时,它失败了。 As per the browser console, I am getting 404 error .根据浏览器控制台,我收到404 错误

Is there any limitation to the number of parameters we can pass?我们可以传递的参数数量有什么限制吗? I need to pass 12-13 parameters.How can I achieve this?我需要传递 12-13 个参数。我怎样才能做到这一点?

Update -更新 -

I was able to resolve my issue .我能够解决我的问题。 I was using Date datatype in @QueryParam because of which it was throwing 404 error .我在 @QueryParam 中使用 Date 数据类型,因此它抛出 404 错误。 Changed it to String and it worked .将其更改为 String 并且它起作用了。 Did not know that we cannot use Date in QueryParam .不知道我们不能在 QueryParam 中使用 Date 。

Apologies for not putting the detailed problem .抱歉没有把详细的问题。

If you want to do it in the proper RESTful way then you can try like this, I have given it for couple of variables, but you can do for any.如果您想以适当的 RESTful 方式执行此操作,那么您可以像这样尝试,我已经为几个变量提供了它,但您可以为任何变量执行此操作。

@RequestMapping(path = "/rest/test/getAllData/{filter1}/{filter2}", method = RequestMethod.GET)
public Data getAllData(@PathVariable String filter1, @PathVariable String filter2) {
  // code here
}

You can also achieve it using RequestParam, an example below, ensure your parameters matches您也可以使用 RequestParam 实现它,下面是一个示例,确保您的参数匹配

@RequestMapping(path = "/rest/test/getAllData", method = RequestMethod.GET)
public Data getAllData(@RequestParam String filter1, @RequestParam String filter2) {
// code here

} }

If you still have a complicated structure then send it as a POST with the JSON data in the request body如果您仍然有一个复杂的结构,则将其作为 POST 发送,并在请求正文中包含 JSON 数据

@RequestMapping(path = "/rest/test/getAllData", method = RequestMethod.POST, consumes = "application/json")
public Book getAllData(@RequestBody ObjectKey objectKey) {
// code here

} }

For passing more number of parameters, you can use Rest parameter.要传递更多数量的参数,您可以使用 Rest 参数。 It is used to access indefinite number of arguments passed to a function.它用于访问传递给函数的无限数量的参数。

Syntax :句法 :

function add (...numbers){}

 const params = new HttpParams()
  .set('paramName1', value1)
  .set('paramNmae2', value2);
   return this.httpClient.get<IMyObject[]>(this.myUrl, {params} ).pipe(
        tap(data=>console.log('ALL:'+JSON.stringify(data))),
    catchError(this.handleError));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM