1. 概述
为了简化 Java 中 REST Web 服务及其客户端的开发,业界推出了标准且可移植的 JAX-RS API 实现,其中最知名的便是 Jersey。
Jersey 是一个开源框架,用于开发 RESTful Web 服务,它不仅支持 JAX-RS API,还作为其官方参考实现(RI)被广泛使用。
在这篇文章中,我们将重点探讨如何通过 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>
你可以在这里找到最新的 jaxrs-ri 和 jersey-server 版本。
3. Jersey 中的 Response
在 Jersey 中,构建响应的方式多种多样,下面我们会逐个介绍。以下示例均使用 HTTP GET 请求,并通过 curl
命令进行测试。
3.1. 成功文本响应
下面是一个返回纯文本的简单接口示例:
@GET
@Path("/ok")
public Response getOkResponse() {
String message = "This is a text response";
return Response
.status(Response.Status.OK)
.entity(message)
.build();
}
使用 curl
验证:
curl -XGET http://localhost:8080/jersey/response/ok
返回结果:
This is a text response
✅ 注意: 如果没有显式指定媒体类型,Jersey 默认使用 text/plain
。
3.2. 错误响应
当然,你也可以返回错误信息作为响应:
@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();
}
使用 curl
验证:
curl -XGET http://localhost:8080/jersey/response/not_ok
返回结果:
There was an internal server error
3.3. 明确指定的纯文本响应
我们也可以显式指定响应类型为纯文本:
@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();
}
使用 curl
验证:
curl -XGET http://localhost:8080/jersey/response/text_plain
返回结果:
This is a plain text response
✅ 小技巧: 你也可以使用 @Produces
注解来代替 .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();
}
验证命令:
curl -XGET http://localhost:8080/jersey/response/text_plain_annotation
返回结果:
This is a plain text response via annotation
3.4. 使用 POJO 返回 JSON
我们也可以使用 POJO(Plain Old Java Object) 来构建响应体。
先定义一个简单的 Person
类:
public class Person {
String name;
String address;
// standard constructor
// standard getters and setters
}
然后在接口中返回这个对象:
@GET
@Path("/pojo")
public Response getPojoResponse() {
Person person = new Person("Abhinayak", "Nepal");
return Response
.status(Response.Status.OK)
.entity(person)
.build();
}
使用 curl
验证:
curl -XGET http://localhost:8080/jersey/response/pojo
返回结果:
{"address":"Nepal","name":"Abhinayak"}
✅ Jersey 会自动将 POJO 序列化成 JSON。
3.5. 使用字符串返回 JSON
当然,你也可以手动构造 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();
}
使用 curl
验证:
curl -XGET http://localhost:8080/jersey/response/json
返回结果:
{"hello":"This is a JSON response"}
⚠️ 扩展提示: 同样的方式也适用于 XML 或 HTML,只需将 MediaType
设置为 TEXT_XML
或 TEXT_HTML
即可。
4. 总结
这篇文章我们快速过了一遍如何在 Jersey(JAX-RS)中构建不同媒体类型的响应体,包括纯文本、JSON、POJO、字符串等。
所有代码示例均可在 GitHub 仓库中找到 👉 GitHub 示例代码