從0開始搭建SpringBoot

上篇文章介紹了springBoot的各類優勢,嗯,它很容易就能搭建一個web應用,那麼具體怎麼作呢?java

那麼咱們簡單的搭建一個hello 的web應用,這應用很是簡單,寫一個controller,而後讓這controller能在瀏覽器裏面訪問就ok了。web

你可使用eclipse,也可使用idea或其餘任何你習慣使用的開發工具,這裏我使用ideaspring

新建一個maven應用

完成後把裏面的pom文件修改爲apache

<?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>toroot</groupId>
    <artifactId>spingboot01</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>
瀏覽器

 

這個配置其實很簡單,固然也能夠解釋一下tomcat

固然,你能夠直接照着這些代碼來完成這簡單的功能,我並不建議你如今就來考慮爲何必定要這麼配置,糾結每個配置的意思,是運行,有個直觀的感覺再說。架構

固然,你也能夠看下這配置具體哪來的,後面有空再來看下爲何這麼配置。mvc

新建一個controller

新建一個controller,隨便起個名字都行,這裏就叫TorootController吧,新增一個sayhello的方法,並把相關的springmvc註解給加上app

package cn.toroot; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /**  * Created by www.Toroot.cn  * java資料學習交流羣:523916260  */ @RestController @RequestMapping("/toroot") public class TorootController {     @RequestMapping("/hello")     public Object sayHello() {         return "hello toroot";     } }

新建一個啓動類

接下來再增長一個啓動類,類名無所謂,咱們這裏就叫作TorootApp吧,其中具體的代碼以下eclipse

package cn.toroot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /**  * Created by www.Toroot.cn  * java資料學習交流羣:523916260  */ @SpringBootApplication public class TorootApp {     public static void main(String[] args) {         SpringApplication.run(TorootApp.class,args);         } }

測試運行

嗯,main方法有了,那麼直接點運行試試效果唄,運行main方法後,看到。

嗯,很是好,雖然不知道爲何但這webapp已經運行再tomcat裏面了,經過瀏覽器運行下試試。

http://localhost:8080/toroot/hello

是否是感受很是容易,固然這案例也是經過官網改動而成

具體官網案例以下圖所示

官網:http://projects.spring.io/spring-boot/

好了,至於具體的更高級的應用咱們留下次再分享吧,感興趣的同窗能夠java架構師學習交流羣 862039307

相關文章
相關標籤/搜索