public class RestClient {
private static final RestTemplate temp = new RestTemplate();
private static void validateDeal() throws Exception {
HttpEntity<String> entity = ClientHelper.getHttpEntity(form.toJsonString());
ResponseEntity<String> output = temp.postForEntity("url_string", entity, String.class);
List<MyObject> results = JsonConverter.convert(output.getBody(), new TypeReference<List<MyObject>>(){});
System.out.println(results);
}
private static Portfolio getPortfolio(String uid, String code) throws Exception {
ResponseEntity<String> output = temp.exchange("/test_url?uid=" + uid + "&code=" + code), HttpMethod.GET, ClientHelper.getHttpEntity(String.class), String.class);
List<MyObject> results = JsonConverter.convert(output.getBody(), new TypeReference<List<MyObject>>(){});
}
private static final RestTemplate temp = new RestTemplate();
private static void validateDeal() throws Exception {
HttpEntity<String> entity = ClientHelper.getHttpEntity(form.toJsonString());
ResponseEntity<String> output = temp.postForEntity("url_string", entity, String.class);
List<MyObject> results = JsonConverter.convert(output.getBody(), new TypeReference<List<MyObject>>(){});
System.out.println(results);
}
private static Portfolio getPortfolio(String uid, String code) throws Exception {
ResponseEntity<String> output = temp.exchange("/test_url?uid=" + uid + "&code=" + code), HttpMethod.GET, ClientHelper.getHttpEntity(String.class), String.class);
List<MyObject> results = JsonConverter.convert(output.getBody(), new TypeReference<List<MyObject>>(){});
}
}
--------------------------------------------------------简朴的分割线--------------------------------------------------------
public final class ClientHelper {
public static<T> HttpEntity<T> getHttpEntity(T object){
HttpEntity<T> entity = new HttpEntity<T>(object, getHeaders());
return entity;
}
public static<T> HttpEntity<T> getHttpEntity(Class<T> clz){
HttpEntity<T> entity = new HttpEntity<T>(getHeaders());
return entity;
}
}
本文介绍了一个使用 Java 实现 RESTful API 调用的例子,包括 POST 和 GET 请求的处理方式。展示了如何利用 RestTemplate 发送 JSON 数据,并获取响应结果。
1983

被折叠的 条评论
为什么被折叠?



