Spring Boot——2分鐘構(gòu)建spring web mvc REST風(fēng)格HelloWorld

Spring Boot使我們更容易去創(chuàng)建基于Spring的獨(dú)立和產(chǎn)品級(jí)的可以”即時(shí)運(yùn)行“的應(yīng)用和服務(wù)。支持約定大于配置,目的是盡可能快地構(gòu)建和運(yùn)行Spring應(yīng)用。

之前我們創(chuàng)建基于Spring的項(xiàng)目需要考慮添加哪些Spring依賴和第三方的依賴。使用Spring Boot后,我們可以以最小化的依賴開始spring應(yīng)用。大多數(shù)Spring Boot應(yīng)用需要很少的配置即可運(yùn)行,比如我們可以創(chuàng)建獨(dú)立獨(dú)立大Java應(yīng)用,然后通過java -jar運(yùn)行啟動(dòng)或者傳統(tǒng)的WAR部署。其也提供了命令行工具來(lái)直接運(yùn)行Spring腳本(如groovy腳本)。也就是說(shuō)Spring Boot讓Spring應(yīng)用從配置到運(yùn)行變的更加簡(jiǎn)單,但不對(duì)Spring本身提供增強(qiáng)(即額外的功能)。

目的:

讓所有Spring開發(fā)變得更快,且讓更多的人更快的進(jìn)行Spring入門體驗(yàn),提供“starter” POM來(lái)簡(jiǎn)化我們的Maven配置(也就是說(shuō)使用Spring Boot只有配合maven/gradle等這種依賴管理工具才能發(fā)揮它的能力),不像以前,構(gòu)建一個(gè)springmvc項(xiàng)目需要進(jìn)行好多配置等。

開箱即用,快速開始需求開發(fā)而不被其他方面影響(如果可能會(huì)自動(dòng)配置Spring)

提供一些非功能性的常見的大型項(xiàng)目類特性(如內(nèi)嵌服務(wù)器、安全、度量、健康檢查、外部化配置),如可以直接地內(nèi)嵌Tomcat/Jetty(不需要單獨(dú)去部署war包)
絕無(wú)代碼生成,且無(wú)需XML配置

我的構(gòu)建環(huán)境

JDK 7
Maven 3
Servlet3容器

創(chuàng)建項(xiàng)目

首先使用Maven創(chuàng)建一個(gè)普通Maven應(yīng)用即可,不必是web的。

添加Spring Boot相關(guān)POM配置

在pom.xml中添加如下配置

    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>0.5.0.BUILD-SNAPSHOT</version>
    </parent>

    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

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

    <!-- Allow access to Spring milestones and snapshots -->
    <!-- (you don't need this if you are using anything after 0.5.0.RELEASE) -->
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>

繼承spring-boot-starter-parent后我們可以繼承一些默認(rèn)的依賴,這樣就無(wú)需添加一堆相應(yīng)的依賴,把依賴配置最小化;spring-boot-starter-web提供了對(duì)web的支持,spring-boot-maven-plugin提供了直接運(yùn)行項(xiàng)目的插件,我們可以直接mvn spring-boot:run運(yùn)行。

實(shí)體

Java代碼

package com.sishuok.entity;

/**
 * <p>User: Zhang Kaitao
 * <p>Date: 13-12-22
 * <p>Version: 1.0
 */
public class User {
    private Long id;
    private String name;

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        User user = (User) o;

        if (id != null ? !id.equals(user.id) : user.id != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return id != null ? id.hashCode() : 0;
    }
}

控制器

Java代碼

package com.sishuok.controller;

import com.sishuok.entity.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>User: Zhang Kaitao
 * <p>Date: 13-12-22
 * <p>Version: 1.0
 */
//@EnableAutoConfiguration
@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/{id}")
    public User view(@PathVariable("id") Long id) {
        User user = new User();
        user.setId(id);
        user.setName("zhang");
        return user;
    }

    //public static void main(String[] args) {
    //    SpringApplication.run(UserController.class);
    //}

}

運(yùn)行

第一種方式

通過在UserController中加上@EnableAutoConfiguration開啟自動(dòng)配置,然后通過SpringApplication.run(UserController.class);運(yùn)行這個(gè)控制器;這種方式只運(yùn)行一個(gè)控制器比較方便;

第二種方式

通過@Configuration+@ComponentScan開啟注解掃描并自動(dòng)注冊(cè)相應(yīng)的注解Bean
Java代碼

package com.sishuok;

import com.sishuok.controller.UserController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * <p>User: Zhang Kaitao
 * <p>Date: 13-12-22
 * <p>Version: 1.0
 */
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }
}

到此,一個(gè)基本的REST風(fēng)格的web應(yīng)用就構(gòu)建完成了。

地址欄輸入http://localhost:8080/user/1即可看到j(luò)son結(jié)果。

如果大家查看其依賴,會(huì)發(fā)現(xiàn)自動(dòng)添加了需要相應(yīng)的依賴(不管你用/不用),但是開發(fā)一個(gè)應(yīng)用確實(shí)變得非??焖?,對(duì)于想學(xué)習(xí)/體驗(yàn)Spring的新手,快速建立項(xiàng)目模型等可以考慮用這種方式。當(dāng)然如果不想依賴這么多的jar包,可以去掉parent,然后自己添加依賴。

參考

https://github.com/spring-projects/spring-boot

推薦相關(guān)課程

http://www.dabllo.com/course/195

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容