springboot打war包部署tomcat服務器,以及表單提交數據亂碼處理
小白覺得springboot打成jar包直接使用內嵌的tomcat或jetty容器(java -jar xxx.jar)運行項目不利於定位問題,我還是習慣於查看tomcat或nginx的日誌來定位問題,今天小白就講講springboot打成war部署JavaWeb項目於tomcat。
新建web項目 helloboot
開發工具:Spring Tool Suite(STS)
File->New->Spring Starter Project,如下圖所示:
項目結構如下所示:
application.properties配置:
server.port=8082 server.context-path=/helloboot spring.devtools.restart.additional-exclude=non-restart/** spring.resources.static-locations=classpath:/resources/static/,classpath:/static/ spring.thymeleaf.cache=false logging.level.org.springframework.security=INFO
pox.xml(自動生成的,未作修改):
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>helloboot</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.13.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
項目打包
1)在項目的啟動類HellobootApplication.java的同級目錄下新建ServletInitializer.java,代碼如下,其位置可參考項目結構圖:
package com.example.demo; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(HellobootApplication.class); } }
2)用maven進行打包:Run AS -> Maven install,如下圖所示打包成功,會在target目錄下生成demo-0.0.1-SNAPSHOT.war
項目部署
進入tomcat的webapps子目錄上傳,上傳完成後重命名為helloboot.war:
cd /root/tomcat/webapps
rz
mv demo-0.0.1-SNAPSHOT.war helloboot.war
修改tomcat的配置文件server.xml文件將8080端口改為8082,當然8080端口也是可以,由於本服務的8080端口已被占用。
進入tomcat的bin目錄,啟動tomcat:
cd /root/tomcat/bin
sh startup.sh
查看tomcat的日誌:
cd /root/tomcat/logs tail -f catalina.out
從日誌可以看出啟動成功:
項目訪問
訪問內網,可以通過xshell的通道訪問:
在瀏覽器地址欄中輸入:http://127.0.0.1:8082/helloboot/index,可見項目打包發布成功。
亂碼處理
今天小白在測試demo是發現通過表單提交的數據亂碼,原因可能有兩種,一種前端提交到後臺的數據後臺解析亂碼,另一種是解析正常但是在寫入數據庫的時候由於數據庫編碼導致的亂碼。小白就這兩種情況進行測試,第一種情況通過日誌發現打印出來的前端傳到後臺的數據正常,於是小白查看了mysql數據庫的編碼,如下所示:
mysql> show variables like ‘character_set_database‘; +------------------------+--------+ | Variable_name | Value | +------------------------+--------+ | character_set_database | latin1 | +------------------------+--------+ 1 row in set (0.01 sec)
收集資料嘗試發現只要在mysql的my.cnf的中添加一行代碼character-set-server=utf8,重新啟動mysql即可
[root@localhost etc]# vi /etc/my.cnf
[root@localhost etc]# service mysqld restart
my.cnf:
[root@localhost etc]# cat /etc/my.cnf # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html [mysqld] # # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 character-set-server=utf8 log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
修改後的mysql數據庫的編碼,表單提交數據也正常了。
mysql> show variables like ‘character_set_database‘; +------------------------+-------+ | Variable_name | Value | +------------------------+-------+ | character_set_database | utf8 | +------------------------+-------+ 1 row in set (0.06 sec)
springboot打war包部署tomcat服務器,以及表單提交數據亂碼處理