我正在学习 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();
}
}
(有时 PathSegment
是 List<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 aParamConverter
is available via a registeredParamConverterProvider
. See Javadoc for these classes for more information. 2. Primitive types.
3. Types that have a constructor that accepts a singleString
argument.
4. Types that have a static method namedvalueOf
orfromString
with a singleString
argument that return an instance of the type. If both methods are present thenvalueOf
MUST be used unless the type is an enum in which casefromString
MUST be used.
5.List<T>
,Set<T>
, orSortedSet<T>
, whereT
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/