Search This Blog

Sunday, September 13, 2015

What's the difference between text/xml vs application/xml for webservice response?

XML has two MIME types, application/xml and text/xml. These are often used interchangeably, but there is a subtle difference which is why application/xml is generally recommended over the latter.

Let me explain why: according to the standard, text/*-MIME types have a us-ascii character set unless otherwise specified in the HTTP headers. This effectively means that any encoding defined in the XML prolog (e.g. <?xml version=”1.0” encoding=”UTF-8”?>) is ignored. This is of course not the expected and desired behavior.

To further complicate matters, most/all browser implementations actually implement nonstandard behavior for text/xml because they process the encoding as if it were application/xml.

application/xml is generally the preferred mime type. For text/xml it appears the encoding will generally be treated as us ascii regardless of what is specified in the XML document header (unless otherwise specified in the HTTP headers).

From the RFC (3023), under section 3, XML Media Types:

If an XML document -- that is, the unprocessed, source XML document -- is readable by casual users, text/xml is preferable to application/xml. MIME user agents (and web user agents) that do not have explicit support for text/xml will treat it as text/plain, for example, by displaying the XML MIME entity as plain text. Application/xml is preferable when the XML MIME entity is unreadable by casual users.

So, text/* has encoding issues, and is not implemented by browsers in a standards-compliant manner, which is why using application/* is recommended.

RESTfull valid return types!

Valid return types is general includes:

  • void
  • byte[] 
  • String
  • Application-Supplied JAXB classes and javax.xml.bunding.JAXBElement
  • MultivaluedMap<String,String>
  • Response, which can set status property
  • GenericEntity

RESTfull @QueryParam and @PathParam, What it Can be and Can't be!

  • All primitive types except char.
  • All wrapper classes of primitive types except Character.
  • Any class with the static method named valueOf(String) that accepts a single String argument.
  • Any class with constructorthat takes a single String as a parameter
  • List<T>, Set<T>, SortedSet<T>, where T matches the already listed criteria. Sometimes parameters many contain more than one value for the same name. If this is the case, this types may be used to obtain all values.

DefaultValue annotation may be used to supply a default value for parameters