springboot之helloworld


本例將採用maven管理,代碼託管在github上,地址:https://github.com/wolf909867753/springboot

1。創建maven-module,並在pom.xml中添加springboot依賴

  

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>helloWorld</artifactId>
    <packaging>war</packaging>
    <name>springboot-helloWorld Demo</name>
    <url>http://maven.apache.org</url>

    <!-- Spring Boot 啓動父依賴 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>

    <dependencies>
        <!-- Spring Boot web依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>helloWorld</finalName>
    </build>
</project>


2.創建helloworldController類
@RestController
public class HelloWorld {

    @RequestMapping("/")
    public String helloWorld() {
        return "Hello World!";
    }

}

3.測試類
@SpringBootApplication
public class HelloWorldTest {
        public static void main(String[] args) {
            SpringApplication.run(HelloWorldTest.class,args);
        }
}
運行main方法,訪問http://127.0.0.1:8080後頁面輸出Hello World!

小結:
/**
 * 一個最簡單的Web應用 測試類參考test目錄下的HelloWorldTest.java
 * 使用Spring Boot框架可以大大加速Web應用的開發過程,首先在Maven項目依賴中引入spring-boot-starter-web

 *  運行應用:mvn spring-boot:run或在IDE中運行main()方法,
 *  在瀏覽器中訪問http://localhost:8080,Hello World!就出現在了頁面中。
 *  只用了區區十幾行Java代碼,一個Hello World應用就可以正確運行了,那麼這段代碼究竟做了什麼呢?
 *  們從程序的入口SpringApplication.run(HelloWorld.class, args);開始分析:
 *  SpringApplication是Spring Boot框架中描述Spring應用的類,
 *  它的run()方法會創建一個Spring應用上下文(Application Context)。另一方面它會掃描當前應用類路徑上的依賴,
 *  例如本例中發現spring-webmvc(由 spring-boot-starter-web傳遞引入)在類路徑中,那麼Spring Boot會判斷這是一個Web應用,
 *  並啓動一個內嵌的Servlet容器(默認是Tomcat)用於處理HTTP請求。
 *  Spring WebMvc框架會將Servlet容器裏收到的HTTP請求根據路徑分發給對應的@Controller類進行處理,
 *  @RestController是一類特殊的@Controller,它的返回值直接作爲HTTP Response的Body部分返回給瀏覽器。
 *  @RequestMapping註解表明該方法處理那些URL對應的HTTP請求,也就是我們常說的URL路由(routing),請求的分發工作是有Spring完成的。
 *  例如上面的代碼中http://localhost:8080/根路徑就被路由至helloWorld()方法進行處理。
 *  如果訪問http://localhost:8080/hello,則會出現404 Not Found錯誤,因爲我們並沒有編寫任何方法來處理/hello請求。
 *
 * @SpringBootApplication -----Spring Boot應用啓動類
 * 在本例中也可以去掉@SpringBootApplication 直接使用以下代碼也可以運行
 * Created by bysocket on 16/4/26.
    @SpringBootApplication
    public class HelloWorldTest {
        public static void main(String[] args) {
            SpringApplication.run(HelloWorldTest.class,args);
        }
    }
 * 如果將@RestController ----> @Controller,運行main method將會報以下Error
 * Whitelabel Error Page
    This application has no explicit mapping for /error, so you are seeing this as a fallback.
    Sat Jun 17 14:30:28 CST 2017
    There was an unexpected error (type=Not Found, status=404).
    No message available
 *
 */



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章