栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringMVC----REST设计风格

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringMVC----REST设计风格

1. 开发风格

是一种开发风格,遵从此风格开发软件,符号REST风格,则RESTFUL

两个核心要求

  • 每个资源都有唯一的访问路径(访问标识)
  • 不同的行为(增删改查),使用对应的请求方式(get、post、put、delete)
请求方式标识意图
GEThttp://localhost:8080/xxx/users查询所有用户
POSThttp://localhost:8080/xxx/users在所有用户中增加一个
PUThttp://localhost:8080/xxx/users在所有用户中修改一个
DELETEhttp://localhost:8080/xxx/users/1删除用户1
GEThttp://localhost:8080/xxx/users/1查询用户1
GEThttp://localhost:8080/xxx/users/1/orders查询用户1的所有订单
POSThttp://localhost:8080/xxx/users/1/orders在用户1的所有订单中增加一个

以上,查询——GET、增加——POST、修改——PUT、

2. 优点

看url就知道要什么
看http method(请求方式)就知道干什么

3. 使用 3.1 环境搭建

  1. 导入依赖:jackson、javax.servlet-api、jsp-api、jstl、spring-webmvc
    pom.xml如下:


    4.0.0

    org.example
    springMVC07
    1.0-SNAPSHOT
    war
    
        
            org.mybatis
            mybatis
            3.4.6
        
        
            org.mybatis
            mybatis-spring
            1.3.1
        
        
            mysql
            mysql-connector-java
            5.1.47
        
        
            junit
            junit
            4.12
            compile
        
        
            log4j
            log4j
            1.2.17
        
        
            javax.servlet
            javax.servlet-api
            3.1.0
        
        
            javax.servlet
            jstl
            1.2
        
        
            javax.servlet
            jsp-api
            2.0
        
        
            com.alibaba
            druid
            1.2.9
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.9.8
        
        
            org.springframework
            spring-webmvc
            5.3.16
        
        
            org.springframework
            spring-jdbc
            5.3.16
        
        
            org.springframework
            spring-test
            5.1.6.RELEASE
        
        
            org.springframework
            spring-aspects
            5.1.6.RELEASE
        
    
    
        
            
                src/main/java
                
                    *.xml
                    **/*.xml
                
                true
            
        
    

  1. web.xml


    
        mvc_xmq
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:mvc.xml
        
        1
    
    
        mvc_xmq
        /
    

  1. mvc.xml


    
    
    
        
        
    
    

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 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" %>


    Title
    














3.4 Tomcat运行

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/873856.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号