说一下怎么通过springmvc返回json格式的数据,
第一步,首先是导入依赖(jar包)
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.3.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.0</version> <scope>provided</scope> </dependency>
<!--json返回json的依赖-->
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.1</version> </dependency>
2.然后这里是我的实体类:get set方法就不写了
private Date birthday; private double money; private String email; public Person() { }
3.springmvc配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--配置扫描器--> <context:component-scan base-package="com.controller"></context:component-scan> <!--视图解析器--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--前缀--> <property name="prefix" value="/"></property> <!--后缀--> <property name="suffix" value=".jsp"></property> </bean> <mvc:annotation-driven></mvc:annotation-driven> </beans>4.我的controller类:
//要想返回数据,需要定义一个@ResponseBody @ResponseBody @RequestMapping("getPersons") public List<Person> getPersons(){//这里就定义一些死的数据: List<Person> la=new ArrayList<Person>(); for (int i = 0; i <10 ; i++) { la.add(new Person(new Date(),2000,"123@qq.com")); }//注意:如果你是用类似于easyui这种ui框架来返回得到数据,一般就是返回一个Map集合和总页数的那种格式就ok了。// Map<String, Object> map=new HashMap<String, Object>(); // map.put("total", 页数); // map.put("rows", 集合名);return la; }
5.然后我的前台:
<<a href="/getPersons.action">获取数据</a> <br/>
点击这个连接,就会返回一些json的数据格式:
[{"birthday":1515826972550,"money":2000.0,"email":"123@qq.com"},{"birthday":1515826972550,"money":2000.0,"email":"123@qq.com"},{"birthday":1515826972550,"money":2000.0,"email":"123@qq.com"},{"birthday":1515826972550,"money":2000.0,"email":"123@qq.com"},{"birthday":1515826972550,"money":2000.0,"email":"123@qq.com"},{"birthday":1515826972550,"money":2000.0,"email":"123@qq.com"},{"birthday":1515826972550,"money":2000.0,"email":"123@qq.com"},{"birthday":1515826972550,"money":2000.0,"email":"123@qq.com"},{"birthday":1515826972550,"money":2000.0,"email":"123@qq.com"},{"birthday":1515826972550,"money":2000.0,"email":"123@qq.com"}]