users@jersey.java.net

[Jersey] Re: Supporting Optional arguments

From: Jason Tovey <jason.tovey_at_gmail.com>
Date: Mon, 24 Nov 2014 11:04:43 -0500

Hi Andreas,

  I'm relatively new to the JAX-RS world, so take all this with a grain of
salt. My understanding is that the framework doesn't really care what
types you choose for your resource method parameters, but they must meet
the following criteria (from the @PathParam
<https://javaee-spec.java.net/nonav/javadocs/javax/ws/rs/PathParam.html>
docs):

Binds the value of a URI template parameter or a path segment containing
> the template parameter to a resource method parameter, resource class
> field, or resource class bean property. The value is URL decoded unless
> this is disabled using the @Encoded
> <https://javaee-spec.java.net/nonav/javadocs/javax/ws/rs/Encoded.html>annotation.
> A *default value* can be specified using the @DefaultValue
> <https://javaee-spec.java.net/nonav/javadocs/javax/ws/rs/DefaultValue.html> annotation.
> The type of the annotated parameter, field or property must either:
>
> - Be PathSegment
> <https://javaee-spec.java.net/nonav/javadocs/javax/ws/rs/core/PathSegment.html>,
> the value will be the final segment of the matching part of the path. See
> UriInfo
> <https://javaee-spec.java.net/nonav/javadocs/javax/ws/rs/core/UriInfo.html> for
> a means of retrieving all request path segments. *(Applies to
> PathParams obviously.)*
>
>
> - Be List<javax.ws.rs.core.PathSegment>, the value will be a list of
> PathSegment corresponding to the path segment(s) that matched the
> named template parameter. See UriInfo
> <https://javaee-spec.java.net/nonav/javadocs/javax/ws/rs/core/UriInfo.html> for
> a means of retrieving all request path segments. *(Also PathParam
> specific...)*
>
>
> - Be a primitive type. *(Optional is not a primitive type.)*
>
>
> - Have a constructor that accepts a single String argument. *(Optional
> does not have any public constructors, in fact.)*
>
>
> - Have a static method named valueOf or fromString that accepts a
> single String argument (see, for example, Integer.valueOf(String)
> <http://download.oracle.com/javase/6/docs/api/java/lang/Integer.html?is-external=true#valueOf(java.lang.String)>).
> *(No on both counts.)*
>
>
> - Have a registered implementation of ParamConverterProvider
> <https://javaee-spec.java.net/nonav/javadocs/javax/ws/rs/ext/ParamConverterProvider.html> JAX-RS
> extension SPI that returns a ParamConverter
> <https://javaee-spec.java.net/nonav/javadocs/javax/ws/rs/ext/ParamConverter.html> instance
> capable of a "from string" conversion for the type. *(This has
> potential...)*
>
>
So it would seem the only way you could pull this off would be with a
custom (or find one somewhere) ParamConverter that accepts a String and
returns an Optional of your preferred type. The trick here, and where I
hit the wall being new to it, is how to handle generics. Maybe you can
already handle that and maybe somebody else here could point to help on how
to do it.


One other potential hurdle is the fact that your ParamConverter may not be
called if the value is missing/null. A potential workaround would be to
add a @DefaultValue("Optional.absent") annotation that your ParamConverter
knows how to handle. But then that's a lot more work and maybe no more
elegant than the first line of your code being Optional<T>
realArgument=Optional.ofNullable(suppliedArgument).


Jason




On Mon, Nov 24, 2014 at 10:24 AM, Andreas Lundblad <
andreas.lundblad_at_gmail.com> wrote:

>
>
> On Mon, Nov 24, 2014 at 4:07 PM, Pavel Bucek <pavel.bucek_at_oracle.com>
> wrote:
>
>> Hi Andreas,
>>
>> you might want to clear all cookies for java.net and try again with new
>> user/pass, it should work. Login directly on http://java.net and then
>> access
>> https://java.net/jira/browse/JAX_RS_SPEC/.
>> <https://java.net/jira/browse/JAX_RS_SPEC/>
>>
>
>
> Thanks. Clearing the cookies did the trick.
>
> RFE filed: https://java.net/jira/browse/JAX_RS_SPEC-498
>