سؤال

I believe this is not possible, but I just wanted to verify.

I would like to do something like...

@Path("/awesome")
public class MyRestResource {

  @GET
  public void coolQuery(@QueryParam("user") User) {
    // ...
  }
}

public interface User {
  String name();
  Address address();
}

(Please don't comment on the example... it's completely made-up and not my use case.)

I imagine this is not possible because Jersey/JAX-RS generally requires a static method public static T valueOf(String input) which obviously is not possible with interfaces.

That said, is there any work-around for this to have a query parameter be an interface? And if so, how do you specify the parser / parsing logic?

Thanks

هل كانت مفيدة؟

المحلول

According to the documentation there are more ways than just the static valueOf method:

  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) and java.util.UUID.fromString(String));
  4. Have a registered implementation of javax.ws.rs.ext.ParamConverterProvider JAX-RS extension SPI that returns a javax.ws.rs.ext.ParamConverter instance capable of a "from string" conversion for the type. or
  5. Be List<T>, Set<T> or SortedSet<T>, where T satisfies 2 or 3 above. The resulting collection is read-only.

The solution using a ParamConverterProvider should work in this case.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top