String url="自己的地址";
Map<String,String> map=new HashMap<>();
map.put("x-uid","1");
JSONObject jsonObject = new JSONObject();
jsonObject.put("id","参数");
ResponseData post = httpService.post(url, map, jsonObject);
String url="自己的地址";
Map<String,String> map=new HashMap<>();
map.put("x-uid","1");
ResponseData get = httpService.get(url, map);
String url="自己的地址";
Map<String,String> map=new HashMap<>();
map.put("x-uid","1");
JSONObject jsonObject = new JSONObject();
jsonObject.put("tyoe","参数");
jsonObject.put("id","参数");
ResponseData put = httpService.put(url, map, jsonObject);
String url="自己的地址";
Map<String,String> map=new HashMap<>();
map.put("x-uid","1");
JSONObject jsonObject = new JSONObject();
jsonObject.put("type","参数");
jsonObject.put("id","参数");
ResponseData delete = httpService.delete(url, map, jsonObject);
public class HttpService {
@Autowired
private RestTemplate restTemplate;
public ResponseData get(String url, Map<String, String> headerMap) {
HttpHeaders httpHeaders = new HttpHeaders();
for (Map.Entry<String, String> stringStringEntry : headerMap.entrySet()) {
httpHeaders.add(stringStringEntry.getKey(), stringStringEntry.getValue());
}
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
httpHeaders.setContentType(type);
HttpEntity httpEntity = new HttpEntity(httpHeaders);
ResponseEntity<ResponseData> result1 = restTemplate.exchange(url, HttpMethod.GET, httpEntity, ResponseData.class);
ResponseData data = result1.getBody();
return data;
}
public ResponseData post(String url, Map<String, String> headerMap, JSONObject jsonObject) {
HttpHeaders httpHeaders = new HttpHeaders();
for (Map.Entry<String, String> stringStringEntry : headerMap.entrySet()) {
httpHeaders.add(stringStringEntry.getKey(), stringStringEntry.getValue());
}
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
httpHeaders.setContentType(type);
HttpEntity httpEntity = new HttpEntity(jsonObject, httpHeaders);
ResponseData result = restTemplate.postForObject(url, httpEntity, ResponseData.class);
return result;
}
public ResponseData put(String url, Map<String, String> headerMap, JSONObject jsonObject) {
HttpHeaders httpHeaders = new HttpHeaders();
for (Map.Entry<String, String> stringStringEntry : headerMap.entrySet()) {
httpHeaders.add(stringStringEntry.getKey(), stringStringEntry.getValue());
}
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
httpHeaders.setContentType(type);
HttpEntity httpEntity = new HttpEntity(jsonObject, httpHeaders);
ResponseEntity<ResponseData> exchange = restTemplate.exchange(url, HttpMethod.PUT, httpEntity, ResponseData.class);
ResponseData result = exchange.getBody();
return result;
}
public ResponseData delete(String url, Map<String, String> headerMap, JSONObject jsonObject) {
HttpHeaders httpHeaders = new HttpHeaders();
for (Map.Entry<String, String> stringStringEntry : headerMap.entrySet()) {
httpHeaders.add(stringStringEntry.getKey(), stringStringEntry.getValue());
}
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
httpHeaders.setContentType(type);
HttpEntity httpEntity = new HttpEntity(jsonObject, httpHeaders);
ResponseEntity<ResponseData> exchange = restTemplate.exchange(url, HttpMethod.DELETE, httpEntity, ResponseData.class);
ResponseData result = exchange.getBody();
return result;
}