RestTemplate调用接口怎么正常返回数据?{ "ret": 0, "msg": null, "data": [ { "prtno": "162009048", "grpno": "", "grpcontno": "66200094300" } ], "success": true }入参是{ "insuredno":"100224543188" }
时间: 2023-05-11 11:05:30 浏览: 55
可以使用RestTemplate的exchange方法来调用接口,并将返回结果转换为对应的Java对象。具体代码如下:
```
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> requestEntity = new HttpEntity<>("{\"insuredno\":\"100224543188\"}", headers);
ResponseEntity<MyResponse> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, MyResponse.class);
MyResponse response = responseEntity.getBody();
```
其中,MyResponse是一个自定义的Java类,用于表示接口返回的数据结构。可以根据实际情况进行定义。
需要注意的是,url是接口的地址,可以根据实际情况进行替换。另外,如果接口需要进行身份验证等操作,还需要在headers中添加相应的信息。
相关问题
controller层怎么写,使用RestTemplate调用接口怎么正常返回类数据? url为http://10.0.162.251:8080/lis/optionalPlanRestService/UserSurveys, 除了url入参还有 "insuredno":"100224543188" 需要动态传入 返回json 为 { "ret": 0, "msg": null, "data": [ { "prtno": "162009048", "grpno": "", "grpcontno": "66200094300" } ], "success": true } 怎么正常返回类数据?
在controller层中,可以使用RestTemplate发送HTTP请求并接收响应数据。具体实现如下:
```java
@RestController
public class MyController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/getData")
public MyData getData() {
String url = "http://10.0.162.251:8080/lis/optionalPlanRestService/UserSurveys?insuredno=100224543188";
ResponseEntity<MyData> response = restTemplate.getForEntity(url, MyData.class);
return response.getBody();
}
public static class MyData {
private int ret;
private String msg;
private List<DataItem> data;
private boolean success;
// getter and setter
}
public static class DataItem {
private String prtno;
private String grpno;
private String grpcontno;
// getter and setter
}
}
```
其中,MyData和DataItem是用于封装响应数据的类。使用RestTemplate发送GET请求时,可以直接调用getForEntity方法,并传入请求URL和返回数据类型。RestTemplate会自动将响应数据转换为指定类型的对象,并返回给调用方。
controller层怎么写,使用RestTemplate调用接口怎么正常返回类数据? url为http://10.0.162.251:8080/lis/optionalPlanRestService/UserSurveys, 除了url入参还有{ "insuredno":"100224543188" } 返回json 为 { "ret": 0, "msg": null, "data": [ { "prtno": "162009048", "grpno": "", "grpcontno": "66200094300" } ], "success": true } 怎么正常返回类数据?
在controller层中,可以使用RestTemplate发送HTTP请求,然后将返回的JSON数据转换为Java对象。具体实现可以参考以下代码:
```
@RestController
public class MyController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/getData")
public MyResponse getData() {
String url = "http://10.0.162.251:8080/lis/optionalPlanRestService/UserSurveys";
Map<String, String> params = new HashMap<>();
params.put("insuredno", "100224543188");
ResponseEntity<MyResponse> responseEntity = restTemplate.getForEntity(url, MyResponse.class, params);
return responseEntity.getBody();
}
private static class MyResponse {
private int ret;
private String msg;
private List<Data> data;
private boolean success;
// getters and setters
}
private static class Data {
private String prtno;
private String grpno;
private String grpcontno;
// getters and setters
}
}
```
其中,MyResponse和Data类分别对应返回的JSON数据中的"data"和"data"数组中的元素。使用RestTemplate发送GET请求时,可以通过getForEntity方法指定URL、返回类型和URL参数。最后,通过responseEntity.getBody()方法获取返回的Java对象。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)