春天休息json输出

时间:2013-11-27 18:50:54

标签: java json spring rest jackson

我创建了一个spring应用程序,当我打开以下链接时它会成功运行:localhost:8080 / test / greeting。服务器已完全部署,但是当我打开该链接时,会下载一个名为greeting的文件,其中集成了json。我试图在Internet Explorer上打开链接,我认为json会直接出现在屏幕上。情况不是这样吗?

这是调度程序servlet:

<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd     
                        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="test" />
    <mvc:annotation-driven />
</beans>

这是web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

这是GreetingController代码:

package test;

import java.util.concurrent.atomic.AtomicLong;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping("/greeting")
    public @ResponseBody Greeting greeting(
            @RequestParam(value="name", required=false, defaultValue="World") String name) {
        return new Greeting(counter.incrementAndGet(),
                            String.format(template, name));
    }
}

我一直在寻找,但一直无法找到解决方案。

2 个答案:

答案 0 :(得分:1)

似乎没有为响应设置正确的内容类型。如果要将默认响应内容类型和转换器设置为JSON,除了返回定义上的@ResponseBody注释外,还可以将以下bean定义添加到spring上下文文件中。

<bean id="jacksonMessageConverter"class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
      <property name="messageConverters">
           <list>
              <ref bean="jacksonMessageConverter" />
           </list>
       </property>
</bean>

您还需要将Jackson添加到类路径中以处理实际的Java&lt; =&gt; JSON转换。 如果这是您默认需要的,它可以帮助您避免使用为每个请求映射定义方法手动将内容类型设置为JSON所需的样板代码。

答案 1 :(得分:0)

管理得出它只是Internet Explorer,在其他浏览器中运行良好!