怎样从SpringMVC返回json数据

Srping3中配置

maven依赖pom.xml

需要jackson库的依赖

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>

SpringMVC配置

        <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter" />
            </list>
        </property>
    </bean>
    <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>apolication/json; charset=UTF-8</value>
            </list>
        </property>
    </bean>

接口

注意必须用@ResponseBody返回

    @RequestMapping(value = "/resource", method = RequestMethod.GET)
    @ResponseBody
    public List<Resource> getResource() {

        return resourceDao.findAll();
    }

Spring4配置

在Spring4中配置有所不同,否则返回错误

Caused by: java.lang.ClassNotFoundException: org.springframework.http.converter.json.MappingJacksonHttpMessageConverter

maven依赖pom.xml

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-hppc</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-joda</artifactId>
            <version>${jackson.version}</version>
        </dependency>

SpringMVC配置

注意是MappingJackson2HttpMessageConverter,而不是MappingJacksonHttpMessageConverter

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter" />
            </list>
        </property>
    </bean>
    <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>apolication/json; charset=UTF-8</value>
            </list>
        </property>
    </bean>

接口

这里配置一样

    @RequestMapping(value = "/resource", method = RequestMethod.GET)
    @ResponseBody
    public List<Resource> getResource() {

        return resourceDao.findAll();
    }

测试

用firefox的RESTClient插件进行测试
怎样从SpringMVC返回json数据_第1张图片

你可能感兴趣的:(spring,mvc,json)