5.4. JAX-RS 和 RESTEasy 应用更改
JBoss EAP 6 捆绑的 RESTEasy 2,这是 JAX-RS 1.x 的实施。
JBoss EAP 7.0 和 JBoss EAP 7.1 包括 RESTEasy 3.0.x,这是 JSR 339: JAX-RS 2.0 中定义的 JAX-RS 2.0 实施:RESTful Web 服务规范的 Java API。有关 RESTful Web 服务的 Java API 的更多信息,请参阅 JAX-RS 2.0 API 规范。
JBoss EAP 7.3 包含 RESTEasy 3.9.0,这是 JSR 370 中定义的 JAX-RS 2.1 实施,适用于 RESTful Web 服务(JAX-RS 2.1)规范的 Java(TM)API。Jakarta 对 RESTful Web 服务的对等功能在 Jakarta RESTful Web Services 2.1 中定义。此发行版本还添加了对JDK 11 的支持。虽然提供了一些 RESTEasy 4 关键功能,但此版本基于 RESTEasy 3.0,确保完全向后兼容性。因此,从 RESTEasy 3.0.x 迁移到 3.9.0 时您应该遇到较少的问题。有关 RESTEasy 3.9.0 的 Java API 的更多信息,请参阅 RESTEasy JAX-RS 3.9.0.Final API。
如果要从 JBoss EAP 6.4 迁移,请注意 JBoss EAP 中包含的 Jackson 版本已改变。JBoss EAP 6.4 包括 Jackson 1.9.9。JBoss EAP 7 和更高版本现在包括 Jackson 2.6.3 或更高版本。
本节介绍这些更改如何影响使用 RESTEasy 或 JAX-RS 的应用。
5.4.1. RESTEasy 已弃用的类 复制链接链接已复制到粘贴板!
拦截器和消息类
JSR 311:JAX-RS:RESTful Web 服务的 Java™ API 不包含拦截器框架,因此 RESTEasy 2 提供了框架。JSR 339:JAX-RS 2.0:RESTful Web 服务的 Java API 引入了一个官方拦截器和过滤框架,因此 RESTEasy 2 中包含的拦截器框架现已弃用,并被 RESTEasy 3.x 中的 JAX-RS 兼容拦截器功能所取代。相关的接口在 ja
软件包中定义。
xrs-api 模块的 javax.ws.rs
.ext
RESTEasy 3.x 中弃用了以下拦截器接口:
-
RESTEasy 3.x 中的
org.jboss.resteasy.spi.interception.PreProcessInterceptor
接口被javax.ws.rs.container.ContainerRequestFilter
接口替代。 以下接口和类也在 RESTEasy 3.x 中弃用:
-
org.jboss.resteasy.spi.interception.MessageBodyReaderInterceptor
-
org.jboss.resteasy.spi.interception.MessageBodyWriterInterceptor
-
org.jboss.resteasy.spi.interception.MessageBodyWriterContext
-
org.jboss.resteasy.spi.interception.MessageBodyReaderContext
-
org.jboss.resteasy.core.interception.InterceptorRegistry
-
org.jboss.resteasy.core.interception.InterceptorRegistryListener
-
org.jboss.resteasy.core.interception.ClientExecutionContextImpl
-
-
org.jboss.resteasy.spi.interception.MessageBodyWriterInterceptor
接口被javax.ws.rs.ext.WriterInterceptor
接口取代。 此外,对
javax.ws.rs.ext.MessageBodyWriter
接口的一些更改可能不会向后兼容 JAX-RS 1.x。如果您的应用使用了 JAX-RS 1.x,请检查您的应用代码,确保您的端点定义了@Produces
或@Consumes
。如果不这样做,可能会导致出现类似如下的错误:org.jboss.resteasy.core.NoMessageBodyWriterFoundFailure: Could not find MessageBodyWriter for response object of type: <OBJECT> of media type:
org.jboss.resteasy.core.NoMessageBodyWriterFoundFailure: Could not find MessageBodyWriter for response object of type: <OBJECT> of media type:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 以下是可能导致此错误的 REST 端点示例:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 若要修复问题,可添加
javax.ws.rs.Produces
和@Produces
注释的导入,如下所示:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
RESTEasy 之前发行版中的所有拦截器都可以与新的 JAX-RS 过滤器和拦截器接口并行运行。
如需有关拦截器的更多信息,请参阅 JBoss EAP 开发 Web 服务应用的 RESTEasy Interceptors。
如需有关新替换 API 的更多信息,请参见 RESTEasy JAX-RS 3.9.0.Final API。
客户端 API
resteasy-jaxrs
中的 RESTEasy 客户端框架被 JBoss EAP 7.0 中的 JAX-RS 2.0 兼容 resteasy-client
模块取代。因此,一些 RESTEasy 客户端 API 类和方法已被弃用。
以下类已被弃用。
-
org.jboss.resteasy.client.ClientResponseFailure
异常和org.jboss.resteasy.client.ClientExecutor
和org.jboss.resteasy.client.EntityTypeFactory
接口也已被弃用。 您必须将
org.jboss.resteasy.client.ClientRequest
和org.jboss.resteasy.client.ClientResponse
类分别替换为org.jboss.resteasy.client.jaxrs.ResteasyClient
和javax.ws.rs.core.Response
。以下是如何在 RESTEasy 2.3.x 中使用 RESTEasy 客户端发送链路标头的示例:
ClientRequest request = new ClientRequest(generateURL("/linkheader/str")); request.addLink("previous chapter", "previous", "http://example.com/TheBook/chapter2", null); ClientResponse response = request.post(); LinkHeader header = response.getLinkHeader();
ClientRequest request = new ClientRequest(generateURL("/linkheader/str")); request.addLink("previous chapter", "previous", "http://example.com/TheBook/chapter2", null); ClientResponse response = request.post(); LinkHeader header = response.getLinkHeader();
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 以下是如何在 RESTEasy 3 中使用 RESTEasy 客户端完成同一任务的示例。
ResteasyClient client = new ResteasyClientBuilder().build(); Response response = client.target(generateURL("/linkheader/str")).request() .header("Link", "<http://example.com/TheBook/chapter2>; rel=\"previous\"; title=\"previous chapter\"").post(Entity.text(new String())); javax.ws.rs.core.Link link = response.getLink("previous");
ResteasyClient client = new ResteasyClientBuilder().build(); Response response = client.target(generateURL("/linkheader/str")).request() .header("Link", "<http://example.com/TheBook/chapter2>; rel=\"previous\"; title=\"previous chapter\"").post(Entity.text(new String())); javax.ws.rs.core.Link link = response.getLink("previous");
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 有关与 JAX
-RS Web 服务交互的外部 JAX-RS RESTEasy 客户端示例,请参见 resteasy-jaxrs-client
快速入门。-
org.jboss.resteasy.client.cache
软件包中的类和接口也已弃用。它们被org.jboss.resteasy.client.jaxrs.cache
包中的等效类和接口取代。
有关 org.jboss.resteasy.client.jaxrs
API 类的更多信息,请参阅 RESTEasy JAX-RS JavaDoc。
StringConverter
org.jboss.resteasy.spi.StringConverter
类在 RESTEasy 3.x 中已弃用。可以使用 JAX-RS jax.ws.rs.ext.ParamConverterProvider 类替换此功能。