【问题标题】:json response is not returned using spring 3.1使用spring 3.1不返回json响应
【发布时间】:2013-03-09 10:15:01
【问题描述】:

我正在尝试使用 ResponseBody 返回 json 和 xml 响应,它确实适用于 xml,但不返回 json。我对 xml 的请求 uri 是 '../home.xml' 而对于 json 是来自控制器方法的 '../home.json':

@RequestMapping("home.*")
public  @ResponseBody Message homeOther(HttpServletRequest request, HttpServletResponse response, ModelMap mv){
    Message msg = new Message();
    msg.setDetail("I am here at home");
    msg.setUploadDate(new Date());

    mv.addAttribute("message", msg);

    return msg;
}

这里是调度器 servlet:

<context:component-scan base-package="com.ym"/>

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="alwaysUseFullPath" value="true"/>
</bean>


<bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
    <property name="resourceLoaderPath" value="/views"/>
</bean>

<!-- Simple ViewResolver for Velocity, appending ".vm" to logical view names -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
    <property name="order" value="2" />
    <property name="cache" value="true"/>
    <property name="prefix" value=""/>
    <property name="suffix" value=".vm"/>
    <property name="toolboxConfigLocation" value="/WEB-INF/toolbox.xml"/>
    <!-- if you want to use the Spring Velocity macros, set this property to true -->
    <property name="exposeSpringMacroHelpers" value="true"/>
    <property name="contentType" value="text/html; charset=UTF-8" />
</bean>

<bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller" />


<bean id="xmlView" class="org.springframework.web.servlet.view.xml.MarshallingView"
      >
      <constructor-arg ref="xstreamMarshaller" />
</bean>

<bean id="jsonView" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jsonHttpMessageConverter" />
            <ref bean="marshallingHttpMessageConverter" />
        </list>
    </property>
</bean>

<bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
    <property name="supportedMediaTypes" value="application/json" />
</bean>

<bean id="marshallingHttpMessageConverter"
      class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
    <property name="marshaller" ref="xstreamMarshaller"/>
    <property name="unmarshaller" ref="xstreamMarshaller"/>
</bean>



<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
  <property name="order" value="1" />
  <property name="mediaTypes">
    <map>
       <entry key="json" value="application/json" />
       <entry key="xml" value="application/xml" />
    </map>
  </property>

  <property name="defaultViews">
    <list>
      <!-- JSON View -->
      <bean
        class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
      </bean>

      <ref bean="xmlView" />
     </list>
  </property>
  <property name="ignoreAcceptHeader" value="true" />

</bean>

这是 XML 响应:

<com.ym.mongodb.model.Message>
 <messageId>0</messageId>
 <detail>I am here at home</detail>
 <uploadDate>2013-03-09 09:56:46.606 UTC</uploadDate>
</com.ym.mongodb.model.Message>

我的问题是: 1.配置有什么用?为什么它不返回json响应。? 2. 为什么它返回的xml,显示Message的完全限定名? 我正在使用 Spring 3.1。

编辑: 有趣的是,当请求类型设置正确时,它确实会正确创建 json n xml。但是第二个问题仍然存在。

【问题讨论】:

  • 使用了哪些请求头?
  • 需要发送请求头Accept=application/json
  • 请求头是他们默认的,我没有改变它的头。另外,我想只调用 url 而不更改标题中的任何内容。可能吗?感谢您的快速回复。
  • 不,因为返回类型是通过Accept 标头决定的,您能做一件事吗,使用浏览器开发工具/firebug 检查 ajax 请求,然后查看请求标头部分下的内容是 Accept 的值
  • 它的应用程序/xhtml+xml

标签: java xml json spring


【解决方案1】:

我不知道你想详细实现什么,所以我不知道我是否可以提供帮助,但我会试一试。

首先,您可以尝试添加接受标头。下面是一个例子。

@RequestMapping(value = "/home/yourpath", method = RequestMethod.GET,
headers = "Accept=application/xml, application/json")

通过添加接受标头 json 和 xml 请求将在调用您的方法和解析数据时被接受,例如使用 curl 客户端。另外,也许您应该考虑明确设置请求方法。

当您向客户端发送请求时,请确保根据您发送的内容,将“Content-Type: application/json”或“Content-Type: application/xml”添加到请求的标头中。否则,您将收到不支持的内容类型错误,因为您可能发送了错误的内容类型。

在 Spring 中有两种实际显示正确内容类型的方法。

1:通过解析/接受请求标头中的正确内容类型(通过配置客户端中的接受标头)。

2:通过配置文件扩展名(有一些默认的文件扩展名应该是开箱即用的。xml 和 json 被覆盖。)。尝试将 .xml 或 .json 添加到 uri 的末尾。

看看这里:http://static.springsource.org/spring/docs/3.1.0.RELEASE/reference/htmlsingle/#mvc-multiple-representations

看下面的例子: http://www.mkyong.com/spring-mvc/spring-3-mvc-contentnegotiatingviewresolver-example/

我无法帮助您解决第二个问题。

我希望这会有所帮助。祝你好运。

亲切的问候, 克里斯

【讨论】:

  • 感谢您的回复。是的,我需要添加内容类型以确保解决第一个问题,而无需对我显示的其余代码进行太多更改。我试图在不定义 Content-Type 的情况下修复它。但看起来那是有必要定义的。同样,它也确实解决了第二个问题。
猜你喜欢
  • 2017-02-17
  • 2019-07-27
  • 1970-01-01
  • 2018-02-13
  • 1970-01-01
  • 1970-01-01
  • 2022-07-29
  • 2021-02-20
  • 1970-01-01
相关资源
最近更新 更多