Hello all,
I am learning Jersey 1.3 and so far have been amazed at the
"automagicality" provided by JAX-RS -- how little you have to do to set
up a powerful REST interface.
What I was surprised to learn is that I could not return and take
primitive types as entities in the resource methods like it can as
@PathParam:
@GET @Path( "{name}" ) @Produces( "text/plain" )
public long getCounter( @PathParam( "name" ) String name )
@POST @Path( "{name}" ) @Consumes( "text/plain" )
public long addCounter(
@PathParam( "name" ) String name,
long toAdd )
It's quite possible I missed something, but also possibly not because
normally people are working with more complicated (JAXB) POJOs so this
is not a typical case.
I drafted a NumberProvider which can handle the numeric primitive types
(not char and boolean). Assuming I am not missing something, maybe this
is something useful for Jersey to support out-of-the-box? The code could
probably be also made more generic for many more types, but I restricted
it because I wasn't sure if it's best to try to work automatically with
complex user types, where String might work technically but not be
appropriate in the external interface.
It converts the primitives to and from String, and if a nullable type,
converts "null" to null. It does the same for outgoing, but it appears
that Jersey treats a null return as 204 code rather than going through
the provider (seems reasonable, rarely would you want to return null).
I tried to do some error handling by returning 400 (I think that's
right?) if the number can't be parsed.
Jason