




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
張飛2018-5-13Springboot快速集成第三方技術(shù)詳解課程安排集成模板引擎Thymeleaf實(shí)現(xiàn)web應(yīng)用集成模板引擎FreeMarker實(shí)現(xiàn)web應(yīng)用集成Swagger2構(gòu)建RESTful
API文檔統(tǒng)一異常處理集成Mybatis集成redis集成RabbitMQSpring
boot日志自定義視圖映射自定義Starter靜態(tài)資源訪問靜態(tài)資源:js,css,html,圖片,音視頻等靜態(tài)資源路徑:是指系統(tǒng)可以直接訪問的路徑,且路徑下的所有文件均可被用戶直接讀取。Spring
Boot默認(rèn)提供靜態(tài)資源目錄位置需置于classpath下,目錄名需符合如下規(guī)則:/static/public/resources/META-INF/resources案例:在classpath下面創(chuàng)建static目錄,并且加入一個(gè)圖片直接訪問:修改默認(rèn)的靜態(tài)資源目錄:spring.resources.static-locations集成模板引擎ThymeleafSpringBoot強(qiáng)烈建議使用模板引擎渲染html頁面,避免使用JSP,若一定要使用JSP將無法實(shí)現(xiàn)SpringBoot的多種特性Spring
boot默認(rèn)的模板配置路徑為:src/main/resources/templates集成Thymeleaf步驟加入thymeleaf依賴<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>編寫Controller,返回值為模板名稱建立模板文件在src/main/resources/下面建立templates/testThymeleaf.html集成模板引擎FreeMarker加入FreeMarker依賴<!--集成freemarker--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId></dependency>編寫Controller,返回模板文件名稱(flt結(jié)尾)@RequestMapping("/testFreemarker")public
String
testFreemarker(Map<String,String>
map)
{//默認(rèn)為src/main/resources/templates/hello.fltmap.put("name",
"張三"); return
"hello";}hello.flt,目錄為:src\main\resources\templates集成Swagger2構(gòu)建RESTful
API文檔加入依賴<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.2.2</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.2.2</version></dependency>創(chuàng)建Swagger2配置類創(chuàng)建Controller,加入Swagger2注解啟動(dòng)Springboot,訪問SwaggerUI界面
測試API統(tǒng)一異常處理使用@ControllerAdvice定義統(tǒng)一全局異常處理類使用@ExceptionHandler定義針對的異常類型增加異常頁面:src/main/resources/templates增加error.html測試controller拋出異常集成Mybatis修改pom.xml,增加依賴支持Mysql連接配置(perties)<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.1.1</version><!--版本號(hào)必須需要--></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency>spring.datasource.url=jdbc:mysql://localhost:3306/spring
spring.datasource.username=rootspring.datasource.password=root.mysql.jdbc.Driver集成Mybatis創(chuàng)建表創(chuàng)建User.java文件創(chuàng)建UserMapper.java文件創(chuàng)建測試類測試Mapper中的接口CREATE
TABLE
`t_user`
(`id`
int(11)
NOT
NULL
AUTO_INCREMENT,`name`
varchar(40)
DEFAULT
NULL,`age`
int(11)
DEFAULTNULL,`address`
varchar(100)
DEFAULT
NULL,`phone`
varchar(40)
DEFAULT
NULL,PRIMARY
KEY
(`id`))
ENGINE=InnoDB
AUTO_INCREMENT=1
DEFAULTCHARSET=utf8;集成redis加入redis依賴<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>加入redis配置,參考下一頁ppt啟動(dòng)redisWindows:
redis-server
redis.windows.conf使用RedisTemplate測試集成redis加入redis連接配置#REDIS(RedisProperties)#Redis數(shù)據(jù)庫索引(默認(rèn)為0)spring.redis.database=0#Redis服務(wù)器地址
spring.redis.host=#Redis服務(wù)器連接端口
spring.redis.port=6379#Redis服務(wù)器連接密碼(默認(rèn)為空)spring.redis.password=#連接池最大連接數(shù)(使用負(fù)值表示沒有限制)spring.redis.pool.max-active=8#連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒有限制)spring.redis.pool.max-wait=-1#連接池中的最大空閑連接
spring.redis.pool.max-idle=8#連接池中的最小空閑連接
spring.redis.pool.min-idle=0#連接超時(shí)時(shí)間(毫秒)spring.redis.timeout=0集成RabbitMQ安裝RabbitMQErlang/OTP20.3下載地址:
Erlang/OTP其它版本下載地址:RabbitMQServer3.7.4下載地址
RabbitMQ其它版本下載地址:
集成RabbitMQ啟動(dòng)RabbitMQServerRabbitMQ管理界面插件安裝進(jìn)入rabbitmq安裝目錄的sbin目錄,在此打開dos命令窗口,執(zhí)行以下命令rabbitmq-pluginsenablerabbitmq_management重新啟動(dòng)RabbitMQ服務(wù),打開瀏覽器并訪問:默認(rèn)登陸賬戶guest/guestSpringboot整合增加依賴<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency>集成RabbitMQ新增管理用戶并設(shè)置權(quán)限連接配置信息創(chuàng)建Rabbit配置類創(chuàng)建消息產(chǎn)生者類創(chuàng)建消息消費(fèi)者創(chuàng)建測試類啟動(dòng)springboot主程序,運(yùn)行junit類##
rabbitmq
configspring.rabbitmq.host=localhostspring.rabbitmq.port=5672spring.rabbitmq.username=springbootspring.rabbitmq.password=123456Spring
boot日志日志級(jí)別默認(rèn)情況下,Spring
Boot配置的是INFO日志級(jí)別,也就是會(huì)輸出INFO級(jí)別以上的日志(
ERROR,
WARN,
INFO
)debug日志級(jí)別:debug=true配置logging.level.*來具體輸出哪些包的日志級(jí)別:logging.level.root=INFO.springframework.web=DEBUG.example.boot.controller=DEBUG日志文件Spring
Boot默認(rèn)情況下日志只會(huì)輸出到控制臺(tái),并不會(huì)寫入到日志文件指定日志文件目錄,指定日志名稱,指定日志輸出級(jí)別:logging.path
=
/var/tmplogging.file
=
xxx.loglogging.level.root
=
info如果只配置logging.path,會(huì)在/var/tmp文件夾生成spring.log。如果只配置
logging.file,會(huì)在項(xiàng)目當(dāng)前路徑下生成xxx.log日志文件Spring
boot日志集成log4j2<!--
log4j2
--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-logging</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-log4j2</artifactId></dependency>Spring
boot日志集成log4j<!--
log4j
start
--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-logging</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-log4j</artifactId><version>1.3.8.RELEASE</version></dependency><!--
log4j
end
-->Spring
boot日志classpath下增加perties配置內(nèi)容參考備注注意自定義的日志規(guī)則:.example.boot=INFO,
myFile#com.example.boot下的日志輸出log4j.appender.myFile=org.apache.log4j.DailyRollingFile
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 住院部個(gè)人工作計(jì)劃
- 河北保定曲陽縣2025年七下數(shù)學(xué)期末統(tǒng)考試題含解析
- 高峰期倉庫工作計(jì)劃
- 2024年河南省醫(yī)療保障局下屬事業(yè)單位真題
- 2024年南安市實(shí)驗(yàn)中學(xué)招聘筆試真題
- 社交媒體經(jīng)理工作總結(jié)與傳播策略計(jì)劃
- 內(nèi)蒙古烏海二十二中學(xué)2025屆數(shù)學(xué)七下期末檢測模擬試題含解析
- 2025年網(wǎng)絡(luò)管理員考試自己測試試題
- 材料力學(xué)性能測試疲勞壽命環(huán)境因素重點(diǎn)基礎(chǔ)知識(shí)點(diǎn)
- 廣東省珠海市斗門區(qū)2025年七年級(jí)數(shù)學(xué)第二學(xué)期期末綜合測試試題含解析
- 航空客運(yùn)包機(jī)合同
- 馬拉松志愿者培訓(xùn)
- 車間衛(wèi)生打掃管理制度
- 高中教師培訓(xùn)管理制度
- 造價(jià)風(fēng)險(xiǎn)防范管理制度
- 飼料粉塵清掃管理制度
- 《浙江省中藥飲片炮制規(guī)范》 2015年版
- GB 19762-2025離心泵能效限定值及能效等級(jí)
- 某樓板裂縫修復(fù)及碳纖維加固施工方案
- 青馬選拔考試試題及答案
- 中國金融大模型發(fā)展白皮書
評論
0/150
提交評論