Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.
like image 242
Himanshu Yadav Avatar asked May 22 '15 17:05

Himanshu Yadav


1 Answers

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

like image 88
Paul Samsotha Avatar answered Oct 23 '22 20:10

Paul Samsotha