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/#

相关文章
相关标签/搜索