So, I have a Spring MVC application, and I want a way to determine when a resource doesn’t support a specific media type. I was thinking of doing this with custom View Resolvers… If none of the view resolvers find a successful match for the view name, then throw an exception triggering Unsupported media type (HTTP code of 406).
This mostly makes sense because most view resolvers will return null if it can’t resolve the given view name. The problem arises with view resolvers like InternalResourceViewResolver which ALWAYS returns a view, even if the given view name doesn’t exist.
Just as a reference, here is what my controllers look like:
@RequestMapping(value = "/viewTest", method = RequestMethod.GET)
public ModelAndView getViewData() {
Map<String, Object> myModel = new HashMap<String, Object>();
myModel.put("rightNow", (new Date()).toString());
return new ModelAndView("foo", "model", myModel);
}
Thanks!
You can get most of this with ContentNegotiatingViewResolver, though it sounds like you’ll have to extend
InternalResourceViewResolverif you want it to behave differently depending on the absence of a particular view template.