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

From: Stefan Tilkov <>
Date: Thu, 29 Nov 2007 18:53:29 +0100

On Nov 29, 2007, at 10:18 AM, Liu, Jervis wrote:

> It is mentioned in JSR-311 section 3.1.2 that "An implementation MUST
> include pre-packaged EntityProvider implementations for the following
> Java and media type combinations: java.lang.String All text media
> types
> (text/*).". However I don't see any places in the spec mentions about
> the conversion between a Java primitive types and an entity body. It
> is
> a common use case that I want to map my SQL "select employee _age from
> employee where employee _name='john'" to a java method "int
> getEmployeeAge(String employeeName)" then expose this as a RESTful
> service. Is this the correct way to do REST, or I am still having my
> head in a Service-oriented world instead of resource-oriented?

It's questionable whether an employee's age should be a resource in
itself, but that's a design decision. Nothing unRESTful about it.

> Anyway,
> let's say my use case is indeed valid, in this case what content
> type I
> shall use to return an int type so that the client can make sense of?

text/plain if you return the characters "27". Some other content type
if you use a binary representation; I'm not sure which one would be
applicable and would actually cause less problems. Text seems fine
here from POV.

> Shall I return int using content type "text/*"? In the case of
> returning
> String, as long as the client knows the returned content type is text,
> it should always be able to marshal the response body to a String
> without making any further assumptions. However this does not work for
> int type. If only information I have on the client side is the content
> type "text/*", it would be impossible for me to make sense of the
> response, is it a int type, is it a double type? I simply do not know.

That's a typical trade off, similarly to using application/xml: You
have said that it's XML (or text/plain), which means that an client
capable of handling this can work with it (which is good). You don't
get any more information (which is bad). Make your own choice ;-)

Stefan Tilkov,
> Or maybe, return an int using "text/*" is not a good practice in the
> RESTful world, using "application/xml" would be better. Can I say
> exposing a java method whose signature is " int getEmployeeAge(String
> employeeName)" is not a good practice, you should always wrap the  
> result
> in a Java object instead, e.g., like " Employee getEmployeeAge(String
> employeeName)"? Or maybe, the JSR-311 runtime can go a little bit  
> fancy,
> for example, it can generate a wrapper automatically on top of java
> primitive types so that the returned result is a valid an xml doc.
> Any thoughts would be highly appreciated. And should not JSR-311 spec
> include a section to describe how to do the conversion between a Java
> primitive types and an entity body?
> Thanks,
> Jervis Liu
> ----------------------------
> IONA Technologies PLC (registered in Ireland)
> Registered Number: 171387
> Registered Address: The IONA Building, Shelbourne Road, Dublin 4,  
> Ireland
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail: