Spring Boot 之 HelloWorld 與 多文件上傳詳解

0、用到的框架和組件以下:

  1. Spring Boot 1.4.3.RELEASE
  2. Spring 4.3.5.RELEASE
  3. Thymeleaf
  4. jQuery (webjars)
  5. Maven
  6. Embedded Tomcat 8.5.6
  7. Google Chrome Browser (Network Inspect)
  8. 附:Spring MVC 請求流程圖:

spring-mvc

一、項目結構:

spring-boot-file-upload-ajax-directory-1

二、項目依賴:

Declares an extra jQuery webjar dependency, for Ajax requests in HTML form.html

pom.xmljava

<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.zhuanzhuan.data</groupId>
    <artifactId>HelloSpringBoot</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>HelloSpringBoot</name>
    <url>http://maven.apache.org</url>


    <!--&lt;!&ndash; Spring Boot 啓動父依賴 &ndash;&gt;
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        &lt;!&ndash; Junit &ndash;&gt;
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.5.2.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>-->


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <!-- hot swapping, disable cache for template, enable live reload -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>2.2.4</version>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>13.0.1</version>
        </dependency>
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
            <scope>compile</scope>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <!-- Package as an executable jar/war -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

三、Controller層

HelloWorldController的代碼以下:jquery

/**
 * Spring Boot HelloWorld案例
 *
 * Created by bysocket on 16/4/26.
 */
@RestController
public class HelloWorldController {
 
    @RequestMapping("/")
    public String sayHello() {
        return "Hello,World!";
    }
}

@RestController和@RequestMapping註解是來自SpringMVC的註解,它們不是SpringBoot的特定部分。web

1. @RestController:提供實現了REST API,能夠服務JSON,XML或者其餘。這裏是以String的形式渲染出結果。ajax

2. @RequestMapping:提供路由信息,」/「路徑的HTTP Request都會被映射到sayHello方法進行處理。spring

具體參考,世界上最好的文檔來源自官方的《Spring Framework Documentapache

四、啓動應用類

和第一段描述同樣,開箱即用。以下面Application類:segmentfault

/**
 * Spring Boot應用啓動類
 *
 * Created by bysocket on 16/4/26.
 */
@SpringBootApplication
public class Application {
 
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

1. @SpringBootApplication:Spring Boot 應用的標識api

2. Application很簡單,一個main函數做爲主入口。SpringApplication引導應用,並將Application自己做爲參數傳遞給run方法。具體run方法會啓動嵌入式的Tomcat並初始化Spring環境及其各Spring組件。spring-mvc

運行很簡單,直接右鍵Run運行Application類。一樣你也能夠Debug Run。能夠在控制檯中看到:

Tomcat started on port(s): 8080 (http)
Started Application in 5.986 seconds (JVM running for 7.398)

而後訪問 http://localhost:8080/ ,便可在頁面中看到Spring Boot對你 say hello:

Hello,World!

五、spring boot 多文件上傳代碼

Download – spring-boot-file-upload-ajax-rest.zip (11 KB)

六、curl 測試文件上傳

Test single file upload.
$ curl -F file=@"f:\\data.txt" http://localhost:8080/api/upload/
Successfully uploaded - data.txt

Test multiple file upload.
$ curl -F extraField="abc" -F files=@"f://data.txt" -F files=@"f://data2.txt" http://localhost:8080/api/upload/multi/
Successfully uploaded - data.txt , data2.txt

Test multiple file upload, maps to Model.
curl -F extraField="abc" -F files=@"f://data.txt" -F files=@"f://data2.txt"  http://localhost:8080/api/upload/multi/model
Successfully uploaded!

Refer:

[1] Spring Boot file upload example – Ajax and REST

https://www.mkyong.com/spring-boot/spring-boot-file-upload-example-ajax-and-rest/

[2] Spring Boot Tutorials

https://www.mkyong.com/tutorials/spring-boot-tutorials/

[3] Spring Boot 之 HelloWorld詳解

http://www.bysocket.com/?p=1124

[4] Spring-Boot 1.5 學習筆記

http://www.javashuo.com/article/p-tgrkleqr-hz.html

[5] Spring Boot 基礎

https://www.ibm.com/developerworks/cn/java/j-spring-boot-basics-perry/index.html

[6] Spring Boot開發Web應用

http://blog.didispace.com/springbootweb/

[7] Spring Boot 日誌管理

http://www.importnew.com/25485.html

[8] Spring Boot乾貨系列:(三)啓動原理解析

http://tengj.top/2017/03/09/springboot3/?comefrom=http://blogread.cn/news/#

相關文章
相關標籤/搜索