目前常见的JSON技术:
- json-lib:比较依赖第三方包,在功能上和性能上都不能满足现代化的需求
- Jackson:是springmvc内置的JSON转换工具。性能上比json-lib相对高点
- Gson:是目前功能最全的JSON解析器。性能比FastJson稍差。
- FastJson:是一个用Java语言编写的高性能的JSON处理器。特点是无需依赖,不需要额外的jar文件,能够直接在JDK上运行,运行速度最快。
用JackJson技术返回数据的步骤: 1、准备jar包:
- jackson-databind-2.9.6.jar
- jackson-core-2.9.6.jar
- jackson-annotations-2.9.6.jar
2、配置springmvc.xml文件:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--
配置自动扫描的包
该标签是对包进行扫描,实现注解驱动Bean的定义,同时将Bean自动注入容器中使用,
即标注了SpringMVC注解(如@Controller)的Bean生效。
若没有配置该标签,那么标注@Controller的Bean仅仅是一个普通的JavaBean,
而不是一个可以处理请求的控制器
-->
<context:component-scan base-package="com.springmvc"/>
<!--
MVC注解驱动
该标签会自动注册DefaultAnnotationHandlerMapping(处理器映射器)
与AnnotationMethodHandlerAdapter(处理器适配器)两个Bean。
SpringMVC需要通过这两个Bean实例来完成对@Controller和@ResquestMapping等注解的支持,
从而找出URL与handle method的关系并予以关联。
-->
<mvc:annotation-driven/>
<!--
该元素将在SpringMVC上下文中定义一个DefaultServletHttpRequestHandler,
它会对进入DispatcherServlet的请求进行筛查。
如果发现是没有经过映射的请求, 就将该请求交给Web应用服务器默认的Sevlet处理。
如果不是静态资源的请求, 才会由DispatcherServlet继续处理。
如果没有添加该元素,Web容器启动时会抛出如下异常:
“No mapping found for HTTP request with URI...”
-->
<mvc:default-servlet-handler />
</beans>
3、配置web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--配置SpringMVC的前端控制器DispatcherServlet-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--初始化参数,配置SpringMVC配置文件的位置及名称-->
<init-param>
<param-name>contextConfigLocation</param-name>
<!--在src目录下创建SpringMVC配置文件springmvc.xml-->
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--表示容器在启动时,立即加载dispatcherServlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置HiddenHttpMethodFilter,可将POST请求转为DELETE或PUT请求-->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
4、UserController类代码:
package com.springmvc.controller;
import com.springmvc.entity.Address;
import com.springmvc.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* UserController是一个基于注解的控制器,可同时处理多个请求,
* 并无需实现任何接口
* org.springframework.stereotype.Controller注解用于知识该类是一个指示器
*/
@Controller
/**
* @RequestMapping注解标注在类上时,
* 该类的所有方法都将映射为相对于类级别的请求
*/
@RequestMapping(value = "/user")
public class UserController {
/**
* 返回类型是Collection<User>,
* 但是用了@ResponseBody注解后,
* 返回类型就转变为JSON格式了
*/
@ResponseBody
/**
* @RequestMapping注解标注在方法上时,该方法将成为一个请求处理方法, 它会在程序接收到对应的URL请求时被调用。
*/
@RequestMapping(value = "/returnJson")
public Collection<User> returnJson() {
Map<Integer, User> userMap = new HashMap<>();
userMap.put(1, new User("张三", "123456", new Address("beijing", "beijing")));
userMap.put(2, new User("李四", "123456", new Address("beijing", "beijing")));
userMap.put(2, new User("王五", "123456", new Address("beijing", "beijing")));
return userMap.values();
}
}
5、User类代码:
package com.springmvc.entity;
/**
* @author YuusukeUchiha
* @date 2020/7/5 8:29
* @ide IntelliJ IDEA
*/
public class User {
private String loginName;
private String loginPwd;
private Address address;
public User(String loginName, String loginPwd, Address address) {
this.loginName = loginName;
this.loginPwd = loginPwd;
this.address = address;
}
@Override
public String toString() {
return "User{" +
"loginName='" + loginName + '\'' +
", loginPwd='" + loginPwd + '\'' +
", address=" + address +
'}';
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getLoginPwd() {
return loginPwd;
}
public void setLoginPwd(String loginPwd) {
this.loginPwd = loginPwd;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
6、Address类代码:
package com.springmvc.entity;
/**
* @author YuusukeUchiha
* @date 2020/7/5 8:28
* @ide IntelliJ IDEA
*/
public class Address {
private String province;
private String city;
public Address(String province, String city) {
this.province = province;
this.city = city;
}
@Override
public String toString() {
return "Address{" +
"province='" + province + '\'' +
", city='" + city + '\'' +
'}';
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
7、index.jsp文件代码:
<%--
Created by IntelliJ IDEA.
User: GaoPing
Date: 2020/6/30
Time: 23:45
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>SpringMVC的入门程序</title>
</head>
<body>
<h3>SpringMVC的入门程序</h3>
<div>
<ul>
<li>
<a href="javascript:void(0)" id="returnJson" onclick="getUserJson()">获取JSON数据</a>
</li>
</ul>
</div>
<script type="text/javascript" src="js/jquery-3.4.0.min.js"></script>
<script>
function getUserJson() {
var url = "/user/returnJson";
var args = {};
$.post(url, args, function (data) {
console.log("成功了~~~");
console.log(data);
console.log("成功了~~~");
});
}
</script>
</body>
</html>