spring项目中使用RestTemplate对象调用接口

1.工具类

package com.jiankangle.util;



import com.jiankangle.dto.RequestResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;


import java.io.File;
import java.util.List;
import java.util.Map;


@Component
public class NetWorkRequest {


    private static final Logger LOG = LoggerFactory.getLogger(NetWorkRequest.class);


    @Autowired
    RestTemplate restTemplate;


    public RequestResult getDataFromJKL(String url) {
        RequestResult result;
        try {
            HttpHeaders requestHeaders = new HttpHeaders();
            requestHeaders.set("uuid", "mhealthwx");
            HttpEntity<?> requestEntity = new HttpEntity(requestHeaders);
            HttpEntity<Map> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, Map.class);
            Map<String,Object> resultMap = response.getBody();


//            Map<String, Object> resultMap = restTemplate.getForObject(url, Map.class);
            Boolean success = Boolean.parseBoolean(resultMap.get("success").toString());
            String status = resultMap.get("status").toString();
            String msg = resultMap.get("msg").toString();
            result = new RequestResult(status, success, msg, resultMap.get("data"));
        } catch (RestClientException e) {
            LOG.debug("网络异常:{}", e.getMessage());
            result = new RequestResult("400", false, "网络异常:" + e.getMessage(), null);
        }


        return result;
    }


    public RequestResult getDataFromJKLByVars(String url, Object... pathVariables) {
        RequestResult result;
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.set("uuid", "标记");
        HttpEntity<?> requestEntity = new HttpEntity(requestHeaders);
        try {
            HttpEntity<Map> response =restTemplate.exchange(url, HttpMethod.GET, requestEntity, Map.class, pathVariables);
            Map<String,Object> resultMap = response.getBody();
//            Map<String, Object> resultMap = restTemplate.getForObject(url, Map.class, pathVariables);
            Boolean success = Boolean.parseBoolean(resultMap.get("success").toString());
            String status = resultMap.get("status").toString();
            String msg = resultMap.get("msg").toString();
            result = new RequestResult(status, success, msg, resultMap.get("data"));
        } catch (RestClientException e) {
            LOG.debug("网络异常:{}", e.getMessage());
            result = new RequestResult("400", false, "网络异常:" + e.getMessage(), null);
        }


        return result;
    }




    public RequestResult getDataFromJKLByVarsPost(String url, Object... pathVariables) {
        RequestResult result;
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.set("uuid", "标记");
        HttpEntity<?> requestEntity = new HttpEntity(requestHeaders);
        try {
            HttpEntity<Map> response =restTemplate.exchange(url, HttpMethod.POST, requestEntity, Map.class, pathVariables);
            Map<String,Object> resultMap = response.getBody();
//            Map<String, Object> resultMap = restTemplate.getForObject(url, Map.class, pathVariables);
            Boolean success = Boolean.parseBoolean(resultMap.get("success").toString());
            String status = resultMap.get("status").toString();
            String msg = resultMap.get("msg").toString();
            result = new RequestResult(status, success, msg, resultMap.get("data"));
        } catch (RestClientException e) {
            LOG.debug("网络异常:{}", e.getMessage());
            result = new RequestResult("400", false, "网络异常:" + e.getMessage(), null);
        }


        return result;
    }


    public RequestResult postDataToJKL(String url, Object data) {
        RequestResult result;
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.set("uuid", "标记");
        HttpEntity<?> requestEntity = new HttpEntity(data,requestHeaders);
        try {
            HttpEntity<Map> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, Map.class);
            Map<String, Object> resultMap = response.getBody();
//            Map<String, Object> resultMap = restTemplate.postForObject(url, data, Map.class);
            Boolean success = Boolean.parseBoolean(resultMap.get("success").toString());
            String status = resultMap.get("status").toString();
            String msg = resultMap.get("msg").toString();
            result = new RequestResult(status, success, msg, resultMap.get("data"));
        } catch (RestClientException e) {
            LOG.debug("网络异常:{}", e.getMessage());
            result = new RequestResult("400", false, "网络异常:" + e.getMessage(), null);
        }


        return result;
    }




    public RequestResult uploadFile(List<File> files, String url) {
        RequestResult result;
        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        int no = 0;


        for (File file : files) {
            FileSystemResource resource = new FileSystemResource(file);
            param.add("file" + (++no), resource);
        }


        try {
            result = restTemplate.postForObject(url, param, RequestResult.class);
        } catch (Exception e) {
            result = new RequestResult();
            LOG.error("resttemplate upload error: ", e);
        }


        return result;
    }

}


2.实体类RequestResult

package com.jiankangle.dto;


import java.io.Serializable;


public class RequestResult {
    private String resultCode;


    private Boolean success;


    private String status;


    private String message;


    private Object data;


    public RequestResult() {
    }


    public RequestResult(String resultCode, Boolean success, String message) {
        this.resultCode = resultCode;
        this.success = success;
        this.message = message;
    }


    public RequestResult(String resultCode, Boolean success, String status, String message) {
        this.resultCode = resultCode;
        this.success = success;
        this.status = status;
        this.message = message;
    }


    public RequestResult(String resultCode, Boolean success, String message, Object data) {
        this.resultCode = resultCode;
        this.success = success;
        this.message = message;
        this.data = data;
    }


    public RequestResult(String resultCode, Boolean success, String status, String message, Object data) {
        this.resultCode = resultCode;
        this.success = success;
        this.status = status;
        this.message = message;
        this.data = data;
    }


    public String getResultCode() {
        return resultCode;
    }


    public void setResultCode(String resultCode) {
        this.resultCode = resultCode;
    }


    public Boolean getSuccess() {
        return success;
    }


    public void setSuccess(Boolean success) {
        this.success = success;
    }


    public String getStatus() {
        return status;
    }


    public void setStatus(String status) {
        this.status = status;
    }


    public String getMessage() {
        return message;
    }


    public void setMessage(String message) {
        this.message = message;
    }


    public Object getData() {
        return data;
    }


    public void setData(Object data) {
        this.data = data;
    }


    @Override
    public String toString() {
        return "RequestResult{" +
                "resultCode='" + resultCode + '\'' +
                ", success=" + success +
                ",status=" + status +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }

}


3.调用

接口是post请求

public RequestResult quickConsult(String wechatId, String question) {

        Map<String, Object> questionMap = new HashMap<>();

        questionMap.put("userId", user.getUserId());
        questionMap.put("patientId", user.getUserId());
        questionMap.put("question", question);

        return netWorkRequest.postDataToJKL(JKLApi.QUICK_CONSULT, questionMap);

}

接口是get

     @Override
    public RequestResult getServiceInfo(String doctorId, String typeCode, String userId) {
        return netWorkRequest.getDataFromJKLByVars(JKLApi.SERVICE_INFO, doctorId, userId, typeCode);
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值