Himanshu Yadav Himanshu Yadav - 1 year ago 93
reST (reStructuredText) Question

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.

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download