SpringBoot第四课——常用传参方式

本文介绍了SpringBoot中三种常见的参数传递方式:1) path传参,通过在URL路径中携带参数;2) AJAX通过JSON传参,需要注意JSON对象属性与Java对象属性对应;3) AJAX提交表单,说明了不能直接使用@RequestBody接收非JSON格式参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

接着第一课:https://blog.youkuaiyun.com/sinat_22808389/article/details/81590042

1.path传参 

在RestController 下添加如下方法:

//path 传参数 get请求方式
    @RequestMapping(value = "/postPath")
    public String postByPath(@RequestParam(value = "id")Integer id) {
    	System.out.println(id);
    	return "id" + id;
    }

浏览器输入http://localhost:8080/postPath?id=2 

可在后台看到打印的id为2,(这里也可以不要@RequestParam(value = "id"),也可传参进来)

2.ajax通过json传参:

//接收json参数  同时json转为java对象
    @CrossOrigin(origins="*",allowCredentials="true")
    @RequestMapping(value = "/postJson",method = RequestMethod.POST)
    public String postByJson(@RequestBody User user) {
    	System.out.println(user.toString());
    	return "post success!" + user;
    }

User类如下:

public class User {

	private String name;
	private String age;
	private String addr;
// get() set() toString()..

}

前端postjson.html如下:


<!DOCTYPE html>
<html >
<head>
<meta charset="utf-8">
<title>crossdomain</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/echarts.min.js"></script>
</head>
<body >
   <div id = "data" ></div>
<script type="text/javascript">
var data = JSON.stringify({name:"xyp",age:18});
//ajax提交数据
$.ajax({
    async:false,
    type:"post",
    url:"http:localhost:8080/postJson",
    data:data, 
    contentType: "application/json;charset=UTF-8",
    dataType:"json", 
    success:function(data){
       $("#data").text(data);
    },
    error:function(){
        $("#data").text("数据请求失败");
    }
    });
   
</script>
</body>
</html>

 浏览器运行postjson.html,可看到后台如下

 

 可看到name,age都传进来了,但是addr为空的,这是为什么呢?这是因为前端传的只有name,age,这里的json转java是通过反射去赋值的,json里没有addr所以就不会去赋值,但是如果json里有的属性而java对象没有,可想而知会报错,因为java通过反射去设置值的时候找不到那个属性。

 3.ajax提交表单:

postForm.html:

<!DOCTYPE html>
<html style="height: 100%; margin: 0">
<head>
<meta charset="utf-8">
<title>crossdomain</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/echarts.min.js"></script>
</head>
<body >
   <form id = "form" >
    <input type="text" name="name" value="xyp"></input> 
    <input type="text" name="age" value="18"></input> 
   </form>
   <div id = "data"></data>
<script type="text/javascript">
//ajax 提交form
$.ajax({
    async:false,
    type:"post",
    url:"http:localhost:8080/postForm",
    data:$("#form").serialize(), 
    contentType: "application/json;charset=UTF-8",
    success:function(data){
       $("#data").text(data);
    },
    error:function(){
        $("#data").text("error");
    }
    });
   
</script>
</body>
</html>

后端在RestController添加如下方法:

   @CrossOrigin(origins="*",allowCredentials="true")
    @RequestMapping(value = "/postForm",method = RequestMethod.POST)
    public String postByForm(@RequestBody String user) {
    	System.out.println(user.toString());
    	return user.toString();
    }

浏览器打开 postForm.html可在后台看到:

 

这里不能用@RequestBody User user来接收,因为传过来的不是json格式的参数。

更详细的参数传递可参考:https://www.cnblogs.com/harrychinese/p/springboot_mvc_view_function.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值