使用RestTemplate发送post和delete请求
来源:互联网 发布:js替换全部字符串 编辑:程序博客网 时间:2024/05/29 03:40
使用RestTemplate发送post和delete请求,其中delete请求需要特别支持eg:
RestTemplate restTemplate = new RestTemplate(); restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory() { @Override protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) { if (HttpMethod.DELETE == httpMethod) { return new HttpEntityEnclosingDeleteRequest(uri); } return super.createHttpUriRequest(httpMethod, uri); } }); HttpHeaders headers = new HttpHeaders(); MediaType type2 = MediaType.parseMediaType("application/json; charset=UTF-8"); headers.setContentType(type2); headers.add("Accept", MediaType.APPLICATION_JSON.toString()); JSONObject jsonObj = new JSONObject(); value = DigestUtils.sha1Hex(DigestUtils.sha1Hex(value)); jsonObj.put("field", field2); jsonObj.put("value", value); jsonObj.put("reportTime", (new Date()).getTime()); jsonObj.put("type", "OFFLINE"); HttpEntity<String> formEntity = new HttpEntity<String>(jsonObj.toString(), headers); JSONObject res = new JSONObject(); if(REPORT_ADD.equals(type)){ res = restTemplate.postForObject(url, formEntity, JSONObject.class); }else if(REPORT_DELETE.equals(type)){ ResponseEntity<JSONObject> exchange = restTemplate.exchange(url, HttpMethod.DELETE, formEntity, JSONObject.class); res = exchange.getBody(); } if(res.get("status_code") != null && STATE_CODE_SUCESS.equals(res.get("status_code").toString().trim())){ }else{ if(res.get("status_code") == null){ msg = "未获取到返回状态码!"; }else if(STATE_CODE_ERROR.equals(res.get("status_code").toString().trim())){ msg = "系统错误!"; }else if(STATE_CODE_INVALID.equals(res.get("status_code").toString().trim())){ msg = "该接口未授权!"; }else if(STATE_CODE_NO_MATCH.equals(res.get("status_code").toString().trim())){ msg = "未匹配到(未查找到对应项)!"; }else if(STATE_CODE_REQUEST_FORMAT_ERROR.equals(res.get("status_code").toString().trim())){ msg = "请求参数格式校验失败!"; } } return msg;
0 0
- 使用RestTemplate发送post和delete请求
- 使用RestTemplate发送post请求
- RestTemplate的get和post,put,delete请求
- 解决spring3.0.5使用RestTemplate发送post请求响应中文乱码问题
- 使用HttpURLConnection发送post和get请求
- 使用HttpURLConnection发送post和get请求
- 使用HttpURLConnection发送post和get请求
- 使用HttpURLConnection发送post和get请求
- 使用HttpURLConnection发送post和get请求
- 使用HttpURLConnection发送post和get请求
- 使用curl和wget发送post请求
- 使用HttpURLConnection发送get和post请求
- 使用NSURLSession发送get和post请求
- 使用NSURLSessionDataTask发送get和post请求
- 使用HttpClient发送GET和POST请求
- 使用URLConnection发送GET和POST请求
- python发送get/post/put/delete请求
- python 使用urllib2发送get请求和post请求
- ViewPager实现图片轮播
- 微信公众号开发环境ngrok 服务搭建
- Android多进程模式
- 【JEECG Docker安装】Docker启动报错
- Java7并发编程--4.3、运行多个任务并处理结果
- 使用RestTemplate发送post和delete请求
- 深入理解Java内存模型(四)——volatile
- 解决软键盘弹起导致gridview的布局错乱的方法(转载)
- 前端人应该知道的排序知识
- 深度学习主机环境配置: Ubuntu16.04+GeForce GTX 1080+TensorFlow
- 链表
- C++对象的动态建立和释放
- Android studio清除项目中无用的资源
- 为TreeSet集合增加比较器实现排序