-
/public
-
/resources
-
/META-INF/resources
这几个目录需要建立在类路径下,若如此做,则放置在这些目录下的静态资源可以被直接访问到。
也可以通过配置来设置资源的访问前缀:
spring.mvc.static-path-pattern=/res
此时若想访问静态资源,就必须添加res前缀才行。
我们还可以修改Spring Boot的默认资源路径,只需添加配置:
spring.web.resources.static-locations=classpath:/myImg
若如此做,则我们只能将静态资源放在myImg目录下,之前的所有静态资源目录都将失效。
欢迎页
Spring Boot提供了两种方式来实现欢迎页,第一种便是在资源目录放置欢迎页:
SpringBoot Index!
访问结果:
第二种方式是通过Controller处理/index请求:
@Controller
public class HelloController {
@RequestMapping(“/”)
public String toIndex(){
return “hello”;
}
}
Favicon
Spring Boot也提供了自动设置网站图标的方式,只需要将名为 favicon.ico
的图片放在静态资源目录下即可:
Rest映射
在Spring Boot中,默认已经注册了HiddenHttpMethodFilter,所以可以直接编写Rest风格的url,只需在表单中添加一个_method属性的请求域即可:
编写Controller处理请求:
@RestController
public class HelloController {
@GetMapping(“/user”)
public String getUser(){
return “Get”;
}
@PostMapping(“/user”)
public String postUser(){
return “Post”;
}
@DeleteMapping(“/user”)
public String deleteUser(){
return “Delete”;
}
@PutMapping(“/user”)
public String putUser(){
return “Put”;
}
}
最后需要在配置文件中开启对Rest的支持:
spring.mvc.hiddenmethod.filter.enabled=true
04
常用参数及注解
下面介绍Web开发中的一些常用参数和注解。
@PathVariable
该注解用于获取路径变量,比如:
@GetMapping(“/user/{id}”)
public String getUser(@PathVariable(“id”) Integer id){
return id + “”;
}
此时若请求url为http://localhost:8080/user/2,则获取到id值为2。
@RequestHeader
该注解用于获取请求头,比如:
@GetMapping(“/header”)
public String getHeader(@RequestHeader(“User-Agent”) String userAgent){
return userAgent;
}
它还能够通过一个Map集合获取所有的请求头信息:
@GetMapping(“/header”)
public Map<String, String> getHeader(@RequestHeader Map<String,String> headers){
return headers;
}
@RequestParam
该注解用于获取请求参数,比如:
@GetMapping(“/param”)
public String getParam(@RequestParam(“name”) String name,
@RequestParam(“age”) Integer age){
return name + “:” + age;
}
此时若请求url为http://localhost:8080/param?name=zhangsan&age=20,则得到值 zhangsan:20
。
@CookieValue
该注解用于获取Cookie值,比如:
@GetMapping(“/cookie”)
public String getCookie(@CookieValue(“Idea-8296e76f”) String cookie) {
return cookie;
}
它还可以通过Cookie键名获取一个Cookie对象:
@GetMapping(“/cookie”)
public String getCookie(@CookieValue(“Idea-8296e76f”) Cookie cookie) {
return cookie.getName();
}
@RequestBody
该注解用于获取获取请求体的值,比如:
@PostMapping(“/body”)
public String getBody(@RequestBody String content) {
return content;
}
既然是获取请求体的值,那么只有Post请求才有请求体,所以编写一个表单:
账号:
密码:
通过该表单提交数据后,得到 username=admin&password=123
。
@RequestAttribute
该注解用于获取request域的数据,比如:
@GetMapping(“/success”)
public String success(@RequestAttribute(“msg”) String msg){
return msg;
}
通过键名即可获取request域中的数据。
@MatrixVariable
该注解用于获取矩阵变量,比如:
@GetMapping(“/matrix/{path}”)
public String getMatrix(@MatrixVariable(“name”) String name,
@MatrixVariable(“age”) Integer age,
@PathVariable(“path”) String path) {
return path + “—” + name + “:” + age;
对于该注解的使用,需要注意几点,首先矩阵变量是绑定在路径中的,所以请求映射中一定要携带一个${path};其次在SpringBoot中默认禁用掉了矩阵变量的功能,所以我们还需要手动去开启该功能:
@Configuration
public class MyConfig {
@Bean
public WebMvcConfigurer webMvcConfigurer(){
return new WebMvcConfigurer() {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
};
}
}
此时访问请求url:http://localhost:8080/matrix/test;name=zhangsan;age=20, 得到结果:test---zhangsan:20
。
05
拦截器
一个完善的Web应用一定要考虑安全问题,比如,只有登录上系统的用户才能查看系统内的资源,或者只有具备相关权限,才能访问对应的资源,为此,我们需要学习一下拦截器,通过拦截器我们就能够实现这些安全认证。
这里以登录检查为例:
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession();
Object user = session.getAttribute(“user”);
if(user != null){
return true;
}
response.sendRedirect(“/toLogin”);
return false;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
编写好拦截器后需要将其配置到容器中:
@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor())
.addPathPatterns(“/**”)
.excludePathPatterns(“toLogin”, “/css/", "/js/”, “/fonts/", "/images/”);
}
}
需要指定该拦截器需要拦截哪些资源,需要放行哪些资源,这样一个简单的登录校验就完成了。
06
文件上传
Spring Boot中该如何实现文件上传呢?现有如下的一个表单:
编写控制方法:
@RestController
public class FileController {
@PostMapping(“/upload”)
public String upload(@RequestPart(“f”) MultipartFile file){
String name = file.getOriginalFilename();
long size = file.getSize();
return name + “:” + size;
}
}
通过@RequestPart注解即可将上传的文件封装到MultipartFile中,通过该对象便可以获取到文件的所有信息。输出结果:
若是上传多个文件,则先修改表单信息:
在文件框位置添加multiple属性即可支持多文件上传,然后修改控制器代码:
@PostMapping(“/upload”)
public String upload(@RequestPart(“f”) MultipartFile[] file){
return file.length + “”;
}
若是需要将上传的文件保存到服务器,则可以如此做:
@PostMapping(“/upload”)
public String upload(@RequestPart(“f”) MultipartFile[] file) throws IOException {
for (MultipartFile multipartFile : file) {
if(!multipartFile.isEmpty()){
String filename = multipartFile.getOriginalFilename();
multipartFile.transferTo(new File(“E:\” + filename));
}
}
return “success”;
}
因为Spring Boot默认的文件上传大小限制为1MB,所以只要文件稍微大了一点就会上传失败,为此,可以修改SpringBoot的默认配置:
spring.servlet.multipart.max-file-size=30MB # 配置单个文件上传大小限制
spring.servlet.multipart.max-request-size=100MB # 配置总文件上传大小限制
07
错误处理
默认情况下,SpringBoot应用出现了异常或错误会自动跳转至/error页面,也就是这个熟悉的页面:
然而一般情况下,我们都不会选择出异常时显示这个页面,而是想要显示我们自己定制的页 需要zi料+ 绿色徽【vip1024b】
面,为此,我们可以在/static或/templates目录下新建一个error目录,并在/error目录下放置命名为4xx、5xx的页面,SpringBoot会自动帮助我们解析。
此时当出现5xx的异常时,SpringBoot会自动跳转至5xx.html页面,当然你也可以对每个状态码都做一个页面进行对应,比如放置500.html、501.html、502.html文件,当服务器出现对应的异常时,就会跳转至对应的页面。
08
数据层
下面, 我们将探究SpringBoot与数据访问层框架的整合与使用。Spring Boot 与其他第三方中间件整合的技术文章也发布过,我整理成了 PDF,关注微信公众号「Java后端」回复「666」下载这一本技术栈手册。
JDBC
若想使用原生的JDBC进行开发,SpringBoot已经为我们配置好了JDBC的相关信息,只需要引入依赖:
org.springframework.boot
spring-boot-starter-data-jdbc
mysql
mysql-connector-java
5.1.49
Spring Boot 底层自动配置了HikariDataSource数据源,所以我们只需指定数据源的地址、用户名和密码即可:
spring.datasource.url=jdbc:mysql:
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
因为SpringBoot已经自动配置好了JdbcTemplate,所以我们直接使用就可以了:
@SpringBootTest
class SpringbootApplicationTests {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
void contextLoads() {
List names = jdbcTemplate.queryForList(“select name from student”,String.class);
for (String name : names) {
System.out.println(name);
}
}
}
Druid
若是不想使用Spring Boot底层的数据源,我们也可以修改默认配置,以Druid数据源为例,首先引入依赖:
com.alibaba
druid-spring-boot-starter
1.1.10
并对Druid进行配置:
# 开启Druid的监控页功能
spring.datasource.druid.stat-view-servlet.enabled=true
# 开启防火墙功能
spring.datasource.druid.filter-class-names=stat,wall
# 配置监控页的用户名和密码
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=123
# 开启Druid的Web监控功能
spring.datasource.druid.web-stat-filter.enabled=true
# 配置监控哪些请求
spring.datasource.druid.web-stat-filter.url-pattern=…
此时访问http://localhost:8080/druid,将会来到Druid的监控页:
MyBatis
接下来我们将整合MyBatis框架,并介绍它的简单使用。首先引入依赖:
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.4
然后编写Mapper接口:
@Mapper
public interface StudentMapper {
Student getStu(Integer id);
}
编写Mappe配置文件:
<?xml version="1.0" encoding="UTF-8"?>select * from student where id = #{id}
最后配置一下MyBatis:
# 配置Mapper配置文件的位置
mybatis.mapper-locations=classpath:mappers
这样就可以使用MyBatis了:
@SpringBootTest
class SpringbootApplicationTests {
@Autowired
private StudentMapper studentMapper;
@Test
void contextLoads() {
Student stu = studentMapper.getStu(1);
System.out.println(stu);
总结
面试前的“练手”还是很重要的,所以开始面试之前一定要准备好啊,不然也是耽搁面试官和自己的时间。
我自己是刷了不少面试题的,所以在面试过程中才能够做到心中有数,基本上会清楚面试过程中会问到哪些知识点,高频题又有哪些,所以刷题是面试前期准备过程中非常重要的一点。
面试题及解析总结
大厂面试场景
知识点总结
/dependency>
然后编写Mapper接口:
@Mapper
public interface StudentMapper {
Student getStu(Integer id);
}
编写Mappe配置文件:
<?xml version="1.0" encoding="UTF-8"?>select * from student where id = #{id}
最后配置一下MyBatis:
# 配置Mapper配置文件的位置
mybatis.mapper-locations=classpath:mappers
这样就可以使用MyBatis了:
@SpringBootTest
class SpringbootApplicationTests {
@Autowired
private StudentMapper studentMapper;
@Test
void contextLoads() {
Student stu = studentMapper.getStu(1);
System.out.println(stu);
总结
面试前的“练手”还是很重要的,所以开始面试之前一定要准备好啊,不然也是耽搁面试官和自己的时间。
我自己是刷了不少面试题的,所以在面试过程中才能够做到心中有数,基本上会清楚面试过程中会问到哪些知识点,高频题又有哪些,所以刷题是面试前期准备过程中非常重要的一点。
面试题及解析总结
[外链图片转存中…(img-KQg9J4kp-1710342035055)]
大厂面试场景
[外链图片转存中…(img-mNHtmIYC-1710342035056)]
知识点总结
[外链图片转存中…(img-pRz2Ja4b-1710342035056)]