为了账号安全,请及时绑定邮箱和手机立即绑定

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

标签:
Java

一、环境搭建

  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。

  1. 创建实体类

    
    @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;
    }

}


 3. 创建UserRepository

/**

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

}

4. 创建启动类和核心配置文件
启动类

@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数据库,在这里配置相应的数据库源和驱动即可)
到这里,一个简单的环境搭建完毕
![这里写图片描述](http://img.blog.csdn.net/20170106102128407?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc291bF9jb2Rl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
二、实战测试

 1. 搭建测试环境
测试时推荐使用一个神奇Google的postman,用户测试RESTFul接口非常方便,[postman CSDN下载地址](http://download.csdn.net/detail/soul_code/9730303),下载之后解压到任一目录,进入Google浏览器的拓展程序,点击加载已解压的拓展程序,找到你刚才的解压目录即可
 2. 启动项目
 SpringBootApplication类中右键Run as即可
 ![这里写图片描述](http://img.blog.csdn.net/20170106103508828?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc291bF9jb2Rl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
看到这里Logo且项目为报错则启动成功
 3. RESTFul接口测试
     ①POST 新增User
     ![这里写图片描述](http://img.blog.csdn.net/20170106103810431?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc291bF9jb2Rl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
   同样方式新增几天测试数据
   ②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更新数据
![这里写图片描述](http://img.blog.csdn.net/20170106105628269?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc291bF9jb2Rl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
或者
![这里写图片描述](http://img.blog.csdn.net/20170106105823412?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc291bF9jb2Rl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
④DELETE删除数据
![这里写图片描述](http://img.blog.csdn.net/20170106105940538?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc291bF9jb2Rl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)

测试完毕
最后再看一下项目结构
![这里写图片描述](http://img.blog.csdn.net/20170106102128407?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc291bF9jb2Rl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
这里只是简单的编写了三个类,除去配置类和实体类,其实我们就写了两行代码,就实现了RESTFul风格的接口,这就是Spring Data REST的魅力所在。当然实际生成环境不会那么简单,还需要一些额外的处理,比如在返回的数据中,password这类字段是不应该返回的,DELETE 操作,我们实际需求不是硬删除,SAVE操作之前需要做相应的数据校验和数据格式的转换等等,这类定制化的操作,Spring Data REST也提供了相应的支持。在接下来的篇幅为会大家详细介绍,由于博主水平有限,如有不足,请斧正,谢谢
点击查看更多内容
3人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消