Spring Boot 对请求的映射
在SpringBoot中对请求资源的映射有三种方式:
1.通过编写一个Controller请求,获得客户端发送过来的请求就转发出去
//通过这种方式可以来映射页面请求路径
@PostMapping("/hello")
public String hello(){ return "login";//如果使用thymeleaf引擎,会去classpath:/public找对应的html
}
2.通过实现WebMvcConfigurerAdapter但是在2.x.x中已过时
@Override
public void addViewControllers(ViewControllerRegistry registry) { // super.addViewControllers(registry);
//浏览器请求 首页,将跳转到 thymeleaf下的login页面。
registry.addViewController("/index.html").setViewName("login");
registry.addViewController("/").setViewName("login");
registry.addViewController("/main.html").setViewName("dashboard");
} /**
* 通过另一种方式来配置虚拟路径
* @return adapter
*/
//所有的WebMvcConfigurerAdapter组件都会一起起作用
@Bean //添加到Spring容器中
public WebMvcConfigurerAdapter MyWebMvcConfigurerAdapter(){ //内部类
WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
registry.addViewController("/index.html").setViewName("login");
} //WebMvcConfigurerAdapter中注册拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").
excludePathPatterns("/index.html","/","/user/login","/webjars/**","/asserts/**");
} };
return adapter; }
3.通过实现WebMvcConfigurer方式,复写addInterceptors方法,和第二种方式一样(推荐)
Spring Boot 对请求的映射的更多相关文章
- Spring Boot核心技术之Rest映射以及源码的分析
Spring Boot核心技术之Rest映射以及源码的分析 该博客主要是Rest映射以及源码的分析,主要是思路的学习.SpringBoot版本:2.4.9 环境的搭建 主要分两部分: Index.ht ...
- 【spring boot】使用@Value映射properties文件属性
描述 使用@Value映射properties文件属性到Java字段 重点 使用@PropertySource 注解指定*.properties文件位置: 使用@Value进行注入: my.prope ...
- Spring boot data JPA数据库映射关系 : @OneToOne,@OneToMany,@ManyToMany
问题描述 在利用Spring boot data JPA进行表设计的时候,表对象之间经常存在各种映射关系,如何正确将理解的映射关系转化为代码中的映射关系是关键之处. 解决办法 概念理解 举例:在公司的 ...
- spring boot 中的路径映射
在spring boot中集成thymeleaf后,我们知道thymeleaf的默认的html的路径为classpath:/templates也就是resources/templates,那如何访问这 ...
- spring boot 并发请求,其他系统接口,丢失request的header信息【多线程、线程池、@Async 】
场景:一次迭代在灰度环境发版时,测试反馈说我开发的那个功能,查询接口有部分字段数据是空的,后续排查日志,发现日志如下: feign.RetryableException: cannot retry d ...
- Spring Boot 异步请求和异步调用,一文搞定
一.Spring Boot中异步请求的使用 1.异步请求与同步请求 特点: 可以先释放容器分配给请求的线程与相关资源,减轻系统负担,释放了容器所分配线程的请求,其响应将被延后,可以在耗时处理完成(例如 ...
- Spring注解@RequestMapping请求路径映射问题
@RequestMapping请求路径映射,假设标注在某个controller的类级别上,则表明訪问此类路径下的方法都要加上其配置的路径.最经常使用是标注在方法上.表明哪个详细的方法来接受处理某次请求 ...
- spring boot(9)-mybatis关联映射
一对多 查询type表的某一条数据,并且要同时查出所有typeid与之配置的user,最终要得到一个以下类型的Type对象 public class Type { String id; String ...
- spring boot yaml 自定义配置 映射到 java POJO
只需要一个注解就ok: @ConfigurationProperties("user.other") “user.other” 这个值匹配的是user下的other对象 yaml ...
随机推荐
- 【原创】大叔经验分享(43)logstash设置jdbc_default_timezone后报错
logstash6.6.0-6.6.2版本使用jdbc input plugin时如果设置了jdbc_default_timezone,会报错: { 2012 rufus-scheduler inte ...
- VUE 导出Excel(iframe)
1. 概述 1.1 说明 在开发过程中,有时候需要导出某数据表格(excel)以便客户使用,使用iframe对返回二进制文件进行下载处理.记录此功能,以便后期使用. 2. 示例 2.1 vue示例代码 ...
- python计算文件的行数的方法
1.简单方法把文件读入一个大的列表中,然后统计列表的长度. count = len(open("文件名").readlines()) print count 2.读取文件某一行 ...
- query string parameters 、 Form Data 、 Request Payload
微信小程序ajax向后台传递参数的时候总是报400错误 然后看了一下network 发现是query string parameters,但是我写的header如下 header:{ "Co ...
- JS使用小记
1. JSON解析undefined JSON.parse(undefined) VM4456:2 Uncaught SyntaxError: Unexpected token u 2. 事件传值 o ...
- apache基础
apache基于多域名的虚拟主机 NameVirtualHost *:80<VirtualHost *:80> DocumentRoot "/var/www/html/xk/sh ...
- C# .Net List<T>中Remove()、RemoveAt()、RemoveRange()、RemoveAll()的区别,List<T>删除汇总
在List<T>中删除主要有Remove().RemoveAt().RemoveRange().RemoveAll()这几个方法.下面一一介绍使用方法和注意点. 我们以List<st ...
- PHP开发API接口签名及验证
<?php // 设置一个密钥(secret),只有发送方,和接收方知道 /*----发送方和接收方- start ----*/ $secret = "28c8edde3d61a041 ...
- python面试题整理
1.谈谈你对csrf的理解和django中CSRF防护机制. 什么是 CSRF CSRF, Cross Site Request Forgery, 跨站点伪造请求.举例来讲,某个恶意的网站上有一个指向 ...
- es elasticsearch-head安装
---恢复内容开始--- 参考 https://www.jianshu.com/p/36d7f97a20cd 1.下载安装git clone git://github.com/mobz/elastic ...