I am using Spring MVC 3.2.2.RELEASE, and this is my first attempt at using Spring's Java based configuration (@Configuration
).
I have a controller that I use to handle certain files. The content of the files is read by my service method MyContentService.getResource(String)
. At the moment, the content type is always text/html
.
How can I config my Spring MVC application so that it correctly sets the type of the content returned? The content type can only be determined at runtime.
My controller at the moment, which incorrectly always sets the type to text/html
:
@Controller
public class MyContentController {
MyContentService contentService;
@RequestMapping(value = "content/{contentId}")
@ResponseBody
public byte[] index(@PathVariable String contentId) {
return contentService.getResource(contentId);
}
}
EDIT:
The following method works (with PNG and JPEG files), but I am not comfortable with URLConnection
determining the content type (e.g. PDF, SWF, etc):
@RequestMapping(value = "content/{contentId}/{filename}.{ext}")
public ResponseEntity<byte[]> index2(@PathVariable String contentId, @PathVariable String filename, @PathVariable String ext, HttpServletRequest request) throws IOException {
byte[] bytes = contentService.getResource(contentId);
String mimeType = URLConnection.guessContentTypeFromName(filename + "." + ext);
if (mimeType != null) {
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.valueOf(mimeType));
return new ResponseEntity<byte[]>(bytes, headers, HttpStatus.OK);
} else {
logger.warn("Unable to determine the mimeType for " + getRequestedUrl(request));
return new ResponseEntity<byte[]>(HttpStatus.NOT_FOUND);
}
}