RestTemplate远程调用接口方式

1.Post(body空参)

也就是说需要给一个空的json

代码:

java 复制代码
String getDeviceUrl = this.MOVABLE_URL + "detected-data/getMachineLists";
// 远程调用
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
// 请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("token",token);
// 请求体
HttpEntity<String> requestEntity = new HttpEntity<>("{}", headers);
String resultString = restTemplate.postForObject(getDeviceUrl, requestEntity,String.class);
JSONObject jo = JSONObject.fromObject(resultString);

2.Post(表单)

表单不能给json参数,不然被调用方接收不到参数

代码:

java 复制代码
String url = this.INTERFACE_CLIENT_URL + "/jkglApi/saveLog";
// 远程调用
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
// 请求头 指定Content-Type为application/x-www-form-urlencoded
HttpHeaders headers = new HttpHeaders();
//headers.setContentType(MediaType.APPLICATION_JSON);
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

// 构建参数
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
params.add("jkglApiCode", interfaceLog.getJkglApiCode());
params.add("ipAddress", interfaceLog.getIpAddress());
params.add("syncDate", interfaceLog.getSyncDate());
params.add("createDate", interfaceLog.getCreateDate());
params.add("statusSync", interfaceLog.getStatusSync().toString());

// 设置请求参数
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(params, headers);

// 发送POST请求
ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);

String resultString = response.getBody();
JSONObject jo = JSONObject.fromObject(resultString);

使用RestTemplate进行表单传参的注意事项

在使用 RestTemplate 进行表单传参时,有一些注意事项需要我们注意。

1.设置Content-Type为application/x-www-form-urlencoded

在发送POST请求时,我们需要设置请求头的Content-Type为 application/x-www-fomm-urlencoded,表示请求体的内容是表单参数。通过 HtpHeaders 类的 setContentType()方法可以实现此功能。
2.使用MultiValueMap存储表单参数

MuivalueMap 是Spring框架提供的一个接口,它继承自 Map 接口,并提供了一些额外的方法,用于存储键值对,在使用 RestTemplate 发送表单参数时,我们可以使用 MutValueMap 来存储表单参数,通过 add0 方法添加键值对。
3.使用HttpEntity封装请求体内容HtpEntty 是 RestTemplate 发送HTTP请求时的一个重要类,它用于封装请求体内容。在发送POST请求时,我们需要创建一个 HpEnity 对象,并将 MultiValueMap 对象和 HttpHeaders 对象作为参数传递进去。

3.Post(表单,方式2)

代码: 这种好像是将参数放到url后面,按理说post不应该这样,但不知道为什么,可以正常访问,被调用方也能获取,不知道为啥....

java 复制代码
String url = this.MOVABLE_URL + "users/login";
// 远程调用
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
// 请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
// 构建参数
Map<String, String> params= new HashMap();
params.put("username", MOVABLE_USERNAME);
params.put("password", MOVABLE_PASSWORD);
// 2. 构建 URL
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
params.forEach(builder::queryParam);
// 请求
ResponseEntity<String> response = restTemplate.exchange(
                    builder.build().toUri(),
                    HttpMethod.POST,
                    new HttpEntity<>(headers),
                    String.class
            );
String resultString = response.getBody();
JSONObject jo = JSONObject.fromObject(resultString);

持续更新....

相关推荐
guo_wen_qiang1 小时前
mac中docker desktop服务端开启远程访问
运维·服务器·macos·docker
云飞云共享云桌面2 小时前
医疗器械设备研发:多人同时操作 SolidWorks,怎样依靠单台服务器替代多工作站
运维·服务器·网络·数据库·制造
T型码农要学习2 小时前
开源项目5|FileBrowser:免费在线文件管理器!随时随地管控服务器文件
运维·服务器·人工智能·开源
qfljg2 小时前
OpenRewrite 实战指南
java
朱 欢 庆2 小时前
云服务器附件备份到本机内网服务器
运维·服务器·前端·经验分享
Sylvia33.2 小时前
LOL实时数据接入深度解析:从WebSocket到完整数据模型
java·网络·python·websocket·网络协议
梦想不只是梦与想2 小时前
Web 服务器网关协议:ASGI 和 WSGI
服务器·python·fastapi
智恒百亿2 小时前
8 卡 RTX 5090 服务器深度实测:256GB 显存能否支撑 70B 模型微调?选型参考
大数据·运维·服务器·人工智能
2601_962071577 小时前
【Java报错已解决】org.springframework.beans.factory.BeanCreationException
java·开发语言
支支დ8 小时前
VO by Vercel 前端特定优势:为什么它是构建 AI 应用的新范式
前端·人工智能