- 入门
- 创建一个springboot项目
- 编写一个get接口
- 添加依赖
- 基础
- web开发
- 静态资源访问
- 改变静态资源默认路径
- 欢迎页
- 请求方式
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
基础 web开发 静态资源访问org.projectlombok lombok
其中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"的名称。
方法如下



