ショック!!restTemplateのgetとpostリクエストはなんと...
8452 ワード
前言:
get方式の実現:
public static void main(String[] args) {
RestTemplate restTemplate = new RestTemplate();
String category = "0";
String response = restTemplate.getForObject("http://*.*.*.*:8055/showAllBusiness/" + category,String.class);
System.out.println(response);
}
getForObject url ,
token exchange
post方式の実現:
public static void main(String[] args) {
RestTemplate restTemplate = new RestTemplate();
String url = "http://*.*.*.*:3434/insuranceFeeRun"; //
// token( token )
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("token", "a2bbb914-5f46-4580-971d-dfdbcce50cdd");
//post
Map<String, Object> params = new HashMap<>();
params.put("filters",new ArrayList<String>());
params.put("pageIndex",1);
params.put("pageSize",10);
params.put("sorts",new ArrayList<String>());
String json = JSON.toJSONString(params);
HttpEntity<String> request = new HttpEntity<>(json, headers);
ResponseEntity<String> response = restTemplate.postForEntity( url, request , String.class );
System.out.println(response.getBody());
}
post token, header
Exchange方式で実現(get、post、delete、put)
リンクの実装