RestTemplate简单使用
RestTemplate来自import org.springframework.web.client.RestTemplate;
需要的maven依赖
1 2 3 4 | <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> |
get无参调用
接口项目代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <?xml version= "1.0" encoding= "UTF-8" ?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion> 4.0 . 0 </modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 2.7 . 1 </version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com</groupId> <artifactId>rest</artifactId> <version> 0.0 . 1 -SNAPSHOT</version> <name>rest</name> <description>Demo project for Spring Boot</description> <properties> <java.version> 1.8 </java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | package com.rest; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class RestApplication { public static void main(String[] args) { SpringApplication.run(RestApplication. class , args); } } /** * */ package com.rest.controller.front; import java.util.HashMap; import java.util.Map; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @author yourheart * @update 2022年6月28日 下午11:08:02 * @description: */ @RestController public class TestController { @GetMapping ( "/testNoParamterGet" ) public Map<String, Object> testNoParamterGet() { Map<String, Object> resultMap = new HashMap<>(); resultMap.put( "name" , "小白" ); resultMap.put( "age" , "10" ); resultMap.put( "hobby" , "羽毛球" ); resultMap.put( "address" , "东晋" ); return resultMap; } } server.port= 8001 logging.level.com.rest=debug logging.level.web=debug spring.devtools.add-properties= false |
调用方代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <?xml version= "1.0" encoding= "UTF-8" ?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion> 4.0 . 0 </modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 2.7 . 0 </version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.java</groupId> <artifactId>demo</artifactId> <version> 0.0 . 1 -SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version> 1.8 </java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | package com.java; /** * @author yourheart * @Description * @create 2022-06-20 21:14 */ import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.web.client.RestTemplate; @SpringBootTest public class DemoApplicationTests { @Test public void test(){ RestTemplate restTemplate= new RestTemplate(); String url= "http://127.0.0.1:8001//testNoParamterGet" ; String forObject = restTemplate.getForObject(url, String. class ); System.out.println(forObject); } } |
1 2 3 4 5 | server.port= 2000 logging.level.com.java=debug logging.level.web=debug spring.devtools.add-properties= false |
get有参调用
1 2 3 4 5 6 7 8 9 10 11 12 | private final static Logger log = LoggerFactory.getLogger(TestController. class ); @GetMapping ( "/testParamterGet" ) public Map<String, Object> testParamterGet(String param) { log.info( "【入参】param:{}" , param); Map<String, Object> resultMap = new HashMap<>(); resultMap.put( "name" , "小红" ); resultMap.put( "age" , "20" ); resultMap.put( "hobby" , "网球" ); resultMap.put( "address" , "唐朝" ); return resultMap; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | package com.java; /** * @author yourheart * @Description * @create 2022-06-20 21:14 */ import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.web.client.RestTemplate; @SpringBootTest public class DemoApplicationTests { @Test public void test(){ RestTemplate restTemplate= new RestTemplate(); String url= "http://127.0.0.1:8001//testParamterGet?param={param}" ; String forObject = restTemplate.getForObject(url, String. class , "aaa" ); System.out.println(forObject); } } |
get请求多个参数
1 2 3 4 5 6 7 8 9 10 | @GetMapping ( "/testParamterMapGet/{id}/{name}" ) public Map<String, Object> testParamterMapGet( @PathVariable String id, @PathVariable String name) { log.info( "【入参】id:{},name:{}" , id, name); Map<String, Object> resultMap = new HashMap<>(); resultMap.put( "name" , "小红" ); resultMap.put( "age" , "20" ); resultMap.put( "hobby" , "网球" ); resultMap.put( "address" , "唐朝" ); return resultMap; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | package com.java; /** * @author yourheart * @Description * @create 2022-06-20 21:14 */ import com.java.bean.TestBean; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.web.client.RestTemplate; import java.util.HashMap; import java.util.Map; @SpringBootTest public class DemoApplicationTests { @Test public void test(){ RestTemplate restTemplate= new RestTemplate(); String url= "http://127.0.0.1:8001//testParamterMapGet/{id}/{name}" ; Map<String, String> params = new HashMap<String, String>(); params.put( "id" , "1" ); params.put( "name" , "admin" ); String forObject = restTemplate.getForObject(url, String. class ,params); System.out.println(forObject); } } |
post请求多个参数
1 2 3 4 5 6 7 8 9 10 | @PostMapping ( "/testParamterBeanPost" ) public Map<String, Object> testParamterBeanPost( @RequestBody TestBean param) { log.info( "【入参】param:{}" , param); Map<String, Object> resultMap = new HashMap<>(); resultMap.put( "name" , "小红" ); resultMap.put( "age" , "20" ); resultMap.put( "hobby" , "网球" ); resultMap.put( "address" , "唐朝" ); return resultMap; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | /** * */ package com.rest.bean; /** * @author yourheart * @update 2022年6月28日 下午11:56:52 * @description: */ public class TestBean { private String user; private String pwd; /** * @return the user */ public String getUser() { return user; } /** * @param user the user to set */ public void setUser(String user) { this .user = user; } /** * @return the pwd */ public String getPwd() { return pwd; } /** * @param pwd the pwd to set */ public void setPwd(String pwd) { this .pwd = pwd; } @Override public String toString() { return "TestBean [user=" + user + ", pwd=" + pwd + "]" ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package com.java; /** * @author yourheart * @Description * @create 2022-06-20 21:14 */ import com.java.bean.TestBean; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.web.client.RestTemplate; import java.util.HashMap; import java.util.Map; @SpringBootTest public class DemoApplicationTests { @Test public void test() { RestTemplate restTemplate = new RestTemplate(); String url = "http://127.0.0.1:8001/testParamterBeanPost" ; TestBean testBean = new TestBean(); testBean.setUser( "admin" ); testBean.setPwd( "123" ); String postForObject = restTemplate.postForObject(url, testBean, String. class ); System.out.println(postForObject); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | /** * */ package com.java.bean; /** * @author yourheart * @update 2022年6月28日 下午11:56:52 * @description: */ public class TestBean { private String user; private String pwd; /** * @return the user */ public String getUser() { return user; } /** * @param user the user to set */ public void setUser(String user) { this .user = user; } /** * @return the pwd */ public String getPwd() { return pwd; } /** * @param pwd the pwd to set */ public void setPwd(String pwd) { this .pwd = pwd; } @Override public String toString() { return "TestBean [user=" + user + ", pwd=" + pwd + "]" ; } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异