欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot2 Controller接收参数的方式

程序员文章站 2024-03-23 17:59:34
...

Controller

package com.zz.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zz.entity.User;

@RestController
@RequestMapping("test")
public class TestController {
	
	//路径传值
	@RequestMapping("t1/{p1}")
	public Map T1(@PathVariable("p1") String parameter1){
		Map map=new HashMap();
		map.put("rs", parameter1);
		return map;
	}
	//?传值
	@RequestMapping("t2")
	public Map T2(@RequestParam("p2") String parameter2){
		Map map=new HashMap();
		map.put("rs", parameter2);
		return map;
	}
	
	//Body参数
	//application/jason
	@RequestMapping("t3")
	public Map T3(@RequestBody User person){
		Map map=new HashMap();
		map.put("rs", person.getName());
		return map;
	}
	
	//无注解(form提交)
	//form-data
	@RequestMapping("t4")
	public Map T4(User person){
		Map map=new HashMap();
		map.put("rs", person.getName());
		map.put("rs2", person.getPwd());
		return map;
	}
	
	//请求头参数以及Cookie
	@RequestMapping("t5")
	public Map t5(@RequestHeader(name="myHeader") String myHeader,
					@CookieValue(name="myCookie") String myCookie){
		
		System.out.println("myHeader"+myHeader);
		System.out.println("myCookie"+myCookie);
		Map map=new HashMap();
		map.put("rs", myHeader);
		map.put("rs2",myCookie);
		return map;
	}
	 
//	表单的参数写在Controller相应的方法的形参中
//	适用于get方式提交,不适用于post方式提交。
	@RequestMapping("t6")
	public Map t6(String name,String pwd){
		Map map=new HashMap();
		map.put("rs", name);
		map.put("rs2",pwd);
		return map;
	}
}

HTML

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
  <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
  <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script>
</head>
<body>

<div class="container">
  <h2>测试</h2>
  <button type="button" id="t1" class="btn">t1</button>
  <button type="button" id="t2" class="btn btn-primary">t2</button>
  <button type="button" id="t3" class="btn btn-secondary">t3</button>
  <button type="button" id="t4" class="btn btn-success">t4</button>
  <button type="button" id="t5" class="btn btn-info">t5</button>
  <button type="button" id="t6" class="btn btn-warning">t6</button>
</div>
<script type="text/javascript">
$(document).ready(function(){
	$("#t1").click(function(){
		$.get("test/t1/jacky", function(data){
	        alert("Data Loaded: " + data.rs);
	});
	})
	
	$("#t2").click(function(){
		$.get("test/t2?p2=Kangkang", function(data){
	        alert("Data Loaded: " + data.rs);
	});
	})
	
	$("#t3").click(function(){
		var data={"name":"LinHao","pwd":"123"}
		$.ajax({
			url:"test/t3",
			async:false,
			type:"POST",
			contentType:"application/json",
			dateType:"json",
			data:JSON.stringify(data),
			success:function(data){
				 alert("Data Loaded: " + data.rs);
			}
				
	});
	})
	
	$("#t4").click(function(){
		$.ajax({
			url:"test/t4?name=jacky&pwd=123",
			async:false,
			type:"POST",
			contentType:"form-data",
			dateType:"json",
			success:function(data){
				 alert("Data Loaded: " + data.rs+data.rs2);
			}
				
	});
	})
	//暂时不通
	$("#t5").click(function(){
		var data={"name":"LinHao","pwd":"123"}
		$.ajax({
			url:"test/t5",
			async:false,
			type:"POST",
			contentType:"application/json",
			dateType:"json",
			headers:{

                'Header':"from header",
                'Cookie':'sdfs'
				
			},
			data:JSON.stringify(data),
			success:function(data){
				 alert("Data Loaded: " + data.rs+data.rs2);
			}
				
	});
	})
	
	$("#t6").click(function(){
		$.get("test/t6?name=Kangkang&pwd=567", function(data){
	        alert("Data Loaded: " + data.rs);
	});
	})
	
	


});

</script>

</body>
</html>