通過簡單方法實(shí)現(xiàn)spring boot web項(xiàng)目
搭建效果為:
直接在網(wǎng)頁輸入請求,在頁面中顯示一行文字:Hello,Spring Boot
與一般的wen項(xiàng)目不同的地方:
1、不需要配置web.xml 文件,但需要注解@SpringBootApplication 等
2、一切和spring有關(guān)的jar包都不需要版本號,springcloud會(huì)給你選擇它最穩(wěn)定的版本
3、它會(huì)定位public static void main()方法來標(biāo)記為可運(yùn)行類,必須在主路徑下
4、啟動(dòng)方式:
a.右鍵運(yùn)行main方法
b.由于我們使用了 spring-boot-starter-parent POM,所以可以使用 mvn spring-boot:run來啟動(dòng)項(xiàng)目(根路徑)
c.先使用Maven來package(打包),然后java -jar*-0.0.1-SNAPSHOT.jar。
搭建
創(chuàng)建一個(gè)新的maven項(xiàng)目,目錄結(jié)構(gòu)如下:
pom.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>cn.jiashubing</groupId> <artifactId>spring-boot-web</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
HomeController.java文件
package cn.jiashubing.controller;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;/** * @author jiashubing * @since 2018/5/30 */@Controller@EnableAutoConfigurationpublic class HomeController { @RequestMapping(value = '/home', method = RequestMethod.GET) @ResponseBody public String home() { return 'Hello,Spring Boot'; }}
JiashubingApplication.java文件
package cn.jiashubing;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * @author jiashubing * @since 2018/5/29 */@SpringBootApplicationpublic class JiashubingApplication { public static void main(String[] args) { SpringApplication.run(JiashubingApplication.class, args); }}
在瀏覽器中輸入 http://localhost:8080/home
最終效果如下:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式2. bootstrap select2 動(dòng)態(tài)從后臺(tái)Ajax動(dòng)態(tài)獲取數(shù)據(jù)的代碼3. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼4. php redis setnx分布式鎖簡單原理解析5. 《Java程序員修煉之道》作者Ben Evans:保守的設(shè)計(jì)思想是Java的最大優(yōu)勢6. CSS3中Transition屬性詳解以及示例分享7. Python數(shù)據(jù)相關(guān)系數(shù)矩陣和熱力圖輕松實(shí)現(xiàn)教程8. 如何在PHP中讀寫文件9. java加載屬性配置properties文件的方法10. 什么是Python變量作用域
