javascript
在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的博客
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的在Spring Boot中配置web app的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自定义spring boot的自动配置
- 下一篇: Spring Boot中的测试