@Override
public WebApiNewResult<String> addOrUpdateUser(UserModel model, String authToken) {
var result = new WebApiNewResult<String>();
try {
if (model == null || StrUtil.isEmpty(authToken)) {
throw new Exception("userModel和token不能为空");
}
Stirng baseUrl = "127.0.0.1:8080/";
var url = baseUrl + authToken;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
headers.set("token", authToken);
var clientFactory = new SimpleClientHttpRequestFactory();
var restTemplate = new RestTemplate(clientFactory);
var typeRef = new ParameterizedTypeReference<WebApiNewResult<String>>() {
};
HttpEntity<String> requestEntity = new HttpEntity<>(JSON.toJSONString(model), headers);
return restTemplate.exchange(url, HttpMethod.POST, requestEntity, typeRef).getBody();
} catch (Exception e) {
e.getStackTrace();
result.setAllMessages(e.getMessage());
}
return result;
}
@Override
public WebApiNewResult<Boolean> deleteDepartmentOrDistributor(String id, String authToken) {
var result = new WebApiNewResult<Boolean>();
try {
if (StrUtil.isEmpty(id)) {
result.setAllMessages("机构id不能为空");
result.setData(false);
return result;
}
if (StrUtil.isEmpty(authToken)) {
result.setAllMessages("token不能为空");
result.setData(false);
return result;
}
String baseUrl = "127.0.0.1:8080/";
String url = baseUrl +"{1}/{2}";
var clientFactory = new SimpleClientHttpRequestFactory();
var restTemplate = new RestTemplate(clientFactory);
var typeRef = new ParameterizedTypeReference<WebApiNewResult<Boolean>>() {
};
return restTemplate.exchange(url, HttpMethod.GET, null, typeRef, id, authToken).getBody();
} catch (Exception e) {
e.printStackTrace();
result.setAllMessages(e.getMessage());
throw e;
}
}
使用RestTemplate调用接口
最新推荐文章于 2024-02-01 11:11:07 发布