天天看點

Spring Boot學習筆記——一個簡單的Web應用程式一個簡單的Web應用程式

一個簡單的Web應用程式

建立一個Maven項目

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>com.example</groupId>
    <artifactId>spring-boot-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-boot-demo1</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </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>
           

建立一個Controller

package com.demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
// @RestController表示使用Spring MVC來處請網絡請求
// @RestController 是 @Controller 與 @ResponseBody 的結合,表示傳回資料,而不是傳回一個View
public class HelloController {
    // @RequestMapping("/")表示映射路徑 /
    // 也就是通路 "localhost:8080/" 時會執行index()方法
    @RequestMapping("/")
    public String index() {
        return "Hello, world";
    }
}
           

建立 Application 類,Application是項目啟動的地方,而且需要放置在最頂層的包(要包括所有用到的包),比如你有 “com.demo”, “com.demo.application”兩個包,你将 Application 類放到 com.demo.application中會無法掃描 “com.demo”下的類。

package com.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
           

@SpringBootApplication 注解包括以下注解的功能:

  • @Configuration 将類标記為應用程式上下文的bean定義的來源。
  • @EnableAutoConfiguration 告訴Spring Boot根據類路徑設定,其他bean和各種屬性設定開始添加bean。
  • @ComponentScan:告訴Spring在 com.demo 包中尋找其他元件,配置和服務,讓它找到控制器。
  • @EnableWebMvc:通常,您将為Spring MVC應用程式添加@EnableWebMvc,但是當Spring類在類路徑中看到spring-webmvc時,Spring Boot會自動添加它。 這将應用程式标記為Web應用程式,并激活諸如設定DispatcherServlet等關鍵行為。

main()方法使用SpringApplication.run()方法來啟動一個應用。

仔細看一下項目,你會發現連web.xml都沒有,該程式完全由Java來編寫。

運作程式

在項目上右鍵

Run As

->

Maven build...

在Goals行輸入

spring-boot:run

點選

Run

即可

Spring Boot學習筆記——一個簡單的Web應用程式一個簡單的Web應用程式

不報錯的話,表明運作成功。

Spring Boot學習筆記——一個簡單的Web應用程式一個簡單的Web應用程式

測試,打開浏覽器,輸入 “localhost:8080”,得到 “Hello, World”。

Spring Boot學習筆記——一個簡單的Web應用程式一個簡單的Web應用程式

簡單的一個Web項目就搭建完成了~

繼續閱讀