本文將會介紹怎麼在Spring Boot中創建和配置一個web應用程序。
添加依賴
如果要使用Spring web程序,則需要添加如下依賴:
<code><
dependency
><
groupId
>org.springframework.bootgroupId
><
artifactId
>spring-boot-starter-webartifactId
>dependency
> /<code>
配置端口
正如我們之前文章中提到的,要想配置端口需要在application.properties文件中配置如下:
<code>server.port
=8083
/<code>
如果你是用的是yaml文件,則:
<code>server:
port:
8083
/<code>
或者通過java文件的形式:
<code>public
class
CustomizationBean
implements
WebServerFactoryCustomizer
<ConfigurableServletWebServerFactory
> {public
void
customize
(ConfigurableServletWebServerFactory container)
{ container.setPort(8083
); } } /<code>
配置Context Path
默認情況下,Spring MVC的context path是‘/’, 如果你想修改,那麼可以在配置文件application.properties中修改:
<code>server.servlet.contextPath
=/springbootapp /<code>
如果是yaml文件:
<code>server
:servlet
:contextPath
:/springbootapp /<code>
同樣的,可以在java代碼中修改:
<code>public
class
CustomizationBean
implements
WebServerFactoryCustomizer
<ConfigurableServletWebServerFactory
> {public
void
customize
(ConfigurableServletWebServerFactorycontainer)
{ container.setContextPath("/springbootapp"
); } } /<code>
配置錯誤頁面
默認情況下Spring Boot會開啟一個whitelabel的功能來處理錯誤,這個功能本質上是自動註冊一個BasicErrorController如果你沒有指定錯誤處理器的話。同樣的,這個錯誤控制器也可以自定義:
<code>public
class
MyCustomErrorController
implements
ErrorController
{private
static
final
String PATH ="/error"
; (value=PATH)public
Stringerror
()
{return
"Error haven"
; }public
StringgetErrorPath
()
{return
PATH; } } /<code>
當然,和之前講過的自定義服務器信息一樣,你也可以自定義錯誤頁面,就像在web.xml裡面添加error-page:
<code>public
class
CustomizationBean
implements
WebServerFactoryCustomizer
<ConfigurableServletWebServerFactory
> {public
void
customize
(ConfigurableServletWebServerFactorycontainer)
{ container.addErrorPages(new
ErrorPage(HttpStatus.BAD_REQUEST,"/400"
)); container.addErrorPages(new
ErrorPage("/errorHaven"
)); } } /<code>
通過這個功能,你可以對錯誤進行更加細緻的分類。
在程序中停止Spring Boot
SpringApplication提供了一個靜態的exit()方法,可以通過它來關停一個Spring Boot應用程序:
<code>public
void
shutDown
(ApplicationContext applicationContext)
{ SpringApplication.exit(applicationContext,new
ExitCodeGenerator() {public
int
getExitCode
()
{return
0
; } }); } /<code>
第二個參數是一個ExitCodeGenerator的實現,主要用來返回ExitCode。
配置日誌級別
我們可以在配置文件中這樣配置日誌級別:
<code>logging
.level
.org
.springframework
.web
:DEBUG
logging
.level
.org
.hibernate
:ERROR
/<code>
註冊Servlet
有時候我們需要將程序運行在非嵌套的服務器中,這時候有可能會需要自定義servlet的情況,Spring Boot 也提供了非常棒的支持,我們只需要在ServletRegistrationBean中,註冊servlet即可:
<code>public
ServletRegistrationBeanservletRegistrationBean
()
{ ServletRegistrationBean bean =new
ServletRegistrationBean(new
SpringHelloWorldServlet(),"/springHelloWorld/*"
); bean.setLoadOnStartup(1
); bean.addInitParameter("message"
,"SpringHelloWorldServlet special message"
);return
bean; } /<code>
切換嵌套服務器
默認情況下,Spring Boot會使用tomcat作為嵌套的內部服務器,如果想切換成jetty則可以這樣:
<code><
dependencies
><
dependency
><
groupId
>org.springframework.bootgroupId
><
artifactId
>spring-boot-starter-webartifactId
><
exclusions
><
exclusion
><
groupId
>org.springframework.bootgroupId
><
artifactId
>spring-boot-starter-tomcatartifactId
>exclusion
>exclusions
>dependency
><
dependency
><
groupId
>org.springframework.bootgroupId
><
artifactId
>spring-boot-starter-jettyartifactId
>dependency
>dependencies
> /<code>
exclude自帶的Tomcat,並額外添加spring-boot-starter-jetty即可。
歡迎關注我的公眾號:程序那些事,更多精彩等著您!
更多內容請訪問:flydean的博客 flydean.com