java 影子实例_Spring Date Rest学习记录

Spring Data Rest

术语

HATEOAS HATEOAS(The Hypermedia As The Engine Of Application Statue)是REST架构的主要约束。“hepermedia”表示任何包含指向图片、电影、文字等资源的链接,Web是超媒体的经典例子。HATEOAS背后的思想其实非常简单,就是响应中包含指向其它资源的链接。客户端可以利用这些链接和服务器交互。 client不用事先知道服务或者工作流中不同步骤,还有client不用再为不同的资源硬编码URI了。而且服务器还可以在不破坏和客户端交互的情况下,更改URI。

准备条件

使用spring data jpa

使用说明

支持提交的格式

application/hal+json

application/json

默认返回的状态码

200 OK - for plain GET requests.

201 Created - for POST and PUT requests that create new resources.

204 No Content - for PUT, PATCH, and DELETE requests if the configuration is set to not return response bodies for resource updates (RepositoryRestConfiguration.returnBodyOnUpdate). If the configuration value is set to include responses for PUT, 200 OK will be returned for updates, 201 Created will be returned for resource created through PUT.

返回的数据实例

curl -v http://localhost:8080/

< HTTP/1.1 200 OK

< Content-Type: application/hal+json

{ "_links" : {

"orders" : {

"href" : "http://localhost:8080/orders"

},

"profile" : {

"href" : "http://localhost:8080/api/alps"

}

}

}

即在返回的数据中,包含了其他可用的api借口,方便调用,若实现的Spring data Jpa接口为 PageRepository,则还可以根据page,size等查数据

使用步骤

repository 继承 Spring data 的crud接口

public interface OrderRepository extends CrudRepository { }

当使用了资源库,则会在rest 的api中自动实现了一些固定的api。如/orders、/orders/{id}等对应有数据库实现的方法。

在repository 接口上添加注解@RepositoryRestResource

只有添加了注解,才能被spring data rest扫描并实现

可以在资源库的方法上使用@RestResource注解来指定方法实现rest的路径

可以使用@Param指定方法中的参数对应rest中的参数名

Spring Data Rest 对各种操作支持的Request方法

对于请求资源的操作(只支持get和post方法,如果用其他的方法,则会返回405 Method Not Allowed)

若在方法(List findByLastName(@Param("kname") String name);)上使用了@RestResource(exported =false) 则默认不向外界暴漏接口。

也可以添加查询的参数,如page,size等,具体得根据返回的可用url来使用

post方法大体与get方法一样,不过是创建一个新的对象

对于单个请求的操作(GET, PUT, PATCH and DELETE)

具体的使用,与平常的http请求一样

对于查询资源的操作(GET)

常用操作

分页和排序

repository必须继承PagingAndSortingRepository

example http://localhost:8080/people/?size=5&sort=name,desc

若要在继承的类中,实现自己的方法,也方法的参数必须加上Pageable参数,并且返回Page对象

example

http://localhost:8080/people/search/nameStartsWith

@RestResource(path = "nameStartsWith", rel = "nameStartsWith")

public Page findByNameStartsWith(@Param("name") String name, Pageable p);

{

"_links" : {

"self" : {

"href" : "http://localhost:8080/persons{&sort,page,size}",

"templated" : true

},

"next" : {

"href" : "http://localhost:8080/persons?page=1&size=5{&sort}",

"templated" : true

}

},

"_embedded" : {

... data ...

},

"page" : {

"size" : 5,

"totalElements" : 50,

"totalPages" : 10,

"number" : 0

}

}

多表关联(一对多等)问题

** 当实体类互相关联时。查询一个实体类,除了返回这个实体类的自身信息,还会返回其关联的实体类的获取url **

example

{

"_embedded": {

"people": [

{

"firstName": "Frodo",

"lastName": "Baggins",

"_links": {

"self": {

"href": "http://localhost:8080/people/1"

},

"person": {

"href": "http://localhost:8080/people/1{?projection}",

"templated": true

},

"cards": {

"href": "http://localhost:8080/people/1/cards"

}

}

}

]

},

"_links": {

"self": {

"href": "http://localhost:8080/people"

},

"profile": {

"href": "http://localhost:8080/profile/people"

},

"search": {

"href": "http://localhost:8080/people/search"

}

},

"page": {

"size": 20,

"totalElements": 1,

"totalPages": 1,

"number": 0

}

}

在people这个类中关联的card,则会返回获取url

但是当关联的实体类没有对应的repository使,则会直接返回其对象属性

example

{

"firstName" : "Frodo",

"lastName" : "Baggins",

"address" : {

"street": "Bag End",

"state": "The Shire",

"country": "Middle Earth"

},

"_links" : {

"self" : {

"href" : "http://localhost:8080/persons/1"

}

}

}

定制返回数据

可以实现一个接口,并使用@Projection注解,来自定义返回的类型,如: 实现的接口必须放在实体定义的包下,或者其子包下 RepositoryRestConfiguration.getProjectionConfiguration().addProjection(…)也可以通过这种方式来注册Projection

@Projection(name = "noAddresses", types = { Person.class })

interface NoAddresses {

String getFirstName();

String getLastName();

}

当访问people时,则会返回:

{

"firstName" : "Frodo",

"lastName" : "Baggins",

"_links" : {

"self" : {

"href" : "http://localhost:8080/persons/1{?projection}",

"templated" : true

},

"address" : {

"href" : "http://localhost:8080/persons/1/address"

}

}

}

则我们可以通过http://localhost:8080/persons/1?projection=noAddresses.这个地址来获取我们定制的返回数据

@Projection还可以提供虚拟的数据,如下

Entity

public class Person {

...

private String firstName;

private String lastName;

...

}

@Projection(name = "virtual", types = { Person.class })

public interface VirtualProjection {

@Value("#{target.firstName} #{target.lastName}")

String getFullName();

}

也可以在repository的@RepositoryRestResource中直接指定返回的数据类型

@RepositoryRestResource(excerptProjection = NoAddresses.class)

interface PersonRepository extends CrudRepository {}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值