欢迎页面有四个按钮分别实现get、post、put、delete四种传输方式
并返回响应消息
程序中用的ajax来改变传输方式
在controller类中响应
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>欢迎页</title>
<script src="webjars/jquery/3.6.0/dist/jquery.min.js"></script>
<script>
function get(){
$.ajax({
url:"/Index/RESTful",
type:"get",
success:function(text){
window.alert(text);
}
})
}
function post(){
$.ajax({
url:"/Index/RESTful",
type:"post",
success:function(text){
window.alert(text);
}
})
}
function put(){
$.ajax({
url:"/Index/RESTful",
type:"put",
success:function(text){
window.alert(text);
}
})
}
function delet(){
$.ajax({
url:"/Index/RESTful",
type:"delete",
success:function(text){
window.alert(text);
}
})
}
</script>
</head>
<body>
<h1>欢迎页</h1>
<button id="get" onclick="get()">GET</button>
<button id="delete" onclick="delet()">DELETE</button>
<button id="put" onclick="put()">PUT</button>
<button id="post" onclick="post()">POST</button>
</body>
</html>
indexController
package com.igeek.controller;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/Index")
public class IndexController {
@GetMapping("/RESTful")
public String getRESTful(){
return "点击了Get";
}
@DeleteMapping("/RESTful")
public String deleteRESTful(){
return "点击了delete";
}
@PutMapping("/RESTful")
public String putRESTful(){
return "点击了put";
}
@PostMapping("/RESTful")
public String postRESTful(){
return "点击了post";
}
}
效果图