Spring boot2.0 入门(四)-使用RestTemplate 通信多个Spring boot工程

本文主要是介绍Spring boot2.0 入门(四)-使用RestTemplate 通信多个Spring boot工程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

有时候项目中要建立多个微服务,或者把一个大的项目拆分成多个微服务进行解耦,为了方便完成各个微服务之间相互调用,
Spring给予了模板类RestTemplate,下面介绍一下RestTemplate的几种用法:
1.使用RestTemplate进行Get请求,请求参数较少

//通过用户ID获取用户信息
public User RestTemplateGet(Long id)
{RestTemplate restTemplate = new RestTemplate();User user = restTemplate.getForObject("http://localhost:8080/users/{id}", User.class, id);return user;
}

2.使用RestTemplate进行Get请求,请求参数多且可变

//通过用户的年龄,性别,所在城市获取用户列表
public List<User> RestTemplateGet(int age,int sex,String city)
{RestTemplate restTemplate = new RestTemplate();Map<String,Object> params = new HashMap<String,Object>();params.put("age",20);params.put("sex",1);params.put("city","南京");String url = "http://localhost:8080/users/{age}/{sex}/{city}";ResponseEntity<List> responseEntity = restTemplate.getForEntity(url, List.class,params);List<User> users = responseEntity.getBody();return users;
}

3.使用RestTemplate进行POST请求,请求参数在使用JSON请求体

//使用Post新增用户
public User RestTemplatePost(User user)
{HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON_UTF8);HttpEntity<User> request = new HttpEntity<>(user,headers);RestTemplate restTemplate = new RestTemplate();User user = restTemplate.postForObject("http://localhost:8080/users", request, User.class);return user;
}

4.使用RestTemplate进行DELETE请求,请求删除操作

//使用delete删除用户
public void RestTemplateDelete(Long id)
{RestTemplate restTemplate = new RestTemplate();restTemplate.delete("http://localhost:8080/users/{id}",id);
}

5.使用RestTemplate当成普通HTTP交互

//当成普通http请求
public void RestTemplateTest()
{HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON_UTF8);HashMap<String, String> maps = new HashMap<>();maps.put("method", "keepAlive");HttpEntity<Map> request = new HttpEntity<>(maps,headers);RestTemplate rest = new RestTemplate();String str = rest.postForObject("http://localhost:8080/Engine", request, String.class);System.out.println(str);
}

这篇关于Spring boot2.0 入门(四)-使用RestTemplate 通信多个Spring boot工程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1030112

相关文章

Spring Security常见问题及解决方案

《SpringSecurity常见问题及解决方案》SpringSecurity是Spring生态的安全框架,提供认证、授权及攻击防护,支持JWT、OAuth2集成,适用于保护Spring应用,需配置... 目录Spring Security 简介Spring Security 核心概念1. ​Securit

postgresql使用UUID函数的方法

《postgresql使用UUID函数的方法》本文给大家介绍postgresql使用UUID函数的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录PostgreSQL有两种生成uuid的方法。可以先通过sql查看是否已安装扩展函数,和可以安装的扩展函数

SpringBoot+EasyPOI轻松实现Excel和Word导出PDF

《SpringBoot+EasyPOI轻松实现Excel和Word导出PDF》在企业级开发中,将Excel和Word文档导出为PDF是常见需求,本文将结合​​EasyPOI和​​Aspose系列工具实... 目录一、环境准备与依赖配置1.1 方案选型1.2 依赖配置(商业库方案)二、Excel 导出 PDF

Python实现MQTT通信的示例代码

《Python实现MQTT通信的示例代码》本文主要介绍了Python实现MQTT通信的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 安装paho-mqtt库‌2. 搭建MQTT代理服务器(Broker)‌‌3. pytho

SpringBoot改造MCP服务器的详细说明(StreamableHTTP 类型)

《SpringBoot改造MCP服务器的详细说明(StreamableHTTP类型)》本文介绍了SpringBoot如何实现MCPStreamableHTTP服务器,并且使用CherryStudio... 目录SpringBoot改造MCP服务器(StreamableHTTP)1 项目说明2 使用说明2.1

spring中的@MapperScan注解属性解析

《spring中的@MapperScan注解属性解析》@MapperScan是Spring集成MyBatis时自动扫描Mapper接口的注解,简化配置并支持多数据源,通过属性控制扫描路径和过滤条件,利... 目录一、核心功能与作用二、注解属性解析三、底层实现原理四、使用场景与最佳实践五、注意事项与常见问题六

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

Java中Arrays类和Collections类常用方法示例详解

《Java中Arrays类和Collections类常用方法示例详解》本文总结了Java中Arrays和Collections类的常用方法,涵盖数组填充、排序、搜索、复制、列表转换等操作,帮助开发者高... 目录Arrays.fill()相关用法Arrays.toString()Arrays.sort()A

Spring Boot Maven 插件如何构建可执行 JAR 的核心配置

《SpringBootMaven插件如何构建可执行JAR的核心配置》SpringBoot核心Maven插件,用于生成可执行JAR/WAR,内置服务器简化部署,支持热部署、多环境配置及依赖管理... 目录前言一、插件的核心功能与目标1.1 插件的定位1.2 插件的 Goals(目标)1.3 插件定位1.4 核

如何使用Lombok进行spring 注入

《如何使用Lombok进行spring注入》本文介绍如何用Lombok简化Spring注入,推荐优先使用setter注入,通过注解自动生成getter/setter及构造器,减少冗余代码,提升开发效... Lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter