Spring Data REST入门(二):环境搭建+实战演练

版权声明:转载请标注原文地址 https://blog.csdn.net/soul_code/article/details/54135703

Spring Data REST(一):两行代码搞定RESTFul
一、环境搭建

  1. 创建Maven项目,添加相应依赖
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
    </parent>

    <dependencies>

        <!-- 引入spring data rest -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>

        <!-- 引入JPA -->
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-data-jpa</artifactId>  
        </dependency>  

        <!-- 测试环境数据库 -->
        <dependency>  
            <groupId>com.h2database</groupId>  
            <artifactId>h2</artifactId>  
        </dependency>  

    </dependencies>

这里采用最简单的方式Spring Boot集成 Spring Data REST,并引入h2数据库作为测试数据库,使用Spring Data Jpa的默认配置为数据持久层,顺便提一句Spring Data Jpa的默认实现为Hibernate。
2. 创建实体类

@Entity
public class User {

    /**
     * 指定id为主键,并设置为自增长
     */
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @GenericGenerator(name = "increment", strategy = "increment")
    private long id;


    private String name;

    private String password;

    private int age;

    private boolean sex;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public boolean isSex() {
        return sex;
    }

    public void setSex(boolean sex) {
        this.sex = sex;
    }

}
  1. 创建UserRepository
/**
 * path="user": 映射路由
 * User:实体类
 * Long:主键
 * @author white
 *
 */
@RepositoryRestResource(path="user")
public interface UserRepository extends JpaRepository<User, Long>{

}
  1. 创建启动类和核心配置文件
    启动类
@org.springframework.boot.autoconfigure.SpringBootApplication
public class SpringBootApplication {

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

}

在Resource目录下创建application.properties

#测试数据库地址
spring.datasource.url=jdbc:h2:file:D:\\testdb 
#ddl策略
spring.jpa.hibernate.ddl-auto=update

(注:如果你使用的不是h2数据库,在这里配置相应的数据库源和驱动即可)
到这里,一个简单的环境搭建完毕
这里写图片描述
二、实战测试

  1. 搭建测试环境
    测试时推荐使用一个神奇Google的postman,用户测试RESTFul接口非常方便,postman CSDN下载地址,下载之后解压到任一目录,进入Google浏览器的拓展程序,点击加载已解压的拓展程序,找到你刚才的解压目录即可
  2. 启动项目
    SpringBootApplication类中右键Run as即可
    这里写图片描述
    看到这里Logo且项目为报错则启动成功
  3. RESTFul接口测试
    ①POST 新增User
    这里写图片描述
    同样方式新增几天测试数据
    ②GET查询
    (1)普通列表查询
    url: 127.0.0.1:8080/user
    返回数据:
{
  "_embedded": {
    "users": [
      {
        "name": "white",
        "password": "123123",
        "age": 12,
        "sex": false,
        "_links": {
          "self": {
            "href": "http://127.0.0.1:8080/user/1"
          },
          "user": {
            "href": "http://127.0.0.1:8080/user/1"
          }
        }
      },
      {
        "name": "white yu",
        "password": "123123",
        "age": 13,
        "sex": false,
        "_links": {
          "self": {
            "href": "http://127.0.0.1:8080/user/2"
          },
          "user": {
            "href": "http://127.0.0.1:8080/user/2"
          }
        }
      },
      {
        "name": "white yu11",
        "password": "123123",
        "age": 14,
        "sex": false,
        "_links": {
          "self": {
            "href": "http://127.0.0.1:8080/user/3"
          },
          "user": {
            "href": "http://127.0.0.1:8080/user/3"
          }
        }
      },
      {
        "name": "白鱼",
        "password": "123123",
        "age": 15,
        "sex": false,
        "_links": {
          "self": {
            "href": "http://127.0.0.1:8080/user/4"
          },
          "user": {
            "href": "http://127.0.0.1:8080/user/4"
          }
        }
      },
      {
        "name": "小白鱼",
        "password": "123123",
        "age": 25,
        "sex": false,
        "_links": {
          "self": {
            "href": "http://127.0.0.1:8080/user/5"
          },
          "user": {
            "href": "http://127.0.0.1:8080/user/5"
          }
        }
      },
      {
        "name": "小白",
        "password": "123123",
        "age": 25,
        "sex": false,
        "_links": {
          "self": {
            "href": "http://127.0.0.1:8080/user/6"
          },
          "user": {
            "href": "http://127.0.0.1:8080/user/6"
          }
        }
      },
      {
        "name": "小 鱼 ",
        "password": "123123",
        "age": 25,
        "sex": false,
        "_links": {
          "self": {
            "href": "http://127.0.0.1:8080/user/7"
          },
          "user": {
            "href": "http://127.0.0.1:8080/user/7"
          }
        }
      }
    ]
  },
  "_links": {
    "self": {
      "href": "http://127.0.0.1:8080/user"
    },
    "profile": {
      "href": "http://127.0.0.1:8080/profile/user"
    }
  },
  "page": {
    "size": 20,
    "totalElements": 7,
    "totalPages": 1,
    "number": 0
  }
}

(2)主键查询
url: 127.0.0.1:8080/user/2
返回数据:

{
  "name": "white yu",
  "password": "123123",
  "age": 13,
  "sex": false,
  "_links": {
    "self": {
      "href": "http://127.0.0.1:8080/user/2"
    },
    "user": {
      "href": "http://127.0.0.1:8080/user/2"
    }
  }
}

(3)分页查询
url: 127.0.0.1:8080/user?page=1&size=3
返回数据:

{
  "_embedded": {
    "users": [
      {
        "name": "白鱼",
        "password": "123123",
        "age": 15,
        "sex": false,
        "_links": {
          "self": {
            "href": "http://127.0.0.1:8080/user/4"
          },
          "user": {
            "href": "http://127.0.0.1:8080/user/4"
          }
        }
      },
      {
        "name": "小白鱼",
        "password": "123123",
        "age": 25,
        "sex": false,
        "_links": {
          "self": {
            "href": "http://127.0.0.1:8080/user/5"
          },
          "user": {
            "href": "http://127.0.0.1:8080/user/5"
          }
        }
      },
      {
        "name": "小白",
        "password": "123123",
        "age": 25,
        "sex": false,
        "_links": {
          "self": {
            "href": "http://127.0.0.1:8080/user/6"
          },
          "user": {
            "href": "http://127.0.0.1:8080/user/6"
          }
        }
      }
    ]
  },
  "_links": {
    "first": {
      "href": "http://127.0.0.1:8080/user?page=0&size=3"
    },
    "prev": {
      "href": "http://127.0.0.1:8080/user?page=0&size=3"
    },
    "self": {
      "href": "http://127.0.0.1:8080/user"
    },
    "next": {
      "href": "http://127.0.0.1:8080/user?page=2&size=3"
    },
    "last": {
      "href": "http://127.0.0.1:8080/user?page=2&size=3"
    },
    "profile": {
      "href": "http://127.0.0.1:8080/profile/user"
    }
  },
  "page": {
    "size": 3,
    "totalElements": 7,
    "totalPages": 3,
    "number": 1
  }
}

(4)分页+排序查询
url: 127.0.0.1:8080/user?page=1&size=3&sort=age,desc
返回数据

{
  "_embedded": {
    "users": [
      {
        "name": "白鱼",
        "password": "123123",
        "age": 15,
        "sex": false,
        "_links": {
          "self": {
            "href": "http://127.0.0.1:8080/user/4"
          },
          "user": {
            "href": "http://127.0.0.1:8080/user/4"
          }
        }
      },
      {
        "name": "white yu11",
        "password": "123123",
        "age": 14,
        "sex": false,
        "_links": {
          "self": {
            "href": "http://127.0.0.1:8080/user/3"
          },
          "user": {
            "href": "http://127.0.0.1:8080/user/3"
          }
        }
      },
      {
        "name": "white yu",
        "password": "123123",
        "age": 13,
        "sex": false,
        "_links": {
          "self": {
            "href": "http://127.0.0.1:8080/user/2"
          },
          "user": {
            "href": "http://127.0.0.1:8080/user/2"
          }
        }
      }
    ]
  },
  "_links": {
    "first": {
      "href": "http://127.0.0.1:8080/user?page=0&size=3&sort=age,desc"
    },
    "prev": {
      "href": "http://127.0.0.1:8080/user?page=0&size=3&sort=age,desc"
    },
    "self": {
      "href": "http://127.0.0.1:8080/user"
    },
    "next": {
      "href": "http://127.0.0.1:8080/user?page=2&size=3&sort=age,desc"
    },
    "last": {
      "href": "http://127.0.0.1:8080/user?page=2&size=3&sort=age,desc"
    },
    "profile": {
      "href": "http://127.0.0.1:8080/profile/user"
    }
  },
  "page": {
    "size": 3,
    "totalElements": 7,
    "totalPages": 3,
    "number": 1
  }
}

③PUT、POST更新数据
这里写图片描述
或者
这里写图片描述
④DELETE删除数据
这里写图片描述

测试完毕
最后再看一下项目结构
这里写图片描述
这里只是简单的编写了三个类,除去配置类和实体类,其实我们就写了两行代码,就实现了RESTFul风格的接口,这就是Spring Data REST的魅力所在。当然实际生成环境不会那么简单,还需要一些额外的处理,比如在返回的数据中,password这类字段是不应该返回的,DELETE 操作,我们实际需求不是硬删除,SAVE操作之前需要做相应的数据校验和数据格式的转换等等,这类定制化的操作,Spring Data REST也提供了相应的支持。在接下来的篇幅为会大家详细介绍,由于博主水平有限,如有不足,请斧正,谢谢

展开阅读全文

没有更多推荐了,返回首页