在spring mvc3中,已经集成了Jackson(json处理器)来处理数据输出json格式,spring中封装的类是 org.springframework.http.converter.json.MappingJackson2HttpMessageConverter这个json转换器, 如果是springmvc3.2之前的版本,可以使用org.springframework.http.converter.MappingJacksonHttpMessageConverter这个json转换器。
Spring的详细介绍:这里
Json的详细介绍:这里
Spring mvc的详细介绍:这里
1.首先要添加maven的项目依赖,要在spring mvc中使用json的话,需要在项目中的pom.xml中包含Jackson的依赖项。
示例:
<dependencies> <dependency> <groupId>com.fasterxml</groupId> <artifactId>jackson-annotations</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>com.fasterxml</groupId> <artifactId>jackson-core</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>com.fasterxml</groupId> <artifactId>jackson-databind</artifactId> <version>2.1.1</version> </dependency> <!-- Spring 3 dependencies --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-instrument</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-instrument-tomcat</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.2.0.RELEASE</version> </dependency> </dependencies>
2.建立一个POJO类,就以User类为例,这里就贴User类的代码了,大家都懂。
3.通过@Controller注解建立控制器
加上@ResponseBody
代码如下所示:
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.Student; @Controller public class TestJsonController { @RequestMapping(value="/finddata", method = RequestMethod.GET) public @ResponseBody Student getJSONData() { Student result = new Student(); result.setUserName("360sdn.com"); result.setArray(new String[]{"360sdn1", "360sdn2"}); return result; } }
4.配置Spring的servlet配置文件来配置json的输出转换器,但是在我的实际实践中发现不用配置Xml中的bean也能将数据转换为json格式的。
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- 启动spring mvc的注解功能,完成请求和注解POJO的映射 --> <!-- 配置信息转换,将用@responsebody注解的返回值转换为json返回前台, 编码为utf-8--> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </list> </property> </bean> <mvc:annotation-driven/> </beans>
然后运行应该就能看到json格式的数据了。
时间: 2024-12-15 03:01:28