使用 resttemplate 调用带有数组参数的 webservice
Calling webservice with array params using resttemplate
如何使用 resttemplate 调用以下网络服务
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 "Authorization: Bearer zxAM4M90964ae" -d "{\"fileids\" : [1, 21, 22]}"
所以我假设服务器有一些控制器(你可以调整不同的部分来为你工作,这只是一个例子):
@PostMapping
public ResponseEntity somePost(@RequestBody PostRequestObject request)
{
// logic
}
为发送请求创建对应的class:
@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'
如何使用 resttemplate 调用以下网络服务
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 "Authorization: Bearer zxAM4M90964ae" -d "{\"fileids\" : [1, 21, 22]}"
所以我假设服务器有一些控制器(你可以调整不同的部分来为你工作,这只是一个例子):
@PostMapping
public ResponseEntity somePost(@RequestBody PostRequestObject request)
{
// logic
}
为发送请求创建对应的class:
@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'