SpringBoot--RestTemplate 小鱼儿 2022-05-22 03:56 270阅读 0赞 目录结构: ![70][] pom.xml文件: <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.roncoo.education</groupId> <artifactId>spring-boot-demo-23-1</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-boot-demo-23-1</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <!-- 数据库 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</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> application.properties文件: spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver package com.roncoo.example; public class PicInfo \{ private int id; private String name; private String url; public int getId() \{ return id; \} public void setId(int id) \{ this.id = id; \} public String getName() \{ return name; \} public void setName(String name) \{ this.name = name; \} public String getUrl() \{ return url; \} public void setUrl(String url) \{ this.url = url; \} @Override public String toString() \{ return "PicInfo \[id=" + id + ", name=" + name + ", url=" + url + "\]"; \} \} package com.roncoo.example; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.client.RestTemplate; @Controller @RequestMapping("web") public class WebController \{ @Autowired private RestTemplate restTemplate; @RequestMapping("getUser") @ResponseBody public PicInfo getUser() \{ //这里的链接就是提供数据的接口,这里可以将url写在配置文件里面,便于维护 PicInfo bean = restTemplate.getForObject("http://192.168.0.3:9999/picInfo/getById", PicInfo.class); return bean; \} \} package com.roncoo.example; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication public class SpringBootDemo231Application \{ @Autowired private RestTemplateBuilder builder; // 使用RestTemplateBuilder来实例化RestTemplate对象,spring默认已经注入了RestTemplateBuilder实例 @Bean public RestTemplate restTemplate() \{ return builder.build(); \} public static void main(String\[\] args) \{ SpringApplication.run(SpringBootDemo231Application.class, args); \} \} ![70 1][] 链接: http://localhost:8080/web/getUser ![70 2][] [70]: /images/20220522/261cc64c74c5443bbce538180e5b606d.png [70 1]: /images/20220522/a3237585acd9494884e348805cb5631d.png [70 2]: /images/20220522/c85204d693df4ae8bebb4eb3b8079009.png
还没有评论,来说两句吧...