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>

 

 输出

posted on 2015-04-16 10:37  颓废的悠然  阅读(169)  评论(0编辑  收藏  举报

导航