
是一种开发风格,遵从此风格开发软件,符号REST风格,则RESTFUL
两个核心要求
- 每个资源都有唯一的访问路径(访问标识)
- 不同的行为(增删改查),使用对应的请求方式(get、post、put、delete)
| 请求方式 | 标识 | 意图 |
|---|---|---|
| GET | http://localhost:8080/xxx/users | 查询所有用户 |
| POST | http://localhost:8080/xxx/users | 在所有用户中增加一个 |
| PUT | http://localhost:8080/xxx/users | 在所有用户中修改一个 |
| DELETE | http://localhost:8080/xxx/users/1 | 删除用户1 |
| GET | http://localhost:8080/xxx/users/1 | 查询用户1 |
| GET | http://localhost:8080/xxx/users/1/orders | 查询用户1的所有订单 |
| POST | http://localhost:8080/xxx/users/1/orders | 在用户1的所有订单中增加一个 |
以上,查询——GET、增加——POST、修改——PUT、
2. 优点看url就知道要什么
看http method(请求方式)就知道干什么
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>springMVC07artifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<dependencies>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.4.6version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>compilescope>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jsp-apiartifactId>
<version>2.0version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.2.9version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.9.8version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.3.16version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.3.16version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>5.1.6.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aspectsartifactId>
<version>5.1.6.RELEASEversion>
dependency>
dependencies>
<build>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>*.xmlinclude>
<include>**/*.xmlinclude>
includes>
<filtering>truefiltering>
resource>
resources>
build>
project>
- web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>mvc_xmqservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:mvc.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>mvc_xmqservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
web-app>
- mvc.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
>
<context:component-scan base-package="com.lyx"/>
<mvc:annotation-driven>mvc:annotation-driven>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/">property>
<property name="suffix" value=".jsp">property>
bean>
<mvc:default-servlet-handler>mvc:default-servlet-handler>
beans>
3.2 定义Rest风格的Controller
- 访问路径都一样,注解不同
package com.lyx.web;
import com.alibaba.druid.support.json.JSONUtils;
import com.lyx.entity.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
@RestController
public class RESTController {
@GetMapping("/users")
public List<User> test(){
System.out.println("你好啊");
User user = new User(1, "lyx", "1233", 2, new Date());
User user1 = new User(2, "xmq", "3434", 1, new Date());
return Arrays.asList(user,user1);
}
@GetMapping("/users/{id}")
public User test1(@PathVariable int id){
User user = new User(id, "lll", "get1", 2, new Date());
return user;
}
@PostMapping("/users")
public String saveUser(@RequestBody String user){
// User parse = (User)JSONUtils.parse(user);
// System.out.println(parse);
System.out.println(user);
System.out.println("this is post");
return "ok";
}
@PutMapping("/users")
public String updateUsers(@RequestBody String user){
System.out.println(user);
System.out.println("this is put");
return "ok";
}
@DeleteMapping("/users/{id}")
public String deleteOne(@PathVariable int id){
System.out.println(id);
System.out.println("this is delete");
return "ok";
}
}
3.3 AJAX请求
- 访问路径一样,ajax的type=get(查)/post(增)/put(改)/delete(删)
<%--
Created by IntelliJ IDEA.
User: deqi5
Date: 2022/5/9
Time: 11:10
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Titletitle>
<script src="static/js/jquery-3.5.1.js">script>
head>
<body>
<input type="button" value="hhh" onclick="hhh();">
<input type="button" value="query" onclick="query();">
<input type="button" value="saveUser" onclick="saveUser();">
<input type="button" value="updateUser" onclick="updateUser();">
<input type="button" value="deleteUser" onclick="deleteUser();">
<script>
function hhh() {
$.ajax({
url:"${pageContext.request.contextPath}/users",
type:"get",
success:function (ret) {
console.log(ret)
}
});
}
function query(){
$.ajax({
url:"${pageContext.request.contextPath}/users/1",
type:"get",
success:function (ret) {
console.log(ret)
}
});
}
function saveUser() {
var a = {id:3,username:"lyx",password:"34r34",gender:1,registTime:new Date()}
$.ajax({
url:"${pageContext.request.contextPath}/users",
type:"post",
data:JSON.stringify(a),
contentType:"application/json",
success:function (ret) {
console.log(ret)
}
});
}
function updateUser() {
var a = {id:1,username:"lyx",password:"34r34",gender:1,registTime:new Date()}
$.ajax({
url:"${pageContext.request.contextPath}/users",
type:"put",
data:JSON.stringify(a),
contentType:"application/json",
success:function (ret) {
console.log(ret)
}
});
}
function deleteUser() {
$.ajax({
url:"${pageContext.request.contextPath}/users/1",
type:"delete",
success:function (ret) {
console.log(ret)
}
});
}
script>
body>
html>
3.4 Tomcat运行
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)