微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

接着第一课:https://blog.csdn.net/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格式的参数。

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐