在Spring Boot中配置web app
文章目录
- 添加依赖
- 配置端口
- 配置Context Path
- 配置错误页面
- 在程序中停止Spring Boot
- 配置日志级别
- 注册Servlet
- 切换嵌套服务器
在Spring Boot中配置web app
本文将会介绍怎么在Spring Boot中创建和配置一个web应用程序。
添加依赖
如果要使用Spring web程序,则需要添加如下依赖:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
配置端口
正如我们之前文章中提到的,要想配置端口需要在application.properties文件中配置如下:
server.port=8083
如果你是用的是yaml文件,则:
server:port: 8083
或者通过java文件的形式:
@Component
public class CustomizationBean implementsWebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {@Overridepublic void customize(ConfigurableServletWebServerFactory container) {container.setPort(8083);}
}
配置Context Path
默认情况下,Spring MVC的context path是‘/’, 如果你想修改,那么可以在配置文件application.properties中修改:
server.servlet.contextPath=/springbootapp
如果是yaml文件:
server:servlet:contextPath:/springbootapp
同样的,可以在java代码中修改:
@Component
public class CustomizationBeanimplements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {@Overridepublic void customize(ConfigurableServletWebServerFactorycontainer) {container.setContextPath("/springbootapp");}
}
配置错误页面
默认情况下Spring Boot会开启一个whitelabel的功能来处理错误,这个功能本质上是自动注册一个BasicErrorController如果你没有指定错误处理器的话。同样的,这个错误控制器也可以自定义:
@RestController
public class MyCustomErrorController implements ErrorController {private static final String PATH = "/error";@GetMapping(value=PATH)public String error() {return "Error haven";}@Overridepublic String getErrorPath() {return PATH;}
}
当然,和之前讲过的自定义服务器信息一样,你也可以自定义错误页面,就像在web.xml里面添加error-page:
@Component
public class CustomizationBeanimplements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {@Overridepublic void customize(ConfigurableServletWebServerFactorycontainer) { container.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/400"));container.addErrorPages(new ErrorPage("/errorHaven"));}
}
通过这个功能,你可以对错误进行更加细致的分类。
在程序中停止Spring Boot
SpringApplication提供了一个静态的exit()方法,可以通过它来关停一个Spring Boot应用程序:
@Autowiredpublic void shutDown(ApplicationContext applicationContext) {SpringApplication.exit(applicationContext, new ExitCodeGenerator() {@Overridepublic int getExitCode() {return 0;}});}
第二个参数是一个ExitCodeGenerator的实现,主要用来返回ExitCode。
配置日志级别
我们可以在配置文件中这样配置日志级别:
logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR
注册Servlet
有时候我们需要将程序运行在非嵌套的服务器中,这时候有可能会需要自定义servlet的情况,Spring Boot 也提供了非常棒的支持,我们只需要在ServletRegistrationBean中,注册servlet即可:
@Beanpublic ServletRegistrationBean servletRegistrationBean() {ServletRegistrationBean bean = new ServletRegistrationBean(new SpringHelloWorldServlet(), "/springHelloWorld/*");bean.setLoadOnStartup(1);bean.addInitParameter("message", "SpringHelloWorldServlet special message");return bean;}
切换嵌套服务器
默认情况下,Spring Boot会使用tomcat作为嵌套的内部服务器,如果想切换成jetty则可以这样:
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jetty</artifactId></dependency>
</dependencies>
exclude自带的Tomcat,并额外添加spring-boot-starter-jetty即可。
本文的例子可参考: https://github.com/ddean2009/learn-springboot2/tree/master/springboot-config-webapp
更多精彩内容且看:
- 区块链从入门到放弃系列教程-涵盖密码学,超级账本,以太坊,Libra,比特币等持续更新
- Spring Boot 2.X系列教程:七天从无到有掌握Spring Boot-持续更新
- Spring 5.X系列教程:满足你对Spring5的一切想象-持续更新
- java程序员从小工到专家成神之路(2020版)-持续更新中,附详细文章教程
更多教程请参考 flydean的博客
在Spring Boot中配置web app相关推荐
- spring boot中配置虚拟路径,用来映射显示图片
增加配置,继承 WebMvcConfigurerAdapter,如下: package com.wm.mogu_picture.config;import org.springframework.be ...
- Spring Boot中配置嵌入式Servlet容器修改配置
目录 基本概念 演示及源码 基本概念 如何修改Spring Boot的默认配置: 1. Spring Boot在自动配置很多组建的时候,先看容器中有没有用户自己配置的(@Bean,@Component ...
- springboot转发http请求_Spring Boot2 系列教程(八)Spring Boot 中配置 Https
https 现在已经越来越普及了,特别是做一些小程序或者公众号开发的时候,https 基本上都是刚需了. 不过一个 https 证书还是挺费钱的,个人开发者可以在各个云服务提供商那里申请一个免费的证书 ...
- 在Spring Boot中配置Thymeleaf的模板路径
众所周知,Thymeleaf的模板文件默认是在项目文件夹的src\main\resources\templates目录下的.不过出于特殊需要,要修改其路径怎么办呢? 在我们的项目配置文件applica ...
- SpringBoot - Spring Boot 中的配置体系Profile全面解读
文章目录 Pre Spring Boot 中的配置体系 配置文件与 Profile 主 application.properties 中指定激活的Profile Profile 配置信息只保存在一个文 ...
- Spring Boot中的配置文件使用以及重新加载
Spring Boot中的配置文件使用以及重新加载 概要 本教程将展示如何通过Java configuration和@PropertySource或XML和property-placeholder在S ...
- Spring Boot中使用JavaMailSender发送邮件
相信使用过Spring的众多开发者都知道Spring提供了非常好用的JavaMailSender接口实现邮件发送.在Spring Boot的Starter模块中也为此提供了自动化配置.下面通过实例看看 ...
- Spring Boot中Starter是什么
比如我们要在Spring Boot中引入Web MVC的支持时,我们通常会引入这个模块spring-boot-starter-web,而这个模块如果解压包出来会发现里面什么都没有,只定义了一些POM依 ...
- Spring Boot中Web应用的统一异常处理
为什么80%的码农都做不了架构师?>>> 我们在做Web应用的时候,请求处理过程中发生错误是非常常见的情况.Spring Boot提供了一个默认的映射:/error,当处理中抛 ...
最新文章
- HTML的标签描述5
- python正态分布相关函数
- Django框架视图类
- 算法------长度最小的子数组
- NYOJ 286 动物统计
- ArrayList 与 LinkedList 底层实现
- oracle update 数据库恢复,ORACLE update 操作内部原理
- 前端学习(1269):axios的拦截器
- spring的动态代理,碰到了一个类型转换的问题:java.lang.ClassCastException: com.sun.proxy.$Proxy16 cannot be cast to com.
- Python 竟能绘制如此酷炫的三维图
- Java并发性和多线程介绍
- 图论算法 若干定义
- 单片机c语言小波阈值降噪,一种基于改进阈值函数的小波阈值降噪算法
- BigDecimal解读
- 未能联接game center服务器,win10系统GameCenter无法连接服务器的处理步骤
- oracle 70个常用函数
- 基于QT的游戏修改器
- Android开发工程师文集-layout_weight讲解
- Java和C++基本类型与语法的区别
- 【查找各日期段内的银行贷款利率问题】
热门文章
- linux PHP ppt 转图片,linux下用php将doc、ppt转图片
- 一些学习cocos2d的网站
- TCP发送接口(如send(),write()等)的返回值与成功发送到接收端的数据量无直接关系
- Windows服务编写综述
- 第31讲:抓包利器 Charles 的使用
- 力扣- - 最短回文串(KMP算法)
- FFmpeg 4.2“艾达”发布
- HashMap源码解析(JDK1.8)
- 多窗口管理器Tmux - 从入门到精通
- Ceph 的数据回填和恢复