基于SpringMVC接受JSON参数详解及常见错误总结
作者:Heinzyang 时间:2022-08-25 16:08:08
最近一段时间不想使用Session了,想感受一下Token这样比较安全,稳健的方式,顺便写一个统一的接口给浏览器还有APP。所以把一个练手项目的前台全部改成Ajax了,跳转再使用SpringMVC控制转发。对于传输JSON数据这边有了更深的一些理解,分享出来,请大家指正。
在SpringMVC中我们可以选择数种接受JSON的方式,在说SpringMVC如何接受JSON之前,我们先聊聊什么是JSON。具体的定义我也不赘述了,在JavaScript中我们经常这样定义JSON 对象
var jsonObject = {
"username":"admin",
"password":123
}
这种形式的我们叫它JSON对象,同时还有一个概念叫做JSON字符串,字符串呢,顾名思义,是由' ‘或者” “包裹起来的一个整体,我们称之为字符串。我们知道字符串是可以直接输出的,而对象不能直接输出。所以在JavaScript中,我们可以
//定义一个对象 jsonObject
var jsonObject = {
"username":"admin",
"password":123
};
alert(jsonObject);
此时,会显示[object Object]而不会输出JSON对象的内容,JavaScript向我们提供了两个工具
JSON.parse() 用于将一个 JSON 字符串转换为 JavaScript 对象。 JSON.stringify() 用于将 JavaScript 值转换为 JSON 字符串。
所以当我们输入
alert(JSON.stringify(jsonObject));
就会显示 {“username”:”admin”,”password”:123};
* 好了 对于JSON的讲解就到这里了 下面我们说一说SpringMVC *
既然JSON有着上述两种存在方式,那我们通过ajax向SpringMVC传值的时候,我们该传哪一种呢?
我们首先尝试直接发送JSON对象
//定义json对象
var username = $("#username").val();
var password = $("#password").val();
var json = {
"username" : username,
"password" : password
};
// Jquery Ajax请求
$.ajax({
url : "jsontest",
type : "POST",
async : true,
data : json,
dataType : 'json',
success : function(data) {
if (data.userstatus === "success") {
$("#errorMsg").remove();
} else {
if ($("#errorMsg").length <= 0) {
$("form[name=loginForm]").append(errorMsg);
}
}
}
});
我们首先想想SpringMVC提供了什么给我们,有一个@RequestParam的注解,对于这个注解,它的作用和我们Servlet中的request.getParameter是基本相同的。我们首先使用这个注解来获取
@RequestMapping("/jsontest")
public void test(@RequestParam(value="username",required=true) String username,
@RequestParam(value="password",required=true) String password){
System.out.println("username: " + username);
System.out.println("password: " + password);
}
后台成功输出的我们的参数,成功接受!
SpringMVC如此智能,如果我们去除@RequestParam注解,直接将两个值放入会有什么后果?
@RequestMapping("/jsontest")
public void test(String username,String password){
System.out.println("username: " + username);
System.out.println("password: " + password);
}
竟然同样成功了,原理我这里就不多赘述了,有兴趣的朋友们可以打断点看看。
SpringMVC提供了一个@RequestBody,它是用来处理前台定义发来的数据Content-Type: 不是application/x-www-form-urlencoded编码的内容,例如application/json, application/xml等;
细心的朋友们或许发现了,在之前的Ajax中,我们没有定义Content-type的类型,Jquery默认使用application/x-www-form-urlencoded类型。那么意思就是SpringMVC的@RequestParam注解,Servlet的request.getParameter是可以接受到以这种格式传输的JSON对象的。
为什么呢!?GET请求想必大家都不陌生,它将参数以url?username=”admin”&password=123这种方式发送到服务器,并且request.getParameter可以接收到这种参数,我们在浏览器地址栏上也可以看到这一点。而我们Ajax使用的POST,并且发送的是JSON对象,那么后台是如何获取到的呢?答案就在于这个Content-Type x-www-form-urlencoded的编码方式把JSON数据转换成一个字串,(username=”admin”&password=123)然后把这个字串添加到url后面,用?分割,(是不是和GET方法很像),提交方式为POST时候,浏览器把数据封装到HTTP BODY中,然后发送到服务器。所以并不会显示在URL上。(这段可能有点绕口,希望大家用心理解一下。)
终于说完了,长吐一口气。所以说我们使用@RequestBody注解的时候,前台的Content-Type必须要改为application/json,如果没有更改,前台会报错415(Unsupported Media Type)。后台日志就会报错Content type ‘application/x-www-form-urlencoded;charset=UTF-8' not supported,这些错误Eclipse下Tomcat是不会显示错误信息的,只有使用了日志才会显示,如何配置日志大家可以看我上一篇文章。接下来我们正确配置一下,上面说到了 Content-Type需要更改,同时我们的data也要更改了,这种注解方式只接受JSON字符串而不是JSON对象
$.ajax({
url : "jsontest",
type : "POST",
async : true,
contentType : "application/json",
data : JSON.stringify(json),
dataType : 'json',
success : function(data) {
if (data.userstatus === "success") {
$("#errorMsg").remove();
} else {
if ($("#errorMsg").length <= 0) {
$("form[name=loginForm]").append(errorMsg);
}
}
}
});
后台也更改一下,json其实可以理解为键值对嘛,所以我们用Map接收,然后对字符串或者其他数据类型进行进一步处理。
@RequestMapping("/jsontest")
public void test(@RequestBody(required=true) Map<String,Object> map ){
String username = map.get("username").toString();
String password = map.get("password").toString();
System.out.println("username: " + username);
System.out.println("password: " + password);
}
同时,我又想起了神奇的SpringMVC,所以我决定去掉注解试试,好的,果断被爆了一个空指针错误…尝试就此打住。
SpringMVC还提供了参数直接和POJO绑定的方法,我们来尝试一下。前台一样,就不贴出来了。
@RequestMapping("/jsontest")
public void test(@RequestBody User user ){
String username = user.getUsername();
String password = user.getPassword();
System.out.println("username: " + username);
System.out.println("password: " + password);
}
OK,这次是可以取到值的,我个人对于登录这类小数据量的上传来说不太喜欢这种方法,User里面的变量很多,我只用了其中两个,没有必要去创建一个User对象,一般数据量小的时候我还是比较喜欢使用单独取值出来的。我们再想一想,如果是在上传JSON对象的情况下,我们可不可以绑定POJO呢,答案是可以的,不要使用@RequestParam注解,否则会报Required User parameter 'user' is not present错误。到此讲解基本结束了,下面来总结一下。
我们首先说了JSON对象和JSON字符串
然后说了SpringMVC接受两种两种JSON格式的时候,前端ContentType的设定,和后端是否使用注解接受,还提到了一点Servlet。
当Ajax以application/x-www-form-urlencoded格式上传即使用JSON对象,后台需要使用@RequestParam 或者Servlet获取。 当Ajax以application/json格式上传即使用JSON字符串,后台需要使用@RquestBody获取。
这是我实验了一天的一些总结,希望可以帮助到大家,如果有错误,请各位海涵并指正。
来源:https://blog.csdn.net/LostSh/article/details/68923874
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android 消息机制以及handler的内存泄露
Java web的读取Excel简单实例代码
![](https://img.aspxhome.com/file/2023/6/78946_0s.png)
Spring框架构造注入type属性实例详解
![](https://img.aspxhome.com/file/2023/6/105966_0s.png)
Maven本地jar引用的实现方法
Unity实现切割图集工具
![](https://img.aspxhome.com/file/2023/4/82124_0s.jpg)
Java设计模式之抽象工厂模式浅析讲解
Mybatis Limit实现分页功能
![](https://img.aspxhome.com/file/2023/6/61646_0s.png)
SpringBoot如何读取war包jar包和Resource资源
Java与SpringBoot对redis的使用方式
![](https://img.aspxhome.com/file/2023/6/78916_0s.png)
SpringMVC教程之文件上传与下载详解
浅谈c# 浮点数计算
![](https://img.aspxhome.com/file/2023/8/111288_0s.png)
Java中Lambda表达式和函数式接口的使用和特性
Android组件之服务的详解
![](https://img.aspxhome.com/file/2023/7/89557_0s.png)
Android视频播放器屏幕左侧边随手指上下滑动亮度调节功能的原理实现
java实现单词小游戏
![](https://img.aspxhome.com/file/2023/2/59522_0s.png)
关于访问后端接口报404错误问题的解决方法(全网最细!)
![](https://img.aspxhome.com/file/2023/8/72588_0s.png)
使用android隐藏api实现亮度调节的方法
C#实现封面图片生成器的示例代码
![](https://img.aspxhome.com/file/2023/0/96430_0s.png)
C#中Linq查询基本操作使用实例
Java创建型设计模式之抽象工厂模式(Abstract Factory)
![](https://img.aspxhome.com/file/2023/4/101764_0s.png)