1. 概述
為了簡化 Java 中 REST Web 服務及其客户端的開發,設計了一種標準且可移植的 JAX-RS API 實現,稱為 Jersey。
Jersey 是一個開源框架,用於開發支持 JAX-RS API 的 REST Web 服務,並作為 JAX-RS 的參考實現。
在本教程中,我們將探討如何設置 Jersey 響應體,並使用不同的媒體類型。
2. Maven 依賴項
首先,我們需要將以下依賴項包含在 pom.xml
<dependency>
<groupId>org.glassfish.jersey.bundles</groupId>
<artifactId>jaxrs-ri</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>3.1.1</version>
</dependency>
最新版本的 JAX-RS
jaxrs-ri,以及 Jersey 服務器可以在 jersey-server
3. 響應在 Jersey
Naturally, there are different ways to build a response using Jersey, and we’ll look into how we can build them below.
All the examples here are HTTP GET requests, and we’ll be using the curl command to test the resources.
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 -XGET http://localhost:8080/jersey/response/ok
The response will be as follows:
This is a text response
When the media type isn’t specified, Jersey 將默認到 text/plain.
3.2. 錯誤響應
錯誤也可以被髮送回 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 -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. 純文本響應
We can also return 簡單的純文本響應:
@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 -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:
@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 -XGET http://localhost:8080/jersey/response/text_plain_annotation
Here’s the response:
This is a plain text response via annotation
3.4. JSON 響應使用 POJO
A simple 普通的 Java 對象 (POJO) 也可以用來構建 Jersey 響應.
We have a very simple Person POJO shown below, which we’ll use to build a response:
public class Person {
String name;
String address;
// standard constructor
// standard getters and setters
}
The Person POJO can now be used to 返回 JSON 作為 Response body:
@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:
curl -XGET http://localhost:8080/jersey/response/pojo
The Person POJO will be transformed into a JSON and sent back as a response:
{"address":"Nepal","name":"Abhinayak"}
3.5. JSON 響應使用簡單字符串
We can use 預格式化字符串來創建響應, 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:
@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 -XGET http://localhost:8080/jersey/response/json
Calling this resource will return a JSON:
{"hello":"This is a JSON response"}
相同的模式也適用於其他常見的媒體類型,如 XML 或 HTML。 我們只需要通知 Jersey 它是一個 XML 或 HTML,使用 MediaType.TEXT_XML 或 MediaType.TEXT_HTML, Jersey 將處理其餘部分。
4. 結論
在本文中,我們構建了多種媒體類型的Jersey (JAX-RS) 響應。