继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Spring REST Template 快速入门

关注TA
已关注
手记
粉丝
获赞

本篇将使用Spring Boot发布REST Service,使用Spring REST Template来调用服务

本篇代码均已上传到GITHUB,https://github.com/IloveZiHan/restfultemplate.git

介绍

我们之前都使用过HttpClient来发送HTTP请求,但我们很多时间都需要将HttpClient请求或者响应的数据转换为Java对象,HttpClient需要我们自己手动来解析。我们今天要介绍的Spring REST Template比HttpClient用起来更加方便简洁。

背景

案例分为两个部分:

  1. CRM_MANAGEMENT——后台主要提供RESTFUL Service服务,因为尽量使案例简单,所以数据部分是直接使用内存中的列表来模拟

  2. PORTAL——门户,也就是前端系统,用来处理浏览器提交的请求,接收到用户请求会使用Spring REST Template来提交RESTFUL Service请求


环境说明

  • 本案例基于Maven构建,使用Spring Boot 2.x开发

  • 可以选择IDEA或者Eclipse导入案例运行

项目开发

CRM_DOMAIN实体

POM依赖

  • 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">;
    <parent>
    <artifactId>resttemplate_common</artifactId>
    <groupId>com.itheima</groupId>
    <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>crm_domain</artifactId><packaging>jar</packaging>

    </project>

  • User实体类

    package com.itheima.crm.domain;

    public class User {

    private Integer id;     // idprivate String username; // 用户名private String password; // 密码public Integer getId() {    return id;}public void setId(Integer id) {    this.id = id;}public String getPassword() {    return password;}public void setPassword(String password) {    this.password = password;}public String getUsername() {    return username;}public void setUsername(String username) {    this.username = username;}public boolean matchUsernameAndPassword(String username, String password) {    if(this.username.equals(username) && this.password.equals(password)) {        return true;    }    else {        return false;    }}

    }

ResResult——封装响应结果

package com.itheima.crm.domain;/** * 响应结果 */public class ResResult {    private Boolean success; // 是否操作成功    private String message; // 消息    public ResResult() {    }    public ResResult(Boolean success, String message) {        this.success = success;        this.message = message;    }    public Boolean getSuccess() {        return success;    }    public void setSuccess(Boolean success) {        this.success = success;    }    public String getMessage() {        return message;    }    public void setMessage(String message) {        this.message = message;    }}

CRM_MANAGEMENT后台

POM依赖

  • 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><artifactId>crm_management</artifactId><parent>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-parent</artifactId>    <version>2.0.1.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-starter-web</artifactId>    </dependency>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-devtools</artifactId>        <scope>runtime</scope>    </dependency>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-test</artifactId>        <scope>test</scope>    </dependency>    <dependency>        <groupId>com.itheima</groupId>        <artifactId>crm_domain</artifactId>        <version>0.0.1-SNAPSHOT</version>    </dependency></dependencies><build>    <plugins>        <plugin>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-maven-plugin</artifactId>        </plugin>    </plugins></build>

    </project>

RESTFUL Service接口开发

package com.itheima.crm.webservice;import com.itheima.crm.domain.ResResult;import com.itheima.crm.domain.User ;import com.itheima.crm.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;import java.util.List;@RestController@RequestMapping("/user")public class UserRestService {    @Autowired    private UserService userService;    /**     * 注册服务     * @param user 用户     * @return     */    @RequestMapping(method = RequestMethod.POST)    public ResResult regist(@RequestBody User user) {        try {            userService.regist(user);            return new ResResult(true, "添加成功");        } catch (Exception e) {            e.printStackTrace();            return new ResResult(false, "添加失败");        }    }    /**     * 登录服务     * @param username 用户名     * @param password 密码     * @return     */    @RequestMapping(value = "/{username}/{password}", method = RequestMethod.GET)    public User login(@PathVariable("username") String username, @PathVariable("password") String password) {        try {            return userService.login(username, password);        } catch (Exception e) {            return null;        }    }    @RequestMapping(method = RequestMethod.GET)    public List<User> findAll() {        return userService.findAll();    }}

Service层开发

  • UserService接口

    package com.itheima.crm.service;

    import com.itheima.crm.domain.User ;

    import java.util.List;

    public interface UserService {
    void regist(User user);

    User login(String username, String password);List<User> findAll();

    }

  • UserServiceImpl实现类

    package com.itheima.crm.service;

    import com.itheima.crm.dao.UserDao;
    import com.itheima.crm.domain.User;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    import java.util.List;

    @Service
    public class UserServiceImpl implements UserService{

    @Autowiredprivate UserDao userDao;@Overridepublic void regist(User user) {    userDao.save(user);}@Overridepublic User login(String username, String password) {    return userDao.findOne(username, password);}@Overridepublic List<User> findAll() {    return userDao.findAll();}

    }

DAO层开发

  • UserDao接口

    package com.itheima.crm.dao;

    import com.itheima.crm.domain.User ;

    import java.util.List;

    public interface UserDao {
    /**

    • 保存用户

    • @param user 用户
      */
      void save(User user);

      User findOne(String username, String password);

      List<User> findAll();
      }

  • UserDaoImpl实现类

    package com.itheima.crm.dao;

    import com.itheima.crm.domain.User;
    import org.springframework.stereotype.Repository;

    import java.util.ArrayList;
    import java.util.List;

    @Repository
    public class UserDaoImpl implements UserDao{

    private List<User> users = new ArrayList<>();@Overridepublic void save(User user) {    users.add(user);}@Overridepublic User findOne(String username, String password) {    User userTmp = new User();    userTmp.setUsername(username);    userTmp.setPassword(password);    for(User user : users) {        if(user.matchUsernameAndPassword(username, password)) {            return user;        }    }    return null;}@Overridepublic List<User> findAll() {    return users;}public UserDaoImpl() {    // 添加一些测试的用户    User user1 = new User();    user1.setId(1);    user1.setUsername("zhangsan");    user1.setPassword("0");    User user2 = new User();    user2.setId(2);    user2.setUsername("lisi");    user2.setPassword("0");    User user3 = new User();    user3.setId(3);    user3.setUsername("wangwu");    user3.setPassword("0");    User user4 = new User();    user4.setId(4);    user4.setUsername("zhaoliu");    user4.setPassword("0");    users.add(user1);    users.add(user2);    users.add(user3);    users.add(user4);}

    }

Spring Boot入口

  • 配置文件(application.properties)

    server.port=9001
    server.servlet.context-path=/

  • 入口

    package com.itheima.crm;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;

    @SpringBootApplication
    public class Application {

    public static void main(String[] args) {    SpringApplication.run(Application.class, args);}

    }

RESTFUL Service接口调用URL

  • 添加用户
    Requsest URL: http://localhost:9001/user/ Request Method: POST

  • 根据用户名密码查询用户
    Requsest URL: http://localhost:9001/user/用户名/密码 Request Method: GET

  • 查询所有用户
    Requeset URL:  http://localhost:9001/user/ Request Method: GET

PORTAL门户

POM依赖

<?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">    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>2.0.1.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <modelVersion>4.0.0</modelVersion>    <artifactId>portal</artifactId>    <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-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-devtools</artifactId>            <scope>runtime</scope>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>fastjson</artifactId>            <version>1.2.28</version>        </dependency><dependency>            <groupId>com.itheima</groupId>            <artifactId>crm_domain</artifactId>            <version>0.0.1-SNAPSHOT</version>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>

Controller开发

package com.itheima.portal.controller;import com.itheima.crm.domain.ResResult;import com.itheima.crm.domain.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.util.Collections;import java.util.HashMap;import java.util.Map;@RestController@RequestMapping("/user")public class UserController {    @Autowired    private RestTemplate restTemplate;    private String USERSERVICE_URL = "http://localhost:9001/user";    @RequestMapping("/login")    public Map login(String username, String password) {        // 构建要传递的参数        Map<String, String> params = new HashMap<>();        params.put("username", username);        params.put("password", password);        // 发送GET请求        User user = restTemplate.getForObject(USERSERVICE_URL + "/{username}/{password}", User.class, params);        Map<String, Object> result = new HashMap<>();        if(user != null) {             result.put("success", true);        }        else {            result.put("success", false);            result.put("message", "登录失败");        }        return result;    }    @RequestMapping("/regist")    public ResResult login(@RequestBody  User user) throws IOException {        // 使用REST Template发送POST请求        return restTemplate.postForObject(USERSERVICE_URL + "/", user, ResResult.class);    }}

Spring Boot入口

  • 配置文件(application.properties)

    server.port=9002
    server.servlet.context-path=/

  • 入口

    package com.itheima.portal;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;

    @SpringBootApplication
    public class Application {

    public static void main(String[] args) {    SpringApplication.run(Application.class, args);}/** * 创建RestTemplate Bean */@Beanpublic RestTemplate restTemplate() {    return new RestTemplate();}

    }

  • 前端页面

  1. 导入jquery.js

  2. login.html登录页面

    <!doctype html>
    <html lang="`">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport"
    content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>登录</title>
    <script src="jquery-1.11.3.js"></script>
    </head>
    <body>
    <table class="table">
    <tr>
    <td>用户名</td>
    <td><input type="text" id="username"></td>
    </tr>
    <tr>
    <td>密码</td>
    <td><input type="text" id="password"></td>
    </tr>
    <tr>
    <td></td>
    <td>
    <button id="btn">登录</button>
    <button id="btn1">注册</button>
    </td>
    </tr>
    </table>

    <script type="text/javascript">    $(function() {       $('#btn').click(function() {           $.get('user/login?username=' + $('#username').val() + '&password=' + $('#password').val(), function(data) {              if(!data.success) {                  alert(data.message);              }              else {                  window.location.href = "success.html";              }           });       });        $('#btn1').click(function() {            var user = {                    username: $('#username').val(),                    password: $('#password').val()            };            $.ajax({                type:'POST',                url:'user/regist',                dataType:"json",                contentType:"application/json",                data:JSON.stringify(user),                success:function(data){                    if(!data.success) {                        alert(data.message);                    }                    else {                        alert("注册成功!");                    }                }            });        });    });</script>

    </body>
    </html>

  3. success.html登录成功页面

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>登录成功</title>
    </head>
    <body>
    <h1>登录成功</h1>
    </body>
    </html>

总结

Spring REST Template比HttpClient更加快捷方便的调用RS服务,它自动封装和解析Java对象。而且可以很方便的与Spring整合,快速注入Spring的bean对象。

注意

注意Jquery $.get、$.post、$.ajax的区别

  • $.get(url, json对象)表示发送一个get请求,使用queryString的方式提交。url?username=tom...

  • $.post(url, json对象)表示发送一个post请求,数据部分类似于提交表单。

  • $.ajax可以选择数据传递的格式、类型等。我们要将一个json串发送到spring mvc @RequestBody来接收需要使用$.ajax。下面是示例代码

    $.ajax({
    type:'POST',
    url:'user/regist',
    dataType:"json",
    contentType:"application/json",
    data:JSON.stringify(user),
    success:function(data){
    if(!data.success) {
    alert(data.message);
    }
    else {
    alert("注册成功!");
    }
    }
    });

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP