users@jersey.java.net

Re: [Jersey] returning basic java data types

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 05 Aug 2009 08:54:56 +0200

Hi,

JAX-RS/Jersey does not support the serialization of primitive types
(or their Object counter parts).

If you want to support such types you need to implement your own
message body writer and also declare an appropriate media type with
that writer (the content-type of the response will use that, if
@Produces does not declare it).

IMHO application/octet-stream is not appropriate for such content, as
it is most likely you will serialize the boolean value out as the
strings "true" or "false". I would recommend using text/plain and
document the representation. You can always use String in your
resource method as well.

Part of the issue with returning such primitive data is there are,
AFAIK, no specific media types associated with them.

Paul.

On Aug 5, 2009, at 3:36 AM, Suchitha Koneru (sukoneru) wrote:

> Hello Jersey Users,
> If we have to return basic Java data types like boolean,
> integer, float etc. other than String , do we need to implement
> message body writer interface ?
>
> For example , when I test the following code I get the exception as
> SEVERE: A message body writer for Java type, class
> java.lang.Boolean, and MIME media type, application/octet-stream, was
> not found
>
>
> @Context SecurityContext sc;
> @GET @Path ("role")
> public boolean checkUserRole(){
>
> if(sc.isUserInRole("cco")){
> return true;
> }else{
> return false;
> }
> }
>
> If MIME type is not specified , is the default mime type assumed as
> “application/octet-stream” ? Does Jersey have a messagebodywriter
> For basic java data types ?
>
> Could you please let me know ,
> Thanks
> Suchitha.