dev@jsr311.java.net

Setting response content-type should be required

From: Bill Burke <bburke_at_redhat.com>
Date: Sat, 07 Feb 2009 00:10:51 -0500

I've recently run into problems with the current rules on "Determining
the MediaType of Responses" in Section 3.8.

Specifically, if there is no @Produces and no Content-Type header set,
the the JAX-RS provider creates a set of possible media types from
MessageBodyWriters that "support" the entity type.

*THIS IS BAD*

Why? Adding a new provider can drastically change the behavior or your
application.

I have a writer that just does object.toString() for all text/*:

@Produces("text/*")
public class ToStringProvider implements MessageBodyWriter
{
    boolean isWritable(...)
    {
        return true;
    }
}

I come along later in the app development cycle and introduce a provider
that can generate YAML from any Java Object:

@Produces({"text/yaml", "text/x-yaml", "application/x-yaml"})
public class YamlProvider implements MessageBodyWriter
{
    boolean isWritable(...)
    {
        return true;
    }
}

Now, instead of writing text/plain, I'm not outputting YAML. A
seemingly innocent introduction of a provider change the whole behavior
of the application (unknowingly).

-- 
Bill Burke
JBoss, a division of Red Hat
http://bill.burkecentral.com