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

Springboot学习笔记

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

Springboot学习笔记

Springboot学习
  • 入门
    • 创建一个springboot项目
    • 编写一个get接口
    • 添加依赖
  • 基础
    • web开发
      • 静态资源访问
      • 改变静态资源默认路径
      • 欢迎页
    • 请求方式

入门 创建一个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";
    }
}
添加依赖

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

 
      org.projectlombok
      lombok

基础 web开发 静态资源访问

其中resources 下的 /static /public /resource /META-INF 文件下的资源为默认静态资源
在网页中可直接访问

当url相同时候,先请求动态资源,才会请求静态资源
改变静态资源默认路径
spring:
  mvc:
#    自定义请求静态资源前缀
    static-path-pattern: /res/**
  web:
    resources:
      static-locations:
      #    自定义静态资源文件路径
        [classpath:/stat/]


如出现中文乱码,修改一下配置
1
2 application.properties配置

server.servlet.encoding.charset=utf-8
server.servlet.encoding.force=true
server.servlet.encoding.enabled=true
欢迎页

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

  mvc:
    static-path-pattern: /res/**

否则无法正确访问

请求方式

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";
    }
}

static的index.html




    
    Title


    

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

spring:
  mvc:
    hiddenmethod:
      filter:
#        能够处理delete和put请求 根据 
 
如果使用postman,可以直接使用delete、put方式,不用添加以上 filter配置。

另外,可以自定义 name="_method"的名称。
方法如下

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

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

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