使用JPA Hibernate实体定义Rest API - LAZY和EAGER [英] Define a Rest API with JPA Hibernate Entities - LAZY and EAGER
问题描述
如何为获取JPA实体的特定关系定义RESTFul Web服务API的最佳方法是什么?
How is the best approach to define a RESTFul web service API for fetch specific relation of a JPA entity?
示例:
如果a有资源用户带有属性角色(1-N关系)
If a have a resource User with an atribute Roles (1-N relations)
我想要一些时间来调用我的资源getUserByName(我这样做)不想带来关系,因为性能)和getUserByNameWithRoles(这里我想要关于evict双网络旅行)
I would like some times to call my resource getUserByName (I do not want to bring the relations because performance) and getUserByNameWithRoles (here I want the relation for evict double network trip)
如何用java休息来获得这个?
How is the best way to get this with java rest?
@Path("/user")
class UserResource{
@GET
@Path("/{name}")
public Response getUserByName(@PathParam("name") String name){
// hibernate query: select u from User u where u.name = :name
}
@GET
// How I map this URL?
@Path("/{name}")
public Response getUserByNameWithRoles(@PathParam("name") String name){
// hibernate query: select u from User u inner join fetch u.roles where u.name = :name
}
}
1)有两种方法吗?
1) Have 2 methods?
2)使用一些扩展技巧,使用@QueryParam(确实存在任何框架或者手动)
2) Use some "expand" trick, with a @QueryParam (does exist any framework for this or it is by hand)
你的家伙如何解决这个问题?
How your guys are solving this?
推荐答案
使用查询参数
您可以使用一个支持查询参数的方法来为您提供加载角色的可能性:
Using a query parameter
You could have a single method that supports a query parameter to give you the possibility of loading the roles:
@GET
@Path("/{name}")
@Produces("application/json")
public Response getUserByName(@PathParam("name") String name,
@QueryParam("load-roles") boolean loadRoles) {
...
}
使用子资源
或者,你可以让一个端点只返回用户的表示而另一个只返回用户角色表示的端点:
Using sub-resource
Alternatively, you could have an endpoint that returns only a representation of the user and another endpoint that return only a representation of the roles of the user:
@GET
@Path("/{name}")
@Produces("application/json")
public Response getUserByName(@PathParam("name") String name) {
...
}
@GET
@Path("/{name}/roles")
@Produces("application/json")
public Response getRolesFromUserByName(@PathParam("name") String name) {
...
}
当角色是必需的,只需对第二个端点执行请求即可返回角色。
When the roles are required, just perform a request to the second endpoint to return the roles.
或者,您可以为资源的完整表示创建自定义媒体类型,为部分表示创建自定义媒体类型。
Alternatively, you could have a custom media type for the full representation of the resource and a custom media type for the partial representation.
使用这种方法,您将拥有以下方法:
With this approach, you would have the following methods:
@GET
@Path("/{name}")
@Produces({ "application/json", "application/vnd.company.partial+json" })
public Response getUserByName(@PathParam("name") String name) {
...
}
@GET
@Path("/{name}")
@Produces("application/vnd.company.full+json")
public Response getUserByNameWithRoles(@PathParam("name") String name) {
...
}
要请求部分代表您的资源,请求将如下:
To request a partial representation of your resource, the request would be like:
GET /api/users/johndoe HTTP/1.1
Host: example.com
Accept: application/json
GET /api/users/johndoe HTTP/1.1
Host: example.com
Accept: application/vnd.company.partial+json
要请求资源的完整代表,请求如下:
To request a full representation of your resource, the request would be like:
GET /api/users/johndoe HTTP/1.1
Host: example.com
Accept: application/vnd.company.full+json
这篇关于使用JPA Hibernate实体定义Rest API - LAZY和EAGER的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!