一、@RequestMapping 快捷方式
@GetMapping = @RequestMapping(method = RequestMethod.GET)
@PostMapping = @RequestMapping(method = RequestMethod.POST)
@PutMapping = @RequestMapping(method = RequestMethod.PUT)
@DeleteMapping = @RequestMapping(method = RequestMethod.DELETE)
二、@RequestMapping 使用
/**
* 调用地址:localhost:8080/home/one
* 该@RequestMapping 请求方式等价于:@GetMapping(value = "hello")
* 同理,post 请求使用方式也是如此
*/
@RequestMapping(value = "/hello", method= RequestMethod.GET)
public String hello() {
return "Hello !";
}
/**
* 调用多个 URL
* 调用地址:localhost:8080/home/ 或 localhost:8080/home/one 或 localhost:8080/home/helloone
* 该@RequestMapping 请求方式等价于:@GetMapping(value = {"", "one", "helloone"})
* 同理,post 请求使用方式也是如此
*/
@RequestMapping(value = {"", "one", "helloone"}, method= RequestMethod.GET)
public String helloOne() {
return "Hello, One !";
}
三、@RequestParam 使用
/**
* 通过 @RequestParam 可以将前端发出的请求参数同后端处理方法的参数绑定在一起,其参数各为value,required,defaultValue
*
* value 参数是请求的参数名
*
* required 参数作用是定义了请求参数值是否是必须要传,默认为 true
* 若该值为 true,则定义请求参数值是必须要传的,该调用地址必为 ocalhost:8080/home/two?id=123
* 其中 123 就是从前端获取的请求参数
* 若该值为 false, 则调用地址:localhost:8080/home/two 或 localhost:8080/home/two?id=123
*
* defaultValue 参数就是用来给取值为空的请求参数提供一个默认值的
* 同理,post 请求使用方式也是如此
*/
@RequestMapping(value = "two")
public String helloTwo(@RequestParam(value="id", required=false, defaultValue="000") String twoId) {
return "Hello Two " + twoId;
}
四、PathVaraible 使用
/**
* 通过 @PathVaraible 用来处理动态的 URL, URI 的值可以作为控制器中处理方法的参数
* 调用地址:http://localhost:8080/home/three/news/123 ,其中 news, 123 从前端获取的值
* 同理,post 请求使用方式也是如此
*/
@RequestMapping(value = "three/{type:[a-z]+}/{id}", method = RequestMethod.GET)
public String helloThree(@PathVariable("type") String type, @PathVariable("id") String threeId){
return "Hello Three " + type + " " + threeId;
}
五、@RequestBody 使用
1、建立 save-user.html 文件,代码如下:
<html>
<head>
<title>保存</title>
</head>
<body>
<script>
function save() {
var datas = '{"name": "xiaoxu", "age": "23", "phone": "1234567890"}';
$.ajax({
type : 'POST',
contentType : 'application/json',
url : "xxx/xxx/user/save",
processData : false,
dataType : 'json',
data : datas,
success : function(data) {
alert(data);
},
});
};
</script>
</body>
</html>
1、建立 UserDomain.java 文件,代码如下:
public class UserDomain {
private String name;
private int age;
private String phone;
// Getter and Setter 方法
public User() { super(); }
public User(String name, int password, String phone) {
super();
this.name = name;
this.age = age;
this.phone = phone;
}
}
2、建立 UserController.java 文件,代码如下:
/**
* 功能描述:bean对象传参
* 注意:1、注意需要指定http头为 content-type为application/json
* 2、使用body传输数据
*/
@RequestMapping("user/login")
public Object saveUser(@RequestBody User user){
params.put("user", user);
return params;
}
六、HttpServletRequest 使用
1、建立 login.html 文件,代码如下:
<html>
<head>
<title>register</title>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="login" method="post">
用户名:<input type="text" name="username"><br>
密 码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
2、建立 LoginController.java 文件,代码如下:
@RestController
public class LoginController {
@PostMapping("login")
public void login(HttpServletRequest request){
String username = request.getParameter("username");
String password = request.getParameter("password");
// 判断用户名、密码是否正确,省略
}
}