开发者

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 b开发者_如何学JAVAecause 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 InternalResourceViewResolver if you want it to behave differently depending on the absence of a particular view template.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜