根据 JAX-RS 中的规范将 PathSegment 与 @PathParam 一起使用?
Using PathSegment with @PathParam against specifications in 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();
}
}
(有时 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
也没有 fromString
的接口。 None 个示例提供了 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。
最新版本的插件不会显示错误。
我正在学习 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
也没有 fromString
的接口。 None 个示例提供了 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。
最新版本的插件不会显示错误。