1. Overview
1.概述
To simplify the development of REST web services and their clients in Java, a standard and portable implementation of JAX-RS API has been designed which is called Jersey.
为了简化REST网络服务及其客户端在Java中的开发,我们设计了一个标准的、可移植的JAX-RS API的实现,它被称为Jersey。
Jersey is an open source framework for developing REST web services that provide support for JAX-RS APIs and serves as a JAX-RS reference implementation.
Jersey是一个开源框架,用于开发REST Web服务,提供对JAX-RS API的支持,并作为JAX-RS参考实现。
In this tutorial, we’ll look at how we can set up a Jersey response body with different media types.
在本教程中,我们将看看如何设置具有不同媒体类型的Jersey响应体。
2. Maven Dependencies
2.Maven的依赖性
First, we need the following dependencies included in the pom.xml file:
首先,我们需要在pom.xml文件中包含以下依赖项。
<dependency>
<groupId>org.glassfish.jersey.bundles</groupId>
<artifactId>jaxrs-ri</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.26</version>
</dependency>
The latest version of JAX-RS can be found at jaxrs-ri, and Jersey server can be found at jersey-server
最新版本的JAX-RS可以在jaxrs-ri找到,而Jersey服务器可以在jersey-server找到。
3. Response in Jersey
3.在泽西岛的反应
Naturally, there are different ways to build a response using Jersey, and we’ll look into how we can build them below.
自然,使用Jersey构建响应有不同的方式,我们将在下面研究如何构建。
All the examples here are HTTP GET requests, and we’ll be using the curl command to test the resources.
这里所有的例子都是HTTP GET请求,我们将使用curl命令来测试资源。
3.1. Ok Text Response
3.1.好的文本响应
The endpoint shown here is a simple example of how plain text can be returned as a Jersey response:
这里显示的端点是一个简单的例子,说明如何将纯文本作为泽西岛响应返回。
@GET
@Path("/ok")
public Response getOkResponse() {
String message = "This is a text response";
return Response
.status(Response.Status.OK)
.entity(message)
.build();
}
We can do an HTTP GET using curl to verify the response:
我们可以使用curl做一个HTTP GET,以验证响应。
curl -XGET http://localhost:8080/jersey/response/ok
This endpoint will send back a response as follows:
这个端点将发回如下的响应。
This is a text response
When the media type isn’t specified, Jersey will default to text/plain.
当没有指定媒体类型时,Jersey将默认为text/plain.。
3.2. Error Response
3.2.错误响应
Errors can also be sent back as a Jersey response:
错误也可以作为泽西岛响应被送回。
@GET
@Path("/not_ok")
public Response getNOkTextResponse() {
String message = "There was an internal server error";
return Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(message)
.build();
}
To verify the response, we can do an HTTP GET request using curl :
为了验证响应,我们可以使用curl做一个HTTP GET请求。
curl -XGET http://localhost:8080/jersey/response/not_ok
The error message will be sent back in the response:
错误信息将在响应中发回。
There was an internal server error
3.3. Plain Text Response
3.3.纯文本响应
We can also return simple plain text responses:
我们还可以返回简单的纯文本响应。
@GET
@Path("/text_plain")
public Response getTextResponseTypeDefined() {
String message = "This is a plain text response";
return Response
.status(Response.Status.OK)
.entity(message)
.type(MediaType.TEXT_PLAIN)
.build();
}
Again, we can do an HTTP GET using curl to verify the response:
同样,我们可以使用curl做一个HTTP GET,以验证响应。
curl -XGET http://localhost:8080/jersey/response/text_plain
The response will be as follows:
答复如下。
This is a plain text response
The same outcome could also be achieved via the Produces annotation instead of using the type() method in the Response:
同样的结果也可以通过Produces注解来实现,而不是在Response中使用type()方法。
@GET
@Path("/text_plain_annotation")
@Produces({ MediaType.TEXT_PLAIN })
public Response getTextResponseTypeAnnotated() {
String message = "This is a plain text response via annotation";
return Response
.status(Response.Status.OK)
.entity(message)
.build();
}
We can do response verification using curl:
我们可以使用curl进行响应验证。
curl -XGET http://localhost:8080/jersey/response/text_plain_annotation
Here’s the response:
以下是答复。
This is a plain text response via annotation
3.4. JSON Response Using POJO
3.4.使用POJO的JSON响应
A simple Plain Old Java Object (POJO) can also be used build a Jersey response.
一个简单的Plain Old Java Object(POJO)也可以用来构建Jersey响应。
We have a very simple Person POJO shown below, which we’ll use to build a response:
我们有一个非常简单的Person POJO,如下所示,我们将用它来建立一个响应。
public class Person {
String name;
String address;
// standard constructor
// standard getters and setters
}
The Person POJO can now be used to return JSON as the Response body:
Person POJO现在可以用来返回JSON作为响应体。
@GET
@Path("/pojo")
public Response getPojoResponse() {
Person person = new Person("Abhinayak", "Nepal");
return Response
.status(Response.Status.OK)
.entity(person)
.build();
}
The working of this GET endpoint can be verified – via the following curl command:
这个GET端点的工作可以通过以下curl命令进行验证。
curl -XGET http://localhost:8080/jersey/response/pojo
The Person POJO will be transformed into a JSON and sent back as a response:
Person POJO将被转换为JSON,并作为响应发送回来。
{"address":"Nepal","name":"Abhinayak"}
3.5. JSON Response Using Simple String
3.5.使用简单字符串的JSON响应
We can use preformatted strings to create a response, and it can be done simply.
我们可以使用预格式化的字符串来创建一个响应,而且可以简单地完成。
The following endpoint is an example of how a JSON represented as a String can be sent back as a JSON in the Jersey response:
下面的端点是一个例子,说明以String表示的JSON如何在Jersey响应中作为JSON被送回来。
@GET
@Path("/json")
public Response getJsonResponse() {
String message = "{\"hello\": \"This is a JSON response\"}";
return Response
.status(Response.Status.OK)
.entity(message)
.type(MediaType.APPLICATION_JSON)
.build();
}
This can be verified by doing an HTTP GET using curl to verify the response:
这可以通过使用curl做HTTP GET来验证响应。
curl -XGET http://localhost:8080/jersey/response/json
Calling this resource will return a JSON:
调用该资源将返回一个JSON。
{"hello":"This is a JSON response"}
The same pattern applies for other common media types like XML or HTML. We just need to notify Jersey that it’s an XML or HTML using MediaType.TEXT_XML or MediaType.TEXT_HTML and Jersey will handle the rest.
同样的模式适用于其他常见的媒体类型,如XML或HTML。我们只需要用MediaType.TEXT_XML或MediaType.TEXT_HTML通知Jersey它是一个XML或HTML,Jersey就会处理剩下的事情。
4. Conclusion
4.结论
In this quick article, we constructed Jersey (JAX-RS) responses for a variety of media types.
在这篇快速文章中,我们为各种媒体类型构建了Jersey(JAX-RS)响应。
All of the code snippets, mentioned in the article, can be found in over on GitHub.
文章中提到的所有代码片段,都可以在GitHub上找到。