Passing custom type query parameter

前端 未结 1 1967
误落风尘
误落风尘 2020-12-03 19:45

How can I accept custom type query parameter?

public String detail(@QueryParam(\"request\") final MYRequest request) {

Above line gives err

相关标签:
1条回答
  • 2020-12-03 20:00

    Take a look at the @QueryParam documentation, in regards to acceptable types to inject. (The same applies to all the other @XxxParam annotations also)

    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.

    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.

    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;
        }
    }
    

    You can see a good example of using a ParamConverter here

    0 讨论(0)
提交回复
热议问题