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;
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);
}