RestTemplate is a client provided by Spring for accessing Rest services. RestTemplate provides a variety of convenient ways to access remote Http services, which can greatly improve the writing efficiency of the client. In the previous blog, //www.VeVB.COM/article/132885.htm, the Jersey client has been used to implement the Restful service that consumes spring boot. Next, we use RestTemplate to consume the Restful service in the previous example. The previous example:
Springboot integrates H2 memory database to achieve unit tests and database irrelevance
Restful services provided by this example are as follows: http://localhost:7900/user/1
{"id":1,"username":"user1","name":"Zhang San","age":20,"balance":100.00}
The pom file dependencies are as follows:
<?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.chhliu.springboot.restful</groupId> <artifactId>springboot-rest-template</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot-rest-template</name> <description>Demo project for Spring Boot RestTemplate</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.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.7</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> <!-- Hot boot, hot deployment dependency package, for debugging convenience, add this package --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Since we need to convert the corresponding json string into a User object after we use RestTemplate to call the Restful service, we need to copy this class to the project, as follows:
package com.chhliu.springboot.restful.vo; import java.math.BigDecimal; public class User { private Long id; private String username; private String name; private Short age; private BigDecimal balance; // ...Omit getter and setter methods/** * attention: * Details: TODO * @author chhliu * Creation time: 2017-1-20 2:05:45 pm * @return */ @Override public String toString() { return "User [id=" + id + ", username=" + username + ", name=" + name + ", age=" + age + ", balance=" + balance + "]"; } } package com.chhliu.springboot.restful.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import com.chhliu.springboot.restful.vo.User; @RestController public class RestTemplateController { @Autowired private RestTemplate restTemplate; @GetMapping("/template/{id}") public User findById(@PathVariable Long id) { // http://localhost:7900/user/ is the corresponding url of the previous service User u = this.restTemplate.getForObject("http://localhost:7900/user/" + id, User.class); System.out.println(u); return u; } } package com.chhliu.springboot.restful; 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 SpringbootRestTemplateApplication { // Be careful when starting that because we inject RestTemplate into the controller, we need to instantiate an instance of the class when starting @Autowired private RestTemplateBuilder builder; // Use RestTemplateBuilder to instantiate the RestTemplate object. Spring has injected the RestTemplate instance by default @Bean public RestTemplate restTemplate() { return builder.build(); } public static void main(String[] args) { SpringApplication.run(SpringbootRestTemplateApplication.class, args); } } Enter: http://localhost:7902/template/1 in your browser
The test results are as follows:
Console printing results:
User [id=1, username=user1, name=Zhang San, age=20, balance=100.00]
Through the above test, it is shown that we have successfully called the spring boot Restful service.
There is a very bad thing in the above test.
User u = this.restTemplate.getForObject("http://localhost:7900/user/" + id, User.class); Hard code appears here. When the server address changes, the corresponding code needs to be changed, and an improved method is required to write the address of the Restful service into the configuration file.
Modify the controller as follows:
package com.chhliu.springboot.restful.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import com.chhliu.springboot.restful.vo.User; @RestController public class RestTemplateController { @Autowired private RestTemplate restTemplate; // The url address corresponding to the Restful service @Value("${user.userServicePath}") private String userServicePath; @GetMapping("/template/{id}") public User findById(@PathVariable Long id) { User u = this.restTemplate.getForObject(this.userServicePath + id, User.class); System.out.println(u); return u; } }The configuration file is modified as follows:
server.port:7902 user.userServicePath=http://localhost:7900/user/
Start the program:
I found that the test is OK. Later, we will introduce spring cloud to further improve this calling method!
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.