Jersey RESTful Services:Resources&回应 [英] Jersey RESTful Services: Resources & Responses
问题描述
我看到很多基于泽西岛的网络服务包含1 + WebResources
有1个以上的端点/方法,如下所示:
I see a lot of Jersey-based web services consisting of 1+ WebResources
that have 1+ endpoints/methods like so:
package com.myws.fizz;
public class FizzResource {
@GET
@Path("/fizz/{id}")
public Response getFizzById(@PathParam("id") Long id) {
// ...etc.
}
@GET
@Path("/fizz")
public Fizz getFizzByFoo(Foo foo) {
// ...etc.
}
}
package com.myws.buzz;
public class BuzzResource {
@POST
@Path("/buzz")
public Response createBuzz(Buzz buzz) {
// ...etc.
}
}
- 我很困惑泽西岛认为是资源。 资源和数据库之间是否存在关系?一张桌子? POJO?
- 什么时候返回
响应
与POJO?看看我上面的2getFizz
方法。我何时返回Fizz
,何时返回响应
? - I'm confused with what Jersey considers a "resource". Is there a relationship between a "resource" and a database? A table? A POJO?
- When do you return a
Response
vs. a POJO? Look at my 2getFizz
methods above. When do I return aFizz
and when do I return aResponse
?
推荐答案
术语资源并不仅仅是泽西语术语,因为它是一个REST术语。在处理REST时,我们有资源和表示。资源可以是任何东西,在这种情况下,它是位于服务器上的某个对象,具有URL位置。当客户端请求资源时,我们会发回它的表示。你问:
The term "Resource" isn't so much just a Jersey term, as it is a REST term. When dealing with REST, we have Resources and Representations. A resource can be anything, and in this context, it is some object located on the server, with a URL location. When a client requests for a resource, we send back a representation of it. You ask:
资源和数据库之间是否存在关系?一张桌子?一个POJO?
Is there a relationship between a "resource" and a database? A table? A POJO?
它可能是一个数据库(这是一件事)。我们可以简单地将其表示为具有数据库名称的JSON对象。它也可以成为一个表(这是一个东西)。我们可以将它表示为具有名称和列名称的JSON对象。 可能是表中的一行,我们可以使用JSON对象表示该行,其中列名称为键,行值为JSON值。它可以是一个网页,一个图像,等等。所以希望你明白资源可以任何。我们发回的是它的代表。
It could be a database (that's a thing). And we could simply represent it as a JSON object with the name of the database. Also it could be a table (that's a thing). And we could represent it as a JSON object with the name and column names. It could be a row in a table, and we could represent that with a JSON object with the column names as the keys and the row values as the JSON values. It could be a web page, an image, whatever. So hopefully you get the point that a resource can be anything. What we send back is its representation.
但是术语资源不仅限于将某些内容返回给客户端请求。客户端还可以发送我们的资源表示,比如创建新资源(POST)或更改现有资源(PUT)。客户端可以在我们的数据库中向我们发送行(资源)的JSON表示。
But the term resource is not just limited to returning something to a client request. The client could also send us a representation of a resource, to say create a new resource (POST) or change an existing resource (PUT). The client may send us a JSON representation of a row (resource) in our database.
什么时候返回
响应
与POJO?看看我上面的2getFizz
方法。我何时返回Fizz
,何时返回响应
?
When do you return a
Response
vs. a POJO? Look at my 2getFizz
methods above. When do I return aFizz
and when do I return aResponse
?
返回响应
允许您微调响应
。当客户提出请求时,他们总是会收到回复。响应具有标头和实体主体。当资源方法的返回类型是 Fizz
时,您说实体主体类型将是 Fizz
类型。当方法返回时,实际发生的是 Fizz
对象不是直接返回给请求客户端,所有这些都是自己的。在幕后,它被包含在 Response
中,并被发送回客户端。框架将设置它认为合适的标题。
Returning Response
allows you to fine tune the Response
. When the client make a request, they always get back a response. Responses have headers, and entity bodies. When the return type of the resource method is Fizz
, you are saying the entity body type will be a Fizz
type. When the method returns, what actually happens is that the Fizz
object isn't directly returned to the requesting client, all by itself. Behind the scenes, it gets wrapped in a Response
that get's sent back to the client. The framework will set the headers it feels appropriate.
因此我们是否决定返回 Response
或 Fizz
,它将包含在 Response
中。就像我说的那样,当我们返回一个 Response
时,它允许我们微调 Response
,添加我们自己的头文件,状态代码等。例如,假设有人制作 POST
。你可以这样做
So whether we decide to return a Response
or Fizz
, it will get wrapped in a Response
. And like I said, when we return a Response
, it allows us to fine tune the Response
, adding our own headers, status codes and such. For example, say someone make a POST
. You could do something like
@POST
@Path("/buzz")
@Produces(...)
public Response createBuzz(Buzz buzz, @Context UriInfo uriInfo) {
int buzzID = // create buzz and get the resource id
UriBuilder builder = uriInfo.getAbsolutePathBuilder();
builder.path(Integer.toString(buzzId)); // concatenate the id.
return Response.created(builder.build()).build();
}
基本上这是创建资源,比如在数据库中,我们获得返回ID。我们可以使用id连接到id的URI,这将是新的资源位置。就 Response
而言, .created(...)
表示状态代码应为 201已创建
,以及我们传递给创建的
方法的值是新创建的资源的位置。此位置将设置为响应中的位置
标头。因此,假设POST请求的路径是 http://blah.com/buzz
。我们发回的是 http://blah.com/buzz/100
,其中 100
是 buzzId
,这个完整的URL是我们如何访问buzz资源的方式,用GET请求者来说是一个用 @GET @PATH注释的资源方法(/ buzz / {id})
Basically what this does is create the resource, say in the database, and we get a return id. We can use the id to concatenate to the URI to the id, this will be the new resource location. In terms of the Response
, .created(...)
is saying that the status code should be 201 Created
, and the value we pass to the created
method is the location of the newly created resource. This location will be set as the Location
header in the response. So lets say that the path to the POST request is http://blah.com/buzz
. What we would send back is http://blah.com/buzz/100
, where 100
is the buzzId
, and this complete URL is how we will access the buzz resource say with a GET requesst to a resource method annotated with say @GET @PATH("/buzz/{id}")
就 GET
而言, 回复
,我们可以做到
In terms of a GET
, with a Response
, we could do
Fizz newFizz = fizzService.getFizz();
return Response.ok(newFizz).build(); // sends the Fizz as the entity body
这与返回<实际上并没有多大区别来自该方法的code> newFizz ,因为我们没有对 Response
做任何特别的事情。我们只是说状态代码应该是 200 OK
并附加实体主体。这是成功的GET请求通常会发生的情况。因此,如果我们只返回 Fizz
,而不是响应
,在成功获取GET的情况下,框架将隐式附加200 OK状态。
This is really not so much different than just returning the newFizz
from the method, because we aren't doing anything special with the Response
. We're just saying the status code should be 200 OK
and attaching the entity body. This is what normally happens with a successful GET request. So if we just return the Fizz
, instead of a Response
, in the case of a successful GET, the framework will implicitly attach a 200 OK status.
就个人而言,我更喜欢返回回复
,因为微调因素。
Personally, I prefer to return Responses
, because of the fine tuning factor.
这篇关于Jersey RESTful Services:Resources&回应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!