Re: JSR311: The best practice to return primitive types from a RESTful service?

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Mon, 03 Dec 2007 09:50:01 -0500

On Dec 3, 2007, at 2:37 AM, Liu, Jervis wrote:
>>> a). in JSR-311 section 3.1.2, "An implementation MUST include pre-
>>> packaged EntityProvider implementations for the following Java and
>>> media type combinations:", add the support for Java Primitive types:
>>> Java primitive types All text media types (text/*).
>>> But with an statement that returning a fine grained primitive type
>>> is not a recommended practice.
>>> Essentially this means a JSR-311 runtime implementation needs to
>>> provide a PrimitiveEntityProvider to read and write primitive types
>>> to/from entity body using text/plain content type, same as what we
>>> do with String.
>>> b). Do not provide any support for Java Primitive types but
>>> explicitly stated in JSR-311 section 3.1.2 that no default support
>>> for returning Java Primitive types is because this is not a
>>> recommended practice. But if users really want to do this, they
>>> really understand the design choice they have made, they can do this
>>> by registering their own PrimitiveEntityProvider on the JSR-311
>>> container.
>> c) Use one of the primitive type wrapper classes (e.g.
>> java.lang.Integer) along with a corresponding MessageBodyWriter.
>> Thinking about it we could remove the need for a MessageBodyWriter by
>> requiring the runtime to use the toString method when there's no
>> MessageBodyWriter. The problem with that is that Object provides a
>> default toString so it would be easy for an application to exhibit
>> unexpected behavior if the MessageBodyWriter they provide isn't
>> picked
>> up for some reason. Essentially a class of error (missing
>> MessageBodyWriter) would be hard to detect.
> Hi Marc, did you suggest that the JSR-311 runtime provides a
> behavior such that when there is no MessageBodyWriter selected for
> the return type, Object.toString will be used to form an
> output( does this also suggest that in the case of no
> MessageBodyWriter selected, the default output content type is text/
> plain?).

Yes and no respectively. Yes, I was suggesting use of Object.toString
in the case of no MessageBodyWriter though I'm not convinced its a
good idea due to the issue I describe above. No, the media type would
still be determined based on the criteria I described in the mail I
sent last week with the subject "Determining the media type of

> Or instead what you meant is defining a pre-packaged
> MessageBodyWriter in JSR-311 spec to handle primitive types. For the
> later, I donít see the difference between this and approach a).
> Using primitive type wrapper classes is the implementation detail of
> PrimitiveTypeMessageBodyWriter.
There's a difference between implementing support for int someMethod()
vs Integer someMethod(). E.g. a primitive type can't be the
parameterized type of MessageBodyWriter so you have to use the
primitive wrapper class instead.


Marc Hadley <marc.hadley at>
CTO Office, Sun Microsystems.