• Springboot学习笔记


    入门

    创建一个springboot项目

    在这里插入图片描述

    编写一个get接口

    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController //返回是json格式
    public class Hello {
        @GetMapping("/hello")   //get请求的路由  Request Post
        public String Hello() {
            return "get-hello";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    添加依赖

    在pom.xml文件中添加
    例如添加lombok

     <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4

    基础

    web开发

    静态资源访问

    在这里插入图片描述

    其中resources 下的 /static /public /resource /META-INF 文件下的资源为默认静态资源
    在网页中可直接访问
    在这里插入图片描述

    当url相同时候,先请求动态资源,才会请求静态资源
    
    • 1

    改变静态资源默认路径

    spring:
      mvc:
    #    自定义请求静态资源前缀
        static-path-pattern: /res/**
      web:
        resources:
          static-locations:
          #    自定义静态资源文件路径
            [classpath:/stat/]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述
    如出现中文乱码,修改一下配置
    1,
    2 application.properties配置

    server.servlet.encoding.charset=utf-8
    server.servlet.encoding.force=true
    server.servlet.encoding.enabled=true
    
    • 1
    • 2
    • 3

    欢迎页

    静态资源路径下访问index.html
    不能自定义访问前缀
    可以自定义默认文件

      mvc:
        static-path-pattern: /res/**
    
    • 1
    • 2

    否则无法正确访问
    在这里插入图片描述

    请求方式

    Controller.Hello
    四种请求方式

    package com.example.demoday3.Controller;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class Hello {
        @RequestMapping(value = "/user",method = RequestMethod.GET)
        public String getUser(){
            return "getUser";
        }
        @RequestMapping(value = "/user",method = RequestMethod.POST)
        public String postUser(){
            return "postUser";
        }
        @RequestMapping(value = "/user",method = RequestMethod.DELETE)
        public String deleteUser(){
            return "deleteUser";
        }
        @RequestMapping(value = "/user",method = RequestMethod.PUT)
        public String putUser(){
            return "putUser";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    static的index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <form action="/user" method="post">
            <input type="text" name="userName"/>
            <input type="submit">
        </form>
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    因为得form表单method只有get和post方式
    所以要在配置添加

    spring:
      mvc:
        hiddenmethod:
          filter:
    #        能够处理delete和put请求 根据<input type="_method" name="put" 处理另外的请求
            enabled: true
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
     <form action="/user" method="post">
            <input name="_method"  type="hidden" value="delete"/>
            <input type="text" name="userName"/>
            <input type="submit" value=delete>
        </form>
        <form action="/user" method="post">
            <input name="_method"  type="hidden" value="PUT"/>
            <input type="text" name="userName"/>
            <input type="submit" value="put">
        </form>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    如果使用postman,可以直接使用delete、put方式,不用添加以上 filter配置。
    
    • 1

    另外,可以自定义 name="_method"的名称。
    方法如下
    在这里插入图片描述

  • 相关阅读:
    LeetCode刷题——有序矩阵中第 K 小的元素#378#Medium
    Cisco Packet Tracer 模拟器实现一些交换机的基本配置
    ISO三体系的流程及必要性
    Spring小记
    Array和ArrayList有什么区别呢?
    安装模块失败?一篇文章教你如何解决掌握镜像安装~
    计算机网络 基础面试第一弹
    青大数据结构【2013】
    java中的MVC设计模式、模型层 model 主要处理数据、视图层 view 显示数据、控制层 controller 处理业务逻辑
    Android UI freeze分析
  • 原文地址:https://blog.csdn.net/weixin_51277037/article/details/125428061