使用RestTemplate實現rest服務的調用

添加maven依賴:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

建立rest工具類繼承RestTemplateweb

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
 * emailly_triangle@126.com
 * Date: 2017/8/23 16:01
 */
@Component
public class ClientUtil extends RestTemplate {

    public JSONObject callGetMethod(String requestUrl) {
        HttpEntity entity = new HttpEntity(new HttpHeaders());
        ResponseEntity<String> result = 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<String> 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<String> 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<String> result = this.exchange(requestUrl, HttpMethod.DELETE, entity, String.class);

        return JSONObject.parseObject(result.getBody());
    }

調用服務:spring

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服務調用
 */
@Service
public class RestServiceInvoking {

    @Autowired
    private ClientUtil clientUtil;

    public JSONObject loadSth(Long id) {
        String requestUrl = tmisServiceUrl + "/sth/" + id;
        ResponseEntity<String> result = clientUtil.callGetMethod(requestUrl);
        return JSONObject.parseObject(result.getBody());
    }

    public JSONObject addSth(String view) {
        String requestUrl = tmisServiceUrl + "/sth";
        ResponseEntity<String> result =  clientUtil.callPostMethod(requestUrl,view); 

        return JSONObject.parseObject(result.getBody());
    }

    public JSONObject updateSth(String view) {
        String requestUrl = tmisServiceUrl + "/sth";
        ResponseEntity<String> result =  clientUtil.callPutMethod(requestUrl,view); 

        return JSONObject.parseObject(result.getBody());
    }

    public JSONObject deleteSth(Long id) {
        String requestUrl = tmisServiceUrl + "/sth/" + id;
        ResponseEntity<String> result =  clientUtil.callDeleteMethod(requestUrl); 

        return JSONObject.parseObject(result.getBody());
    }
}
    

被調用服務接收參數都是以json格式接受的,如:json

@RequestMapping(value ="/sth" method = RequestMethod.POST)
@ResponseBody
public ResponseEntityBody saveInfo(@RequestBody view view) {……}

若是被調用服務以表單形式接受入參,如(無@RequestBody,表示表單形式接受參數):app

@RequestMapping(value = "/sth", method = RequestMethod.POST)
@ResponseBody
public ResponseEntityBody saveInfo(view view) {……}

則RestTemplate使用方式變成以下形式:maven

public JSONObject callPostMethodByForm(String requestUrl, Map<String, String> parameters) {
    HttpHeaders headers = new HttpHeaders();
    headers.set("Content-Type", MediaType.APPLICATION_FORM_URLENCODED + ";charset=UTF-8");
    MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
    if (null != parameters && !parameters.isEmpty()) {
        for (Map.Entry<String, String> entry : parameters.entrySet()) {
            params.add(entry.getKey(), entry.getValue());
        }
    }
    params.add("sign", "123456");
    params.add("timestamp", "12345600000");
    HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params, headers);
    ResponseEntity<String> response = this.exchange(requestUrl, HttpMethod.POST, requestEntity, String.class);

    return JSONObject.parseObject(response.getBody());
}
相關文章
相關標籤/搜索