使用resttemplate使用数组参数调用webservice

问题描述 投票:0回答:1

如何使用resttemplate调用以下Web服务

curl -v POST --cert kkk.cer --key kkk.private --pass kkk --cacert sslservercachain.cer“https://hello-sign.api-dev.myname.com/v1/gkscl/1p1/services/filesLocation”-H“授权:bearer zxAM4M90964ae”-d“{\”fileids \“:[1, 21,22]}“

java spring curl resttemplate query-parameters
1个回答
0
投票

所以我假设服务器有一些控制器(你可以调整差异部分为你工作它只是一个例子):

@PostMapping
public ResponseEntity somePost(@RequestBody PostRequestObject request)
{
    // logic
}

为发送请求创建相应的类:

@Data
class PostRequestObject { 
    List<Integer> data;
}

使用resttemplate发送请求:

PostRequestObject request = new PostRequestObject();
request.setData(Arrays.asList(1,21,22));
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer " + "zxAM4M90964ae");
HttpEntity<Object> requestEntity = new HttpEntity<Object>(request, headers);

     ResponseEntity<YourResponseClass> result = restTemplate.exchange(
                "https://hello-sign.api-dev.myname.com/v1/gkscl/1p1/services/filesLocation",
                HttpMethod.POST,
                requestEntity,
                YourResponseClass.class);

在-d之后尝试这个:

 'fields[]=1&fields[]=21&fields[]=22'
© www.soinside.com 2019 - 2024. All rights reserved.