广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Springboot HTTP如何调用其他服务
  • 288
分享到

Springboot HTTP如何调用其他服务

2024-04-02 19:04:59 288人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

目录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;
 
@Service
public class UserInfoClient {
    public String getUserTotalAmount(){
        Map<String,String> map=new HashMap<String,String>();
        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;
 
@Service
public class UserInfoClient { 
    public String getUserTotalAmount(){
        Map<String,String> map=new HashMap<String,String>();
        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<String> entity = new HttpEntity<String>(JSON.toJSONString(map),headers);
        RestTemplate restTemplate=new RestTemplate();
        ResponseEntity<String> 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请求数据

//注入
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/FaceInfo")
@ResponseBody
public 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<String, Object> requestParam = new HashMap<>();
    HttpEntity<Map<String, Object>> request = new HttpEntity<Map<String, Object>>(requestParam, headers);
        ResponseEntity<String> entity2 = restTemplate.exchange(apiURL, HttpMethod.GET, request, String.class);
    String body = entity2.getBody();
    return body;
}

使用Post方式,携带body请求数据

//注入
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/FaceInfo")
@ResponseBody
public 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<String, Object> requestParam = new HashMap<>();
    requestParam.put("loginName", "jnr");
    requestParam.put("password", "jn");
    HttpEntity<Map<String, Object>> request = new HttpEntity<Map<String, Object>>(requestParam, headers);
    String s=request.toString();
    ResponseEntity<String> entity2 = restTemplate.exchange(apiURL, HttpMethod.POST, request, String.class);
    String body = entity2.getBody();
    return body;
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: Springboot HTTP如何调用其他服务

本文链接: https://www.lsjlt.com/news/164098.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Springboot HTTP如何调用其他服务
    目录HTTP如何调用其他服务1.GET请求2.POST请求springboot请求其他服务器的http接口使用Get方式,携带headers请求数据使用Post方式,携带body请求...
    99+
    2022-11-13
  • Springboot HTTP怎么调用其他服务
    这篇“Springboot HTTP怎么调用其他服务”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Springbo...
    99+
    2023-06-29
  • SpringBoot使用Feign调用其他服务接口
    使用SpringCloud的Feign组件能够为服务间的调用节省编码时间并提高开发效率,当服务本身不复杂时可以单独将该组件拿出使用。 引入依赖 <!-- https://...
    99+
    2022-11-11
  • 怎么在SpringBoot中利用Feign调用其他服务接口
    本篇文章给大家分享的是有关怎么在SpringBoot中利用Feign调用其他服务接口,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。引入依赖<!-- https:...
    99+
    2023-06-14
  • SpringBoot如何实现其他普通类调用Spring管理的Service,dao等bean
    这篇文章将为大家详细讲解有关SpringBoot如何实现其他普通类调用Spring管理的Service,dao等bean,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。普通类调用Spring管理的Servi...
    99+
    2023-06-25
  • 聊聊如何在php中调用其他方法
    PHP(Hypertext Preprocessor)是一种广泛使用的服务器端编程语言,用于生成动态 Web 内容。在 PHP 中,调用其他方法是常见的任务,可以通过不同的方法实现。在本文中,我们将讨论如何在 PHP 中调用其他方法。方法一...
    99+
    2023-05-14
  • php方法如何在其他文件中调用
    这篇文章主要介绍“php方法如何在其他文件中调用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“php方法如何在其他文件中调用”文章能帮助大家解决问题。一、PHP方法的调用方式在了解php方法在其他文...
    99+
    2023-07-05
  • 阿里云如何用其他服务器登录
    阿里云是一款强大而灵活的云计算服务,它可以让你轻松地在自己的服务器上运行应用程序。然而,你可能有时候需要在阿里云上登录到其他服务器,这时候该怎么办呢?本文将为你详细说明如何在阿里云上用其他服务器登录。 首先,你需要在阿里云的控制台上创建一个...
    99+
    2023-11-17
    阿里 如何用 服务器
  • 小程序如何调用其他页面的函数
    在小程序中调用其他页面函数的方法可以小程序使用wx.navigateBack()方法调用上一个页面的函数,具体方法如下:let pages = getCurrentPages();let prevPage = pages[pages.len...
    99+
    2022-10-04
  • CentOS7服务器建如何安装其他软件
    这篇文章给大家分享的是有关CentOS7服务器建如何安装其他软件的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。phpRemi源,用于安装php: http://rpms.famillecollet.com/安装图...
    99+
    2023-06-26
  • 如何利用ftp定时上传log到其他服务器
    这篇文章主要介绍“如何利用ftp定时上传log到其他服务器”,在日常操作中,相信很多人在如何利用ftp定时上传log到其他服务器问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何利用ftp定时上传log到其他...
    99+
    2023-06-13
  • C++一个函数如何调用其他.cpp文件中的函数
    目录一个函数调用其他.cpp文件中的函数看示例在主文件cpp中调用其他文件函数的方法直接用extern方法总结一个函数调用其他.cpp文件中的函数 使用VC或VS创建C++项目的时候...
    99+
    2023-02-23
    C++函数调用 C++调用函数 C++调用.cpp文件函数
  • 服务之间的如何调用 HTTP代替RPC
    今天就跟大家聊聊有关服务之间的如何调用 HTTP代替RPC,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。RPC(Remote Procedure Call)—远程过程调用,它是一种通...
    99+
    2023-06-03
  • 如何将阿里云服务器IP转让给其他人使用
    阿里云服务器是一种基于云计算的虚拟化服务器,它可以提供高效稳定的网络服务,但通常情况下,服务器的IP地址是固定的。然而,如果你想要将阿里云服务器的IP地址转让给其他人使用,那么你需要了解一些相关的信息和步骤。本篇文章将会详细介绍如何将阿里云...
    99+
    2023-11-07
    阿里 如何将 给其
  • SpringBoot项目如何使用hutool工具进行http接口调用
    本文小编为大家详细介绍“SpringBoot项目如何使用hutool工具进行http接口调用”,内容详细,步骤清晰,细节处理妥当,希望这篇“SpringBoot项目如何使用hutool工具进行http接口调用”文章能帮助大家解决疑惑,下面跟...
    99+
    2023-06-30
  • 云服务器监控:如何设置监控其他设备?
    1. 了解云服务器监控功能 在开始设置之前,我们先来了解一下云服务器监控的功能。云服务器监控是一种用于监视和管理云服务器性能和状态的工具。它可以提供实时的监控数据,帮助您了解服务器的运行情况,并及时发现和解决潜在的问题。 2. 设置云服务...
    99+
    2023-10-27
    如何设置 服务器 设备
  • 阿里云备案是否可以用其他服务器?安全性如何?
    阿里云是中国领先的云计算服务提供商,其备案系统备受用户关注。用户在使用阿里云服务时,常常会遇到一个问题,那就是阿里云备案是否可以用其他服务器?这个问题的答案不仅关系到用户的使用体验,也关系到服务器的安全性。本文将对这个问题进行详细的说明。 ...
    99+
    2023-10-29
    可以用 阿里 安全性
  • 如何在springboot中使用feign实现跨服务调用
    这篇文章给大家介绍如何在springboot中使用feign实现跨服务调用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。springboot整合feign引入依赖, 这里注意, spring-cloud.version...
    99+
    2023-06-06
  • 如何在阿里云服务器上安装其他镜像文件
    在阿里云服务器上,你可以通过不同的方式安装不同的操作系统镜像文件,以满足你的不同需求。本文将详细介绍如何在阿里云服务器上安装其他镜像文件。 一、准备工作登录阿里云账号并选择相应的服务器。连接阿里云服务器,确保服务器已经在线并且可以正常使用。...
    99+
    2023-11-05
    阿里 镜像文件 器上
  • 阿里云服务器租赁市场探析如何将服务器租给其他人使用
    阿里云作为国内领先的云计算服务提供商,除了为企业和开发者提供稳定可靠的云服务器外,还提供了将服务器租给其他用户使用的服务。本文将从阿里云服务器租赁市场的角度出发,介绍如何将阿里云服务器租给其他人使用,并探讨其中的机会和挑战。1. 为什么要...
    99+
    2024-01-01
    服务器 探析 阿里
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作