DigitalZebra
DigitalZebra

Reputation: 41553

When/where is it ideal to determine Unsupported Media Type in Spring?

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!

Upvotes: 0

Views: 456

Answers (2)

RicoZ
RicoZ

Reputation: 845

You can also use the @RequestHeader annotation to define which media types are supported by your method.

Upvotes: 0

earldouglas
earldouglas

Reputation: 13473

You can get most of this with ContentNegotiatingViewResolver, though it sounds like you'll have to extend InternalResourceViewResolver if you want it to behave differently depending on the absence of a particular view template.

Upvotes: 1

Related Questions