@RepositoryRestResource注解的使用
<?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>org.springframework</groupId>
<artifactId>gs-accessing-data-rest</artifactId>
<version>0.1.0</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> <repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
2、Create a domain object
目录:
src/main/java/hello/Person.java
代码:
package hello; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id; @Entity
public class Person { @Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id; private String firstName;
private String lastName; public String getFirstName() {
return firstName;
} public void setFirstName(String firstName) {
this.firstName = firstName;
} public String getLastName() {
return lastName;
} public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Person
有一个firstName和lastName,主键id
配置成自动生成。
3、Create a Person repository
创建一个简单的repository类
package hello; import java.util.List; import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource; @RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> { List<Person> findByLastName(@Param("name") String name); }
PersonRepository
是一个继承PagingAndSortingRepository
的interface,然后,你就可以对Person
对象进行各种操作。
在程序运行的时候,Spring Data REST将自动创建此接口的实现。然后,它将使用@RepositoryRestResource注解让Spring MVC在/people
处创建RESTful入口点。
在这里,您还可以加入一个自定义查询,传入lastName
参数来检索Person对象的列表,稍后我会将介绍如何详细使用。
4、Make the application executable
目录:
src/main/java/hello/Application.java
代码:
package hello; 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);
}
}
这里就不需要我详细解释了吧?使用过SpringBoot的人基本知道了。
5、Test the application
运行这个Web程序,然后进入http://localhost:8080
会显示如下信息:
$ curl http://localhost:8080
{
"_links" : {
"people" : {
"href" : "http://localhost:8080/people{?page,size,sort}",
"templated" : true
}
}
}
在这里你能看到服务器提供给你的基本信息,能看到刚才我们所定义的people
所在地址http://localhost:8080/people
,它还提供了一些选项:?page
,?size
,?sort
。
注意:Spring Data REST的JSON输出格式使用的是HAL
格式。
现在我们进入这个URL地址:http://localhost:8080/people
,看下服务器是如何输出的。
$ curl http://localhost:8080/people
{
"_links" : {
"self" : {
"href" : "http://localhost:8080/people{?page,size,sort}",
"templated" : true
},
"search" : {
"href" : "http://localhost:8080/people/search"
}
},
"page" : {
"size" : 20,
"totalElements" : 0,
"totalPages" : 0,
"number" : 0
}
}
由输出可见,数据库中暂无任何Person
对象的信息,现在我们开始新建一个对象:
$ curl -i -X POST -H "Content-Type:application/json" -d '{ "firstName" : "Frodo", "lastName" : "Baggins" }' http://localhost:8080/people
HTTP/1.1 201 Created
Server: Apache-Coyote/1.1
Location: http://localhost:8080/people/1
Content-Length: 0
Date: Wed, 13 Dec 2016 14:26:55 GMT
-i 显示请求头信息
-X POST : 利用POST创建一个新的实体
-H "Content-Type:application/json" : 设置内容类型, 让应用程序知道你要传输的内容是JSON还是XML
-d '{ "firstName" : "Frodo", "lastName" : "Baggins" }' 一个数据包,要发送给应用程序的数据。
$ curl http://localhost:8080/people
{
"_links" : {
"self" : {
"href" : "http://localhost:8080/people{?page,size,sort}",
"templated" : true
},
"search" : {
"href" : "http://localhost:8080/people/search"
}
},
"_embedded" : {
"persons" : [ {
"firstName" : "Frodo",
"lastName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/1"
}
}
} ]
},
"page" : {
"size" : 20,
"totalElements" : 1,
"totalPages" : 1,
"number" : 0
}
}
persons
JSON数组现在就包含了所有Person
对象的列表。其中也包含了这条消息的self
连接地址,也就是你可以直接查询单个记录的详细信息,现在就让我们访问一下看下结果。
$ curl http://localhost:8080/people/1
{
"firstName" : "Frodo",
"lastName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/1"
}
}
}
查找所有自定义查询:
$ curl http://localhost:8080/people/search
{
"_links" : {
"findByLastName" : {
"href" : "http://localhost:8080/people/search/findByLastName{?name}",
"templated" : true
}
}
}
通过返回结果,我们能看见之前定义的findByLastName
接口,给name
参数传入一个值,就可以达到查询功能。
$ curl http://localhost:8080/people/search/findByLastName?name=Baggins
{
"_embedded" : {
"persons" : [ {
"firstName" : "Frodo",
"lastName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/1"
}
}
} ]
}
}
因为我们之前在代码里面定义的返回结果是一个List<Person>
类型,所以它将返回所有符合条件的结果。如果你在代码里定义返回结果为Person
,程序会选择一个Person对象进行返回。
除此之外,我们还可以使用PUT
、PATCH
、DELETE
请求方式,来进行替换、更新或删除现有记录的操作
$ curl -X PUT -H "Content-Type:application/json" -d '{ "firstName": "Bilbo", "lastName": "Baggins" }' http://localhost:8080/people/1
$ curl http://localhost:8080/people/1
输出内容:
{
"firstName" : "Bilbo",
"lastName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/1"
}
}
}
$ curl -X PATCH -H "Content-Type:application/json" -d '{ "firstName": "Bilbo Jr." }' http://localhost:8080/people/1
$ curl http://localhost:8080/people/1
输出内容:
{
"firstName" : "Bilbo Jr.",
"lastName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/1"
}
}
}
注意:如果你使用PUT
命令,它将替换整个记录,如果有字段没有提供值,它将置为null。PATCH
反之。
你也可以删除记录:
$ curl -X DELETE http://localhost:8080/people/1
$ curl http://localhost:8080/people
{
"_links" : {
"self" : {
"href" : "http://localhost:8080/people{?page,size,sort}",
"templated" : true
},
"search" : {
"href" : "http://localhost:8080/people/search"
}
},
"page" : {
"size" : 20,
"totalElements" : 0,
"totalPages" : 0,
"number" : 0
}
}
@RepositoryRestResource注解的使用的更多相关文章
- Spring Boot 学习(2)
文 by / 林本托 Tips 做一个终身学习的人. 源代码:github下的/code01/ch2. 配置 Web 应用程序 在上一章中,我们学习了如何创建一个基本的应用程序模板,并添加了一些基本功 ...
- SpringCloud的DataRest(一)
一.概念与定义 Spring Data Rest 基于Spring Data的repository,可以把 repository 自动输出为REST资源, 这样做的好处: 可以免去大量的 contro ...
- spring-data-rest的魔力 10分钟实现增删改查
目录 创建项目 启动项目 添加person 查看person 及 person 列表 条件查询 分页查询 controller 去哪里了 自定义 spring-data-rest 魔力之外的contr ...
- 初入spring boot(八 )Spring Data REST
1. 什么是Spring Data REST Spring Data JPA是基于Spring Data 的Repository之上,可以将Repository自动输出为REST资源.目前Spring ...
- spring 、spring boot 常用注解
@Profile 1.用户配置文件注解. 2.使用范围: @Configration 和 @Component 注解的类及其方法, 其中包括继承了 @Component 的注解: @Service. ...
- 基于spring注解AOP的异常处理
一.前言 项目刚刚开发的时候,并没有做好充足的准备.开发到一定程度的时候才会想到还有一些问题没有解决.就比如今天我要说的一个问题:异常的处理.写程序的时候一般都会通过try...catch...fin ...
- [Spring]IoC容器之进击的注解
先啰嗦两句: 第一次在博客园使用markdown编辑,感觉渲染样式差强人意,还是github的样式比较顺眼. 概述 Spring2.5 引入了注解. 于是,一个问题产生了:使用注解方式注入 JavaB ...
- Android注解使用之通过annotationProcessor注解生成代码实现自己的ButterKnife框架
前言: Annotation注解在Android的开发中的使用越来越普遍,例如EventBus.ButterKnife.Dagger2等,之前使用注解的时候需要利用反射机制势必影响到运行效率及性能,直 ...
- Android注解使用之注解编译android-apt如何切换到annotationProcessor
前言: 自从EventBus 3.x发布之后其通过注解预编译的方式解决了之前通过反射机制所引起的性能效率问题,其中注解预编译所采用的的就是android-apt的方式,不过最近Apt工具的作者宣布了不 ...
随机推荐
- All shortest paths between a set of nodes
.big{font-size:larger} .small{font-size:smaller} .underline{text-decoration:underline} .overline{tex ...
- redis 学习(二)-- 通用命令
redis 学习(二)-- 通用命令 1. keys pattern 含义:查找所有符合给定模式(pattern)的key 命令 含义 keys * 遍历所有 key keys he[h-l]* 遍历 ...
- java中的重写总结
这个理解了就挺容易的~~ 我就直接贴个程序啦 ,程序里有说明!! 父类: package ParentAndSon; public class Parent extends Object {//4:以 ...
- Js 将图片的绝对路径转换为base64编码(3)
图片文件改变一方法:$('#file').change(function(){var oFReader = new FileReader();oFReader.readAsDataURL(this.f ...
- Linux查看系统开机和关机时间
1.who 命令查看 who -b 查看最后一次系统启动的时间 who -r 查看当前系统运行时间 [root@test ~]# who -b system ...
- nginx搭建及加固
系统使用的是centos7 Nginx安装及配置 Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务 安装 我是用的环境是ce ...
- 基于SDN4.2.4的neo4j实例
首先添加maven依赖 (1)SND的声明 <dependency> <groupId>org.springframework.data</groupId> < ...
- oracle的监听控制
来自网络: listener control 监听控制 因为你在键入 lsnrctl 回车之后,就进入到监听控制界面. 在启动.关闭或者重启oracle监听器之前确保使用lsnrctl status命 ...
- BZOJ2656 [Zjoi2012]数列(sequence)[模拟]
这个递推式子可以发现$i$是偶数下标可以缩一半,是奇数下标就可以拆成两个下标,$\lfloor \frac{i}{2} \rfloor$以及$\lfloor \frac{i}{2}+1 \rfloor ...
- 【SPOJ2371】LIS2
题目大意:求二维最长上升子序列的长度. 题解: 可以看出,这个问题等价于三维偏序问题. 不过在进行分治的时候要注意,由于 dp 转移是有顺序的,因此只能先处理左半部分,再处理左半部分对右边的贡献,最后 ...