一、jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html>
<html lang="zh-CN" style="position: relative;">
<head>
<base href="<%=basePath%>">
</head>
<body>
<div>
<button type="button" "restGet()">rest get读取(Read)</button>
<button type="button" "restPost()">rest post新建(Create)</button>
<button type="button" "restPut()">rest put更新(Update)</button>
<button type="button" "restPatch()">rest patch更新(Update),通常是更新部分字段</button>
<button type="button" "restDelete()">rest delete删除(Delete)</button>
</div>
<script src='static/plugins/aceadmin/assets/js/jquery-2.1.4.min.js'></script>
<script type="text/javascript">
function restGet(){
$.ajax({
type: "get",
url: "/rest/user/admin",
dataType:'json',
cache: false,
success: function(data){
alert(JSON.stringify(data));
}
});
}
function restPost(){
var user = {"userId":"abc",
"username":"abc",
"mobilePhone":"12345678901"};
$.ajax({
type: "post",
contentType:"application/json;charset=UTF-8",
url: "/rest/user",
data: JSON.stringify(user),
dataType:'json',
cache: false,
success: function(data){
alert(data);
}
});
}
function restPut(){
var user = {"userId":"abc",
"username":"",
"mobilePhone":null};
$.ajax({
type: "put",
contentType:"application/json;charset=UTF-8",
data: JSON.stringify(user),
url: "/rest/user",
dataType:'json',
cache: false,
success: function(data){
alert(JSON.stringify(data));
}
});
}
function restPatch(){
var user = {"userId":"abc",
"username":"",
"mobilePhone":null};
$.ajax({
type: "patch",
contentType:"application/json;charset=UTF-8",
data: JSON.stringify(user),
url: "/rest/user",
dataType:'json',
cache: false,
success: function(data){
alert(JSON.stringify(data));
}
});
}
function restDelete(){
$.ajax({
type: "delete",
url: "/rest/user/admin",
dataType:'json',
cache: false,
success: function(data){
alert(data);
}
});
}
</script>
</body>
</html>
二、controller
package com.abc.controller.demo.rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.sinowel.bxtwf.entity.system.user.User;
import com.sinowel.bxtwf.service.system.login.LoginService;
/**
* @Description: REST全称是Representational State Transfer(表述性状态转移),
* 它是Roy Fielding博士在2000年写的一篇关于软件架构风格的论文,此文一出,威震四方!
* 国内外许多知名互联网公司纷纷开始采用这种轻量级的Web服务,大家习惯将其称为RESTful Web Services,或简称REST服务
* @create:2019年4月10日 下午4:36:16
*/
@org.springframework.web.bind.annotation.RestController
@RequestMapping("/rest")
public class RestController {
@Autowired
LoginService loginService;
/**
* @Description:查询所有用户信息
* @param username
* @return
* @throws Exception
* @author:ZhangLiting
* @update:2019年4月10日 下午5:19:10
*/
@GetMapping(value = "/users")
public User getUser() throws Exception {
System.out.println("get,all user");
return null;
}
/**
* @Description:查询用户信息
* @param username
* @return
* @throws Exception
* @update:2019年4月10日 下午5:19:10
*/
@GetMapping(value = "/user/{username}")
public User getUser(@PathVariable("username") String username) throws Exception {
System.out.println("get,username:" + username);
User user = loginService.getUserByUsername(username);
return user;
}
/**
* @Description:新增保存用户信息
* @param user
* @return
* @throws Exception
* @update:2019年4月10日 下午5:19:37
*/
@PostMapping(value = "/user")
public int createUser(@RequestBody User user) throws Exception {
System.out.println("post,create user:" + user);
return 1;
}
/**
* @Description:更新用户信息
* @param username
* @return
* @throws Exception
* @update:2019年4月10日 下午5:19:49
*/
@PutMapping(value = "/user")
public User updateUser(@RequestBody User user) throws Exception {
System.out.println("put,update:" + user);
return user;
}
/**
* @Description:更新用户部分信息
* @param username
* @return
* @throws Exception
* @update:2019年4月10日 下午5:19:21
*/
@PatchMapping(value = "/user")
public User patchUser(@RequestBody User user) throws Exception {
System.out.println("patch,user:" + user);
return user;
}
/**
* @Description:删除用户信息
* @param username
* @return
* @throws Exception
* @update:2019年4月10日 下午5:19:21
*/
@DeleteMapping(value = "/user/{username}")
public int deleteUser(@PathVariable("username") String username) throws Exception {
System.out.println("delete,username:" + username);
return 1;
}
}