Springboot HTTP怎么调用其他服务
发表于:2025-11-11 作者:千家信息网编辑
千家信息网最后更新 2025年11月11日,这篇"Springboot HTTP怎么调用其他服务"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一
千家信息网最后更新 2025年11月11日Springboot HTTP怎么调用其他服务
这篇"Springboot HTTP怎么调用其他服务"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇"Springboot HTTP怎么调用其他服务"文章吧。
HTTP如何调用其他服务
1.GET请求
1.1Client代码
import com.alibaba.fastjson.JSON;import org.springframework.stereotype.Service;import org.springframework.web.client.RestTemplate;import org.springframework.web.util.UriComponentsBuilder;import java.net.URI;import java.util.HashMap;import java.util.Map; @Servicepublic class UserInfoClient { public String getUserTotalAmount(){ Map map=new HashMap(); map.put("name","123"); map.put("password","123"); URI uri = UriComponentsBuilder.fromHttpUrl("http://localhost:8081/spring/test") .queryParam("jsonString",JSON.toJSONString(map)) .queryParam("token","12122222111") .build().encode().toUri(); RestTemplate restTemplate=new RestTemplate(); String data=restTemplate.getForObject(uri,String.class); System.out.println(data); return null; } public static void main(String[] args){ UserInfoClient c=new UserInfoClient(); c.getUserTotalAmount(); }} 1.2 Service 代码
import org.springframework.web.bind.annotation.*; @RestController@RequestMapping(value = "/spring")public class Test { @RequestMapping(value = "/test",method = RequestMethod.GET) public String testSpringBoot(@RequestParam String jsonString,@RequestParam String token){ System.out.println(jsonString); System.out.println(token); /* *逻辑处理 */ return "Spring Boot 测试成功!"; }}2.POST请求
2.1Client代码
import com.alibaba.fastjson.JSON;import org.springframework.http.*;import org.springframework.stereotype.Service;import org.springframework.web.client.RestTemplate;import java.util.HashMap;import java.util.Map; @Servicepublic class UserInfoClient { public String getUserTotalAmount(){ Map map=new HashMap(); map.put("name","123"); map.put("password","123"); String url="http://localhost:8081/spring/test"; //设置请求头信息 HttpHeaders headers = new HttpHeaders(); MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8"); headers.setContentType(type); headers.add("Accept", MediaType.APPLICATION_JSON.toString()); //设置body部分 HttpEntity entity = new HttpEntity(JSON.toJSONString(map),headers); RestTemplate restTemplate=new RestTemplate(); ResponseEntity result = restTemplate.exchange(url, HttpMethod.POST, entity, String.class); System.out.println(result.getBody()); return null; } public static void main(String[] args){ UserInfoClient c=new UserInfoClient(); c.getUserTotalAmount(); }} 2.2 Service代码
import org.springframework.web.bind.annotation.*; @RestController@RequestMapping(value = "/spring")public class Test { @RequestMapping(value = "/test",method = RequestMethod.POST) public String testSpringBoot(@RequestBody UserBean userBean){ System.out.println(userBean); /* *逻辑处理 */ return "Spring Boot 测试成功!"; }}springboot请求其他服务器的http接口
使用Get方式,携带headers请求数据
//注入@Autowiredprivate RestTemplate restTemplate;@RequestMapping("/FaceInfo")@ResponseBodypublic Object faceInfo(String startTime,String endTime,Integer size ){ String apiURL = "http://www.05un.cn/wspp/GceGroups"; HttpHeaders headers = new HttpHeaders(); headers.add("userId","38"); // headers.set("userId","38"); headers.setContentType(MediaType.APPLICATION_JSON); Map requestParam = new HashMap<>(); HttpEntity 使用Post方式,携带body请求数据
//注入@Autowiredprivate RestTemplate restTemplate;@RequestMapping("/FaceInfo")@ResponseBodypublic Object faceInfo(String startTime,String endTime,Integer size ){ String apiURL = "http://www.0531yun.cn/wsjc/app/Login"; HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); Map requestParam = new HashMap<>(); requestParam.put("loginName", "jnr"); requestParam.put("password", "jn"); HttpEntity> request = new HttpEntity>(requestParam, headers); String s=request.toString(); ResponseEntity entity2 = restTemplate.exchange(apiURL, HttpMethod.POST, request, String.class); String body = entity2.getBody(); return body;} 以上就是关于"Springboot HTTP怎么调用其他服务"这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注行业资讯频道。
服务
内容
代码
成功
数据
文章
方式
知识
篇文章
逻辑
处理
测试
价值
信息
大部分
就是
接口
更多
服务器
步骤
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
数据库数据异常有几种
为什么数据库软件很慢
国内待遇好的服务器测试公司
服务器安全审计报告
数据库中有学院表和成绩表
我的世界pvp服务器怎么用
Domino服务器安全日志
进行文献传递的数据库是
防微杜渐共建网络安全黑板报
计算机网络技术实操题
服务器网络要求
日本网络安全市场规模
雄安网络安全会议
vb 项目数据库配置
软件开发的职称怎么考
G卡服务器
计算机网络技术王协瑞第六章
ps4港服代理服务器
网络安全定义属性
浪潮服务器产地
沈阳软件开发公司哪个好
string数据库缺陷
中兴网络技术工程师要外派
征文网络安全1200
当今网络技术基础
国睿科技工业互联网
工作都用什么软件开发java
网易云数据库失败
oracle数据库常识总结
互联网大数据库