如何定制错误响应

我们在模板引擎的文件夹(templates)下面新建一个error文件夹,我们建立一个 404.html ,以后发生404错误直接会跳到该html页面,我们还可以新建一个 4xx.html ,如果碰到其他的,比如是400/403等错误,会直接跳到该页面,先精准匹配,再模糊匹配,如果碰到403错误,先去error下面找403.html,如果没有的话,再去找4xx.html页面,同理,5xx错误也是一样的。

错误页面能获取哪些消息?

  • timestamp:时间戳
  • status:状态码
  • error:错误提示
  • exception:异常对象
  • message:异常消息
  • errors:JSR303数据校验错误

我们可以直接在页面这样获取:

<h1>status : [[${status}]]</h1>
<h1>message: [[${message}]]</h1>
......
......

配置嵌入式servlet容器

如何定制和修改Servlet容器的相关配置?(因为springboot的tomcat是内嵌的)
springboot 能不能支持其他的servlet容器?

针对第一个问题:

  1. 我们可以通过修改application配置文件来修改,for instance , server.port = 8083这就修改了端口,我们要修改其他的,语法也是一样的。
  2. 我们可以编写一个嵌入式的servlet容器定制器(WebServerFactoryCustomizer),来实现修改端口。
// 这个方法就写在之前我们配置拦截器和转发器的那个配置类中@Beanpublic WebServerFactoryCustomizer webServerFactoryCustomizer(){return new WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>() {@Overridepublic void customize(ConfigurableServletWebServerFactory factory) {factory.setPort(8888);}};}
  1. 那怎么注册servlet的三大组件?(Servlet, Filter, Listener) 下面是个注册servlet的例子:
// 新建一个MyServlet类
public class MyServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.getWriter().write("hello servlet");}
}

再在配置类中注册:

  @Beanpublic ServletRegistrationBean myservlet(){ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myservlet");return registrationBean;}

最后,我们通过在浏览器中访问 localhost:8080/myservlet 就能访问到 hello servlet

再看看注册Filter的例子:

public class MyFilter implements Filter {@Overridepublic void init(FilterConfig filterConfig) throws ServletException {}@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {System.out.println("Filter running ~~~~");filterChain.doFilter(servletRequest,servletResponse);}@Overridepublic void destroy() {}
}

配置类中注册:

 @Beanpublic FilterRegistrationBean myfilter(){FilterRegistrationBean registrationBean = new FilterRegistrationBean();registrationBean.setFilter(new MyFilter());registrationBean.setUrlPatterns(Arrays.asList("/hello","/myservlet"));return registrationBean;}

针对第二个问题,我们怎么引入其他的servlet容器?

如果我们想引入jetty,只需要如下配置:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

使用外置的Servlet容器

嵌入式的 springboot 不支持JSP,需要使用外置的servlet容器才能,此外还要打包成war,不能打包成jar,总结步骤如下:

  1. 创建一个war项目
  2. 将嵌入式的Tomcat指定为provided
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><scope>provided</scope>
</dependency>
  1. 必须编写一个SpringBootServletInitializer的子类,并调用configure方法:
public class ServletInitializer extends SpringBootServletInitializer { @Override  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {  //SpringBootWebJspApplication  这是springboot的启动主程序return application.sources(SpringBootWebJspApplication.class);  }
}

启动的时候,不能像springboot一样启动主配置类,得要启动这个ServletInitializer这个类才是正确的,不然启动不了。

下一章进入Docker环节

深入理解SpringBoot (4)相关推荐

  1. 深入理解SpringBoot(1)

    之前学习过springboot,但是由于很长一段时间没有用到springboot,想重新学习一遍,决定用一段时间来写一系列深入学习springboot的博客,将会从一些注解,官方文档等多个方面来深入理 ...

  2. 【理解springboot自动装配原理】

    理解springboot自动装配原理: 最近读了小马哥(mercyblitz)Springboot编程思想(核心篇),有了一些心得和感悟,分享给大家: 1. 官网介绍了激活自动装配的方法: * 文档提 ...

  3. 你是如何理解SpringBoot Starters的?

    你是如何理解SpringBoot Starters的? SpringBoot官方对我们平时开发过程中涉及到的场景(数据访问,Web开发)封装成一个个的启动器,我们只需要在项目中引入某个场景的启动器,则 ...

  4. 深入理解SpringBoot(2)

    这一章接上面的深入理解SpringBoot1: 1, 配置文件占位符,这个是针对.properties文件的,比如: name=zhangsan${random.uuid} dog_name=${na ...

  5. Reactive(3)5分钟理解 SpringBoot 响应式的核心-Reactor

    目录 一.前言 二. Mono 与 Flux 构造器 三. 流计算 1. 缓冲 2. 过滤/提取 3. 转换 4. 合并 5. 合流 6. 累积 四.异常处理 五.线程调度 小结 参考阅读 一.前言 ...

  6. 这篇带你深入理解SpringBoot中的自动装配(好文精读)

    作者: 聂晨 cnblogs.com/niechen/p/9027804.html SpringBoot的自动装配是拆箱即用的基础,也是微服务化的前提.其实它并不那么神秘,我在这之前已经写过最基本的实 ...

  7. 深入理解 SpringBoot 原理

    官网:Spring Boot 文章目录 1.SpringBoot简介 回顾Spring Spring简化Java开发 什么是SpringBoot SpringBoot的大时代背景 2.HelloWor ...

  8. 深入理解SpringBoot之装配条件

    我们知道自动装配是SpringBoot微服务化的核心,它会把META-INF/spring.factoires里配置的EnableAutoConfiguration注册到IOC容器里.但是,请大家考虑 ...

  9. 深入理解 SpringBoot 启动机制(starter 机制)

    一.前言 使用过springboot的同学应该已经知道,springboot通过默认配置了很多框架的使用方式帮我们大大简化了项目初始搭建以及开发过程.本文的目的就是一步步分析springboot的启动 ...

  10. 深度理解springboot集成cache缓存之源码解析

    一.案例准备 1.创建数据表(employee表) 2.创建Employee实体类封装数据库中的数据 @AllArgsConstructor @NoArgsConstructor @Data @ToS ...

最新文章

  1. docker-compose报错:(root) Additional property mail-service is not allowed
  2. Oracle查询优化-07日期运算
  3. JavaScript实现knuth-morris-pratt(KMP)算法(附完整源码)
  4. Google Code Jam 2015 Round 1A Mushroom Monster 水
  5. MongoDB误删表恢复
  6. python中format函数用法简书_Python format 格式化函数
  7. 2020-08-22 OpenWRT 脚本修改网卡MAC
  8. Sklearn fit , transform ,fit_transform
  9. solidworks工程图剖视图没有从默认的A开始解决办法
  10. JVM垃圾回收机制,万字详解
  11. Travis CI(持续集成)
  12. 手机聊天页面 html5,HTML5仿手机微信聊天界面
  13. 我喜欢的作品风格: 韩国daksha作品IF YOU GO AWAY…
  14. 如何防止木马性图片上传
  15. 卸载的软件电脑重启后又出现了,怎么办?
  16. jenkins自动化_通过Jenkins自动化PSR合规性
  17. 《用户体验设计:100堂入门课》20190920
  18. 毕业后我这操蛋的五年
  19. 全球最大的Spark+AI峰会发放优惠码SAIS20TRAIN,培训费优惠20%!
  20. 关于装有WIN7系统的硬盘转移到USB3.0移动硬盘盒后开机启动蓝屏(代码7B)的解决方案

热门文章

  1. web服务器集群(多台web服务器)后session如何同步和共享
  2. SQL Server 2008 R2 数据库安装
  3. 安装配置Collabnet_Subversion
  4. 适合于小团队产品迭代的APP测试流程 1
  5. [置顶] J2EE (八) 策略模式+装饰模式+反射(java)
  6. Fedora 18在ASUS N6系列电脑上以太网卡驱动的安装
  7. 5月第二周全球五大顶级域名总量新增10.5万个
  8. HMC支持管理服务器数量及适合POWER6服务器的HMC型号
  9. IE Automation Tabs
  10. bzoj4195 noi2015 day1 t1