在 Spring-MVC 控制器中支持多种内容类型 [英] Supporting multiple content types in a Spring-MVC controller

查看:29
本文介绍了在 Spring-MVC 控制器中支持多种内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rails 控制器使支持多种内容类型变得非常容易.

A Rails controller makes it very easy to support multiple content types.

respond_to do |format|
  format.js { render :json => @obj }
  format.xml
  format.html
end

漂亮.在一个控制器操作中,我可以轻松地响应多种内容类型,并且可以灵活地响应我希望呈现的内容,无论是模板还是对象的序列化形式等.

Beautiful. In one controller action I can easily respond to multiple content types with plenty of flexibility as to what I wish to render, be it a template, a serialized form of my object, etc.

我可以在 Spring-MVC 中做类似的事情吗?Spring 支持多种内容类型的标准是什么?我见过涉及视图解析器的解决方案,但这看起来很难管理,特别是如果我想除了 xhtml 和 xml 之外还支持 JSON.

Can I do something similar to this in Spring-MVC? What is the standard for supporting multiple content types in Spring? I've seen solutions involving view resolvers, but this looks difficult to manage, especially if I want to support JSON in addition to xhtml and xml.

感谢任何建议,但更简单和更优雅的解决方案将受到更多人的赞赏;)

Any suggestions are appreciated, but the simpler and more elegant solutions will be appreciated more ;)

编辑

如果我断言视图解析器难以管理是错误的,请随时纠正我并提供示例.最好是可以返回 xml、xhtml 和 JSON 的.

If I'm incorrect in asserting that a view resolver is difficult to manage, please feel free to correct me and provide an example. Preferably one that can return xml, xhtml, and JSON.

推荐答案

在 Spring 3 中,您希望使用 org.springframework.web.servlet.view.ContentNegotiatingViewResolver.

In Spring 3, you want to use the org.springframework.web.servlet.view.ContentNegotiatingViewResolver.

它需要一个媒体类型列表和ViewResolvers.来自 Spring文档:

It takes a list of media type and ViewResolvers. From the Spring docs:

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
  <property name="mediaTypes">
    <map>
      <entry key="atom" value="application/atom+xml"/>
      <entry key="html" value="text/html"/>
      <entry key="json" value="application/json"/>
    </map>
  </property>
  <property name="viewResolvers">
    <list>
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
      </bean>
    </list>
  </property>
  <property name="defaultViews">
    <list>
      <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
    </list>
  </property>
</bean>
<bean id="content" class="com.springsource.samples.rest.SampleContentAtomView"/>

控制器:

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class BlogsController {

    @RequestMapping("/blogs")
    public String index(ModelMap model) {
        model.addAttribute("blog", new Blog("foobar"));
        return "blogs/index";
    }    
}

您还需要包含 Jackson JSON jar.

You'll also need to include the Jackson JSON jars.

这篇关于在 Spring-MVC 控制器中支持多种内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆