SpringBoot(一)——簡介、HelloWorld、Json

SpringBoot(一)——簡介、HelloWorld、Json

一、什麼是Spring Boot

  Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。

二、Spring Boot特性

  1. 創建獨立的Spring應用程序
  2. 嵌入的Tomcat,無需部署WAR文件
  3. 簡化Maven配置
  4. 自動配置Spring
  5. 提供生產就緒型功能,如指標,健康檢查和外部配置
  6.開箱即用,沒有代碼生成,也無需XML配置。

三、Spring Boot特性理解

  1、爲基於Spring的開發提供更快的入門體驗
  2、開箱即用,沒有代碼生成,也無需XML配置。同時也可以修改默認值來滿足特定的需求。
  3、提供了一些大型項目中常見的非功能特性,如嵌入式服務器、安全、指標,健康檢測、外部配置等。
  4、Spring Boot並不是對Spring功能上的增強,而是提供了一種快速使用Spring的方式。

四、開發Spring Boot(Hello World)

1、開發前準備

  1) 開發環境JDK 1.8
  2) 開發工具(Eclipse)
  3) 項目管理工具( Maven )

2、開發步驟

1) 創建Maven Project (spring-boot-hello)

2) 寫pom.xml

<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>cn.qiuuuu</groupId>
  <artifactId>spring-boot-hello</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

  <!-- 
spring boot 父節點依賴,引入這個之後相關的引入就不需要添加version配置,spring boot會自動選擇最合適的版本進行添加。
 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!-- 指定一下jdk的版本 ,這裏我們使用jdk 1.8 ,默認是1.6 -->
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
        <!-- spring-boot-starter-web: MVC,AOP的依賴包....-->
        <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <!-- <version></version>由於我們在上面指定了 parent(spring boot)-->
        </dependency>

        <!-- 添加fastjson 依賴包. -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.15</version>
        </dependency>

        <!-- spring boot devtools 依賴包. -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
           <scope>true</scope>
        </dependency>

  </dependencies>

    <!-- 構建節點. -->
    <build>
        <plugins>

            <!-- 在這裏添加springloader plugin
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin </artifactId>
                <dependencies>  
                   <dependency>  
                       <groupId>org.springframework</groupId>  
                       <artifactId>springloaded</artifactId>  
                       <version>1.2.4.RELEASE</version>
                   </dependency>  
                </dependencies>  
                <executions>  
                   <execution>  
                       <goals>  
                           <goal>repackage</goal>  
                       </goals>  
                       <configuration>  
                           <classifier>exec</classifier>  
                       </configuration>  
                   </execution>  
                </executions>
            </plugin>
             -->

            <!-- 這是spring boot devtool plugin -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--fork :  如果沒有該項配置,肯呢個devtools不會起作用,即應用不會restart -->
                    <fork>true</fork>
                </configuration>
            </plugin>

        </plugins>

    </build>    
</project>

3) 在src/main/webapp包下面創建一個名爲cn.qiuuuu的包

4) 在qiuuuu的包下面新建一個名爲HelloController的類,內容如下:

package com.kfit;

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

/*
 * 在這裏使用RestController(等價於@Controller和@ResonseBody)
 */
@RestController
public class HelloController {

    /*
     * 在這裏使用@RequestMapping建立請求映射: http://127.0.0.1:8080/hello
     */
    @RequestMapping("/hello")
    public String hello() {
        return "<h1>Hello World</h1>";
    }
}

5) 在qiuuuu的包下面新建一個名爲App的類,內容如下:
【注】springboot的啓動程序類要放在其他controller的外層

package com.kfit;

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

/*
 * 啓動類:
 *   使用SpringBootApplication指定這是一個spring boot的應用程序
 */

@SpringBootApplication
public class App {
    public static void main(String[] args) {
        // 在main方法中啓動我們的程序
        SpringApplication.run(App.class, args);
    }
}

6) 在src/main/siurce包下面新建一個名爲application.properties的文件,並在文件中寫入以下內容:

server.port = 8089

  A、這個配置文件時Spring Boot的默認配置文件;
  B、由於我的tomcat設置的默認端口是8089,而Spring Boot的默認端口是8080,因此需要配置,讓Spring Boot的啓動端口爲8089;
  C、若tomcat的啓動端口爲8080,則不需要這一步。

3、測試

1) 運行此Spring Boot程序

  運行cn.qiuuuu包下面的App文件(Run As Java Application)

SpringBoot(一)——簡介、HelloWorld、Json

2) 瀏覽器測試

  在瀏覽器中輸入http://localhost:8089/hello

SpringBoot(一)——簡介、HelloWorld、Json

五、開發Spring Boot(返回Json數據)

  SpringBoot默認使用的是json解析框架jackson

1、開發步驟:

  在上一個項目的基礎上,進行以下操作,步驟如下:

1) 在com.kift下新建一個名爲Demo的實體類:

package com.kfit;

/*
 * 這是一個測試實體類。
 */
public class Demo {
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

2) 在com.kift包下的HelloController中加入getDemo()方法:

package com.kfit;

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

/*
 * 在這裏使用RestController(等價於@Controller和@ResonseBody)
 */
@RestController
public class HelloController {

    /*
     * 在這裏使用@RequestMapping建立請求映射: http://127.0.0.1:8080/hello
     */
    @RequestMapping("/hello")
    public String hello() {
        return "<h1>Hello World</h1>";
    }

    @RequestMapping("/demo")
    public Demo getDemo() {
        Demo demo = new Demo();
        demo.setId(1);
        demo.setName("張三");
        return demo;
    }
}

2、測試

  在瀏覽器中輸入http:localhost:8089/demo

SpringBoot(一)——簡介、HelloWorld、Json

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