Springboot|Springboot HTTP如何调用其他服务
目录
- HTTP如何调用其他服务
- 1.GET请求
- 2.POST请求
- springboot请求其他服务器的http接口
- 使用Get方式,携带headers请求数据
- 使用Post方式,携带body请求数据
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=https://www.it610.com/article/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 = "https://www.it610.com/spring")public class Test {@RequestMapping(value = "https://www.it610.com/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 = "https://www.it610.com/spring")public class Test {@RequestMapping(value = "https://www.it610.com/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 ObjectfaceInfo(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
【Springboot|Springboot HTTP如何调用其他服务】
使用Post方式,携带body请求数据
//注入@Autowiredprivate RestTemplate restTemplate; @RequestMapping("/FaceInfo")@ResponseBodypublic ObjectfaceInfo(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"); HttpEntityrequest = new HttpEntity (requestParam, headers); String s=request.toString(); ResponseEntity entity2 = restTemplate.exchange(apiURL, HttpMethod.POST, request, String.class); String body = entity2.getBody(); return body; }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
推荐阅读
- 送走 CentOS Linux 8,开发者们该如何保持 Linux 的采用途径()
- 如何修改HttpServletRequest中header中的信息
- Java如何使用while循环计算一个整数的位数
- 在阿里巴巴,我们如何先于用户发现和定位 Kubernetes 集群问题()
- python关键词对联_如何用深度学习框架PaddlePaddle实现智能春联
- V8如何在内存中快速查找对象属性(隐藏类)
- spring|Vue-admin-template+SpringBoot+MyBatisPlus实现图书的增删改查功能
- mybatis如何批量修改数据
- SpringBoot实战教程之新手入门篇
- SpringBoot项目实战之数据交互篇