I achieve this either through defining different MVC handlers, or by reflecting on the content-type and then deciding what to return.
Defining Different Handlers
If you specify a @RequestMapping
that produces
some value, then that will be the type on your Content-Type
header regardless of the automatic negotiation that lead your request there. You can 'force' requests to those handlers by being the only ones available to answer. I use this to return a more-specific type, but I suspect that you can also use it in order to return a more generic one as well.
@RequestMapping(value="/sparql/service", produces={"application/rdf+xml;charset=utf-8", MediaType.ALL_VALUE})
public @ResponseBody String serviceDescriptionAsRdfXml()
{
return null; // something here
}
@RequestMapping( value="/sparql/service", produces={"text/turtle;charset=utf-8"} )
public @ResponseBody String serviceDescriptionAsTurtle( final HttpServletRequest request )
{
return null; // something here
}
Reflecting on the Content-Type
To reflect on the type coming in, and produce something more generic, then you can actually retrieve a list of MediaType
objects as part of your request, then use a ResponseEntity
to define what the Content-Type
will be for your result. This requires a little more thought.
@RequestMapping(value="/sparql/query", method=RequestMethod.GET)
public ResponseEntity<String> queryViaGet(@RequestHeader(value="Accept") final List<MediaType> contentTypes)
{
MediaType.sortBySpecificityAndQuality(contentTypes);
// Do some stuff to select your content type and generate your response
final String results = null;
final MediaType desiredType = null;
// Create your REST response
final HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(desiredType);
return new ResponseEntity<String>(results, responseHeaders, HttpStatus.OK);
}