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.
Take a look at the @QueryParam
documentation, in regards to acceptable types to inject. (The same applies to all the other @XxxParam
annotations also)
valueOf
or fromString
that accepts a single String argument (see, for example, Integer.valueOf(String)
)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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With