(030)Spring Boot之RestTemplate访问web服务案例
Posted 明月之诗
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(030)Spring Boot之RestTemplate访问web服务案例相关的知识,希望对你有一定的参考价值。
每一个springboot工程都可以看做一个服务,这也是微服务的基础,使用RestTemplate访问springboot提供的web服务。如下:
String BASE_URL="http://127.0.0.1:8080"; RestTemplate res=new RestTemplate(); String body= res.getForObject(BASE_URL+"/soa/product/20",String.class);//请求服务,返回json字符串 System.out.println(body); Gson gson=new Gson();
Response response=gson.fromJson(body,Response.class);//将json字符串转化为实体类 System.out.println(response); System.out.println(response.getCode()); System.out.println(response.getMsg()); System.out.println(response.getData());
创建过程及源码如下:
(一)首先新建一个springboot工程,mall-product
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.edu.spring.mall</groupId> <artifactId>mall-product</artifactId> <version>1.0.0</version> <name>mall-product</name> <!-- FIXME change it to the project\'s website --> <url>http://www.example.com</url> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.0.4.RELEASE</version> <scope>import</scope> <type>pom</type> </dependency> </dependencies> </dependencyManagement> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </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-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> </dependencies> </project>
Product.java
package com.edu.spring.mall.product.bean; import java.sql.Timestamp; public class Product { private Integer pid; private String pname; private String type; private double price; private Timestamp createTime; public Integer getPid() { return pid; } public void setPid(Integer pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public String getType() { return type; } public void setType(String type) { this.type = type; } public Timestamp getCreateTime() { return createTime; } public void setCreateTime(Timestamp createTime) { this.createTime = createTime; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "Product [pid=" + pid + ", pname=" + pname + ", type=" + type + ", price=" + price + ", createTime=" + createTime + "]"; } }
ProductMapper.java
package com.edu.spring.mall.product.bean; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; @Mapper public interface ProductMapper { @Insert("insert into t_products (pname,type,price) values (#{pname},#{type},#{price})") Integer add(Product product); @Delete("delete from t_products where pid=#{arg1}") Integer deleteById(Integer id); @Update("update t_products set pname=#{pname},type=#{type},price=#{price} where pid=#{pid}") Integer update(Product product); @Select("select * from t_products where pid=#{arg1}") Product getById(Integer id); @Select("select * from t_products order by pid desc ") List<Product> queryByLists(); }
Response.java
package com.edu.spring.mall.product.web; public class Response { private String code; private String msg; private Object data; public Response(String code, String msg) { this.code = code; this.msg = msg; } public Response(String code, String msg, Object data) { this.code = code; this.msg = msg; this.data = data; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } }
ProductController.java
package com.edu.spring.mall.product.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import com.edu.spring.mall.product.bean.Product; import com.edu.spring.mall.product.bean.ProductMapper; import com.edu.spring.mall.product.web.Response; @RestController public class ProductController { @Autowired private ProductMapper productMapper; @PostMapping("/soa/product/add") public Object add(Product product){ Integer res=productMapper.add(product); return res==1?new Response("200","ok"):new Response("500","fail"); } @DeleteMapping("/soa/product/{id}") public Object delete(@PathVariable("id") Integer id){ Integer res=productMapper.deleteById(id); return res==1?new Response("200","ok"):new Response("500","fail"); } @PostMapping("/soa/product/update") public Object update(Product product){ Integer res=productMapper.update(product); return res==1?new Response("200","ok"):new Response("500","fail"); } @GetMapping("/soa/product/{id}") public Object getById(@PathVariable("id") Integer id){ Product product=productMapper.getById(id); return new Response("200","OK", product); } @GetMapping("/soa/product/list") public Object list(){ return new Response("200","OK",productMapper.queryByLists()); } }
App.java
package com.edu.spring.mall.product; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main( String[] args ) { SpringApplication.run(App.class, args); } }
application.properties
spring.datasource.url=jdbc:mysql://127.0.0.1:3309/db_products spring.datasource.username=root spring.datasource.password=123456
product.sql
create database db_products default charset utf8; create table t_products(pid int not null primary key auto_increment,pname varchar(200),type varchar(10),price double,createTime timestamp)
(二)创建一个客户端工程,mall-web
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.edu.spring.web</groupId> <artifactId>mall-web</artifactId> <version>1.0.0</version> <name>mall-web</name> <!-- FIXME change it to the project\'s website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.5</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.1.0.RELEASE</version> </dependency> </dependencies> </project>
Product.java
package com.edu.spring.mall.web.bean; public class Product { private Integer pid; private String pname; private String type; private double price; private String createTime; public Integer getPid() { return pid; } public void setPid(Integer pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getCreateTime() { return createTime; } public void setCreateTime(String createTime) { this.createTime = createTime; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "Product [pid=" + pid + ", pname=" + pname + ", type=" + type + ", price=" + price + ", createTime=" + createTime + "]"; } }
Response.java
package com.edu.spring.mall.web.bean; public class Response { private String code; private String msg; private Product data; public Response(String code, String msg) { this.code = code; this.msg = msg; } public Response(String code, String msg, Product data) { this.code = code; this.msg = msg; this.data = data; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Object getData() { return data; } public void setData(Product data) { this.data = data; } @Override public String toString() { return "Response [code=" + code + ", msg=" + msg + ", data=" + data + "]"; } }
App.java
package com.edu.spring.web; import org.springframework.web.client.RestTemplate; import com.edu.spring.mall.web.bean.Response; import com.google.gson.Gson; public class App { public static void main( String[] args ) { String BASE_URL="http://127.0.0.1:8080"; RestTemplate res=new RestTemplate(); String body= res.getForObject(BASE_URL+"/soa/product/20",String.class); System.out.println(body); Gson gson=new Gson(); Response response=gson.fromJson(body,Response.class); System.out.println(response); System.out.println(response.getCode()); System.out.println(response.getMsg()); System.out.println(response.getData()); } }
(三)首先启动 mall-product,然后启动 mall-web,截图如下:
以上是关于(030)Spring Boot之RestTemplate访问web服务案例的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot 2.X - Spring Boot整合AMQP之RabbitMQ
spring boot 系列之四:spring boot 整合JPA