传递自定义类型查询参数 [英] Passing custom type query parameter

查看:89
本文介绍了传递自定义类型查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何接受自定义类型查询参数?

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 注释)


  1. 是一个原始类型

  2. 有一个接受单个String参数的构造函数

  3. 有一个名为的静态方法valueOf fromString 接受单个String参数(例如,参见 Integer.valueOf(String)

  4. 注册实现 ParamConverterProvider JAX-RS扩展SPI返回 ParamConverter 实例,可以为该类型进行from string转换。

  5. Be List< T> 设置< T> SortedSet< T> ,其中 T 满足上面的2,3或4。生成的集合是只读的。

  1. Be a primitive type
  2. Have a constructor that accepts a single String argument
  3. Have a static method named valueOf or fromString that accepts a single String argument (see, for example, Integer.valueOf(String))
  4. Have a registered implementation of ParamConverterProvider JAX-RS extension SPI that returns a ParamConverter instance capable of a "from string" conversion for the type.
  5. Be List<T>, Set<T> or SortedSet<T>, where T 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆