添加maven依赖:org.springframework.boot spring-boot-starter-web
创建rest工具类继承RestTemplate
import org.springframework.http.HttpEntity;import org.springframework.http.HttpHeaders;import org.springframework.http.HttpMethod;import org.springframework.http.MediaType;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Component;import org.springframework.web.client.RestTemplate;import com.alibaba.fastjson.JSONObject;/** * Desc: * Author: 李阳 * mobile: 15002992382 * email:ly_triangle@126.com * Date: 2017/8/23 16:01 */@Componentpublic class ClientUtil extends RestTemplate { public JSONObject callGetMethod(String requestUrl) { HttpEntity entity = new HttpEntity(new HttpHeaders()); ResponseEntityresult = this.exchange(requestUrl, HttpMethod.GET, entity, String.class); return JSONObject.parseObject(result.getBody()); } public JSONObject callPostMethod(String requestUrl, String jsonBody) { HttpHeaders header = new HttpHeaders(); header.set("Content-Type", MediaType.APPLICATION_JSON + ";charset=UTF-8"); HttpEntity entity = new HttpEntity(jsonBody, header); ResponseEntity result = this.exchange(requestUrl, HttpMethod.POST, entity, String.class); return JSONObject.parseObject(result.getBody()); } public JSONObject callPutMethod(String requestUrl, String jsonBody) { HttpHeaders header = new HttpHeaders(); header.set("Content-Type", MediaType.APPLICATION_JSON + ";charset=UTF-8"); HttpEntity entity = new HttpEntity(jsonBody, header); ResponseEntity result = this.exchange(requestUrl, HttpMethod.PUT, entity, String.class); return JSONObject.parseObject(result.getBody()); } public JSONObject callDeleteMethod(String requestUrl) { HttpHeaders header = new HttpHeaders(); HttpEntity entity = new HttpEntity(header); ResponseEntity result = this.exchange(requestUrl, HttpMethod.DELETE, entity, String.class); return JSONObject.parseObject(result.getBody()); }
调用服务:
import com.alibaba.fastjson.JSONObject;import com.utils.ClientUtil;import com.utils.HttpEntityUtil;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.HttpEntity;import org.springframework.http.HttpMethod;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Service;/** * Author: 李阳 * Date: 16/08/2017 8:54 PM * Desc: Rest服务调用 */@Servicepublic class RestServiceInvoking { @Autowired private ClientUtil clientUtil; public JSONObject loadSth(Long id) { String requestUrl = tmisServiceUrl + "/sth/" + id; ResponseEntityresult = clientUtil.callGetMethod(requestUrl); return JSONObject.parseObject(result.getBody()); } public JSONObject addSth(String view) { String requestUrl = tmisServiceUrl + "/sth"; ResponseEntity result = clientUtil.callPostMethod(requestUrl,view); return JSONObject.parseObject(result.getBody()); } public JSONObject updateSth(String view) { String requestUrl = tmisServiceUrl + "/sth"; ResponseEntity result = clientUtil.callPutMethod(requestUrl,view); return JSONObject.parseObject(result.getBody()); } public JSONObject deleteSth(Long id) { String requestUrl = tmisServiceUrl + "/sth/" + id; ResponseEntity result = clientUtil.callDeleteMethod(requestUrl); return JSONObject.parseObject(result.getBody()); }}
被调用服务接收参数都是以json格式接受的,如:
@RequestMapping(value ="/sth" method = RequestMethod.POST)@ResponseBodypublic ResponseEntityBody saveInfo(@RequestBody view view) {……}
如果被调用服务以表单形式接受入参,如(无@RequestBody,表示表单形式接受参数):
@RequestMapping(value = "/sth", method = RequestMethod.POST)@ResponseBodypublic ResponseEntityBody saveInfo(view view) {……}
则RestTemplate使用方式变成如下形式:
public JSONObject callPostMethodByForm(String requestUrl, Mapparameters) { HttpHeaders headers = new HttpHeaders(); headers.set("Content-Type", MediaType.APPLICATION_FORM_URLENCODED + ";charset=UTF-8"); MultiValueMap params = new LinkedMultiValueMap (); if (null != parameters && !parameters.isEmpty()) { for (Map.Entry entry : parameters.entrySet()) { params.add(entry.getKey(), entry.getValue()); } } params.add("sign", "123456"); params.add("timestamp", "12345600000"); HttpEntity > requestEntity = new HttpEntity >(params, headers); ResponseEntity response = this.exchange(requestUrl, HttpMethod.POST, requestEntity, String.class); return JSONObject.parseObject(response.getBody());}