java - 根据 JAX-RS 中的规范使用带有 @PathParam 的 PathSegment?

标签 java eclipse jakarta-ee jax-rs

我正在学习 JAX-RS。在阅读它和阅读相关书籍时,我多次阅读过如下示例:

import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.PathSegment;

@Path("/read")
@Stateless
@Produces("text/plain")
public class MinimalExample
{
  @GET
  @Path("/b/{file}")
  public String readFile2(@PathParam("file") List<PathSegment> fileParam)
  {
    return fileParam.toString();
  }
}

(有时 PathSegmentList<PathSegment>,但这现在不相关。) 重现示例时 Eclipse 抛出错误,告诉我:

The type 'javax.ws.rs.core.PathSegment' is not valid for this parameter. See JAX-RS 2.0 Specification (section 3.2) for more information.

我查阅了相关规范,确实证实了这一点。 PathSegment是一个既没有 valueOf 又没有的接口(interface)也不fromString 。所有示例均未提供 ParamConverterProvider .

Valid parameter types for each of the above annotations are listed in the corresponding Javadoc, however in general (excluding @Context) the following types are supported:
1. Types for which a ParamConverter is available via a registered ParamConverterProvider. See Javadoc for these classes for more information. 2. Primitive types.
3. Types that have a constructor that accepts a single String argument.
4. Types that have a static method named valueOf or fromString with a single String argument that return an instance of the type. If both methods are present then valueOf MUST be used unless the type is an enum in which case fromString MUST be used.
5. List<T>, Set<T>, or SortedSet<T>, where T satisfies 1, 3 or 4 above.

这与 JavaEE 规范形成对比,JavaEE 规范也允许 PathSegment as shown here作为有效类型。
有人可以向我解释一下所有这些 JAX-RS 示例在违反 JAX-RS 规范时到底是如何使用 PathSegment 的吗?我无法想象这是一个简单的错误,因为它太普遍了,而且我还没有看到任何评论提示他们的示例不起作用。

编辑:我已经确定一些更完整的示例实际上在使用 mvn install 时进行构建但使用 Eclipse 时则不然。这让我相信它可能需要更少地处理 JAX-RS 本身,而更多地处理我的一些 Eclipse 设置。然而,我很难准确地查看哪里,甚至更困惑 - 为什么这些示例在不符合 JAX-RS 规范的情况下构建?

最佳答案

我应该更仔细地阅读我自己的引言(强调我的):

Valid parameter types for each of the above annotations are listed in the corresponding Javadoc, however in general (excluding @Context) the following types are supported:
...

检查 Javadoc 后,我确认 @PathParam 确实允许 PathSegment 作为一种类型。 该错误本身似乎来自 JBoss Eclipse 插件的 JAX-RS-Validator。 该插件的最新版本不会显示该错误。

关于java - 根据 JAX-RS 中的规范使用带有 @PathParam 的 PathSegment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59425427/

相关文章:

javascript - 将 Javascript 组件转换为 JSF 2.0 组件

java - 如何使用jpa从数据库中获取特定的id?

java - Java SE 和 Java EE 之间的主要区别是什么?

java - 域类与 Dto 类与 Swagger 命名

java - 执行按钮单击操作时更新标签文本 (Java)

ios - 将 ipa 提交到 App Store 时出错

android - Java/安卓 : How do you properly import a library in Eclipse?

java - java中字母表的字符串分词器

java - 开始制作Android应用程序

eclipse - 如何为 Eclipse 安装和配置 EclipseFP Haskell 插件?