users@jersey.java.net

[Jersey] Overriding MediaType when using JAXB XML

From: Graham Leggett <minfrin_at_sharp.fm>
Date: Mon, 19 Mar 2012 03:40:29 +0200

Hi all,

I have a jersey method that returns a JAXB object, which when rendered as application/xml works fine:

                return Response.ok(plist).type("application/xml").build();

The problem I have is that I need to override the MIME type for the application - the content is still expected to be valid XML, but the MIME type needs to be changed to "application/x-foo" (I can't change this MIME type).

                return Response.ok(plist).type("application/x-foo").build();

This fails with the following exception:

javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class org.foo.Plist, and Java type class org.foo.Plist, and MIME media type application/x-foo was not found.

Why the error is happening is understood - jersey is no longer recognising the JAXB object as XML, and so doesn't know what to do with it.

My problem is that I don't know the recommended way to fix this problem - I want the JAXB to still be rendered as XML, I just want to have a MIME type that I specify. Can jersey do this, and if so, how?

Regards,
Graham
--