javascript
试用SpringBoot创建WEB应用
最近試用了一下Spring Boot,發現用它創建WEB應用簡直好爽啊,記錄一下。
首先用Eclipse建一個maven工程,創建時選中“Create a simple proejct (skip archetype selection)”,建個簡單工程就好了。
然后編輯pom.xml,引入相關的包,完整文件如下:
<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>org.test</groupId><artifactId>appboot</artifactId><version>0.0.1</version><name>appboot</name><dependencies><!--?引入WEB開發支持?--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>1.4.1.RELEASE</version></dependency><!--?引入FreeMarker支持?--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId><version>1.4.1.RELEASE</version></dependency><!--?引入重編譯自動加載支持,這樣在Eclipse中調試時有變更會自動重新加載?--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><version>1.4.1.RELEASE</version><optional>true</optional></dependency><!--?引入fastjson,用于替換默認的json包?--><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.17</version></dependency></dependencies><build><finalName>appboot</finalName><plugins><!--?引入maven打包支持?--><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>1.4.1.RELEASE</version><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build> </project>添加應用程序入口文件Application.java,如下:
package?org.me;import?org.springframework.boot.SpringApplication; import?org.springframework.boot.autoconfigure.SpringBootApplication; import?org.springframework.boot.autoconfigure.web.HttpMessageConverters; import?org.springframework.context.annotation.Bean; import?org.springframework.http.converter.HttpMessageConverter;import?com.alibaba.fastjson.serializer.SerializerFeature; import?com.alibaba.fastjson.support.config.FastJsonConfig; import?com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;@SpringBootApplication public?class?Application?{/***?用fastjson作為默認的json編碼器*?@return*/@Beanpublic?HttpMessageConverters?fastJsonHttpMessageConverters()?{FastJsonHttpMessageConverter?fastConverter?=?new?FastJsonHttpMessageConverter();FastJsonConfig?fastJsonConfig?=?new?FastJsonConfig();fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteDateUseDateFormat,?SerializerFeature.WriteMapNullValue);fastConverter.setFastJsonConfig(fastJsonConfig);HttpMessageConverter<?>?converter?=?fastConverter;return?new?HttpMessageConverters(converter);}/***?應用程序入口*?@param?args*/public?static?void?main(String[]?args)?{//用SpringApplication啟動SpringApplication.run(Application.class,?args);} }如果只用默認的json編碼器,那么只有main方法就夠了。然后來寫一個控制器,比如叫HomeController:
和用SpringMVC時沒有什么不同。SpringBoot一般不推薦JSP視圖引擎,所以這里使用了freemarker作為視圖引擎。login方法里指定了視圖為home/login,因此在src/main/resources/templates/home下創建視圖login.ftl:
<!DOCTYPE?html> <html> <head><title>Login</title><meta?http-equiv="Content-Type"?content="text/html;?charset=UTF-8"?/><link?rel="stylesheet"?href="css/core.css"?/><script?src="js/jquery-3.1.1.min.js"></script><script>$(function(){$("#login").click(function(){$.ajax({url:?"checkLogin",type:?"POST",contentType:?"application/json",dataType:?"json",data:?JSON.stringify({user:"admin",?pass:"123"}),success:?function(data)?{$("#out").text(JSON.stringify(data));}});});});</script> </head> <body>DateTime:?${time?time}<br>Message:?${message}<br/><br/><button?id="login">登錄</button><br/><br/><div?id="out"></div> </body> </html>這個頁面從控制器接收了time和message兩個參數,用${}的形式顯示在頁面上。此外還包括了一個ajax調用,向后臺請求checkLogin,并傳入參數,顯示返回值。
最后加個SpringBoot的配置文件,application.properties,放到src/main/resources下:
其實主要是server.port參數,我們的工程內置了tomcat,這個參數指定了tomcat的綁定端口。
完整的項目結構如下:
默認情況下,視圖放到resources/templates下,靜態資源放到resources/static下。一切準備就緒,Ctrl+F11把工程跑起來吧,用瀏覽器訪問 http://localhost:8080/login?就可以打開頁面了。一但在Eclipse里修改了java文件,SpringBoot會自動重新加載,在開發調試時會很方便。
打包的時候,可以把整個項目的相關資源打成一個jar,這樣用命令行就可以運行了。
| mvn clean package |
會在target下得到一個appboot.jar,這個jar里包含了用戶代碼的class文件、依賴的jar文件、視圖文件、靜態資源,會比較大,直接用命令行運行:
| java -jar appboot.jar |
一個完整的web應用就跑起來了,目標環境只要有JRE就夠了,完全不用安裝tomcat。雖然jar文件比較大,但是部署到遠程環境的話可就方便多了。
至于性能,完全不用擔心,本質是基于tomcat的,性能肯定不會差,拿ab測試一下:
| ab -c 10 -n 100000 http://localhost:8080/login |
結果如下,超過9000rps:
轉載于:https://blog.51cto.com/boytnt/1866267
總結
以上是生活随笔為你收集整理的试用SpringBoot创建WEB应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ipmitool 中文 帮助 文档
- 下一篇: linux 压缩 解压zip 命令