码迷,mamicode.com
首页 > 编程语言 > 详细

springmvc json

时间:2015-04-16 11:57:00      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

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>

 

 输出

技术分享

springmvc json

标签:

原文地址:http://www.cnblogs.com/tuifeideyouran/p/4431326.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!