springmvc json

pom.xml加入两个jar包

<dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-lgpl</artifactId>
            <version>1.8.1</version>
        </dependency>

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-lgpl</artifactId>
            <version>1.8.1</version>
        </dependency>

方法

        @ResponseBody
	@RequestMapping(value="/testJson")
	public Collection<Employee> testJson(){
		return employeeDao.getAll();
	}    

springmvc.xml

<bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean
                    class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                         <!-- 默认是ISO-88859-1,避免乱码这里设置为UTF-8 -->
                            <value>
                                text/html;charset=UTF-8
                            </value>
                        </list>
                    </property>
                </bean>
                <!-- 启动JSON格式的配置 -->
                <bean id="jacksonMessageConverter"
                    class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
                    <!-- 解决 HttpMediaTypeNotAcceptableException: Could not find acceptable 
                        representation -->
                    <property name="supportedMediaTypes">
                        <list>
                            <value>application/json;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
            </list>
        </property>
    </bean>

 

 输出

技术分享

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。