传递自定义类型查询参数 [英] Passing custom type query parameter
问题描述
如何接受自定义类型查询参数?
How can I accept custom type query parameter?
public String detail(@QueryParam("request") final MYRequest request) {
上面的行在启动服务器时出错
Above line gives error while starting the server
jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
推荐答案
看一下 @QueryParam
文件,关于可接受的注射类型。 (这同样适用于所有其他 @XxxParam
注释)
- 是一个原始类型
- 有一个接受单个String参数的构造函数
- 有一个名为
的静态方法valueOf
或fromString
接受单个String参数(例如,参见Integer.valueOf(String)
) - 注册实现 ParamConverterProvider JAX-RS扩展SPI返回 ParamConverter 实例,可以为该类型进行from string转换。
- Be
List< T>
,设置< T>
或SortedSet< T>
,其中T
满足上面的2,3或4。生成的集合是只读的。
- Be a primitive type
- Have a constructor that accepts a single String argument
- Have a static method named
valueOf
orfromString
that accepts a single String argument (see, for example,Integer.valueOf(String)
) - Have a registered implementation of ParamConverterProvider JAX-RS extension SPI that returns a ParamConverter instance capable of a "from string" conversion for the type.
- Be
List<T>
,Set<T>
orSortedSet<T>
, whereT
satisfies 2, 3 or 4 above. The resulting collection is read-only.
这些要求的原因是值以字符串形式出现。运行时需要知道如何将字符串转换为要注入的类型。异常的原因是启动时有初始资源模型验证。此验证检查以确保所有注入点都有效。它看到注入的类型 MyRequest
不符合上述任何要求,并抛出异常。
The reason for these requirements is that the value comes in as a string. The runtime needs to know how to convert a string to the type to inject. The reason for the exception is that there is an initial resource model validation on startup. This validation checks to make sure all your injection points are valid. It sees that the injected type MyRequest
doesn't meet any of the above requirements, and throws an exception.
基本上你有点2和3,你需要自己解析字符串,例如
Basically you with points 2 and 3, you will need to parse the string yourself, for instance
public class MyRequest {
public static MyRequest fromString(string param) {
// 1. Parse string
// 2. Create MyRequest request;
return request;
}
}
你可以看到一个使用<$的好例子c $ c> ParamConverter 这里
You can see a good example of using a ParamConverter
here
这篇关于传递自定义类型查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!