01-構(gòu)建一個(gè)springboot工程

構(gòu)建過(guò)程

打開(kāi)Idea-> new Project ->Spring Initializr ->填寫group塘装、artifact ->鉤上web(開(kāi)啟web功能)->點(diǎn)下一步就行了刨肃。

目錄結(jié)構(gòu)入下:

├─.idea
│  └─inspectionProfiles
├─.mvn
│  └─wrapper
└─src
    ├─main
    │  ├─java
    │  │  └─com
    │  │      └─stellar
    │  │          └─springboot
    │  │              └─springbootfirstapplication #程序入口
    │  └─resources
    │      ├─static #靜態(tài)資源
    │      └─templates #模板資源
    |      └─application.yml #配置文件
    └─test
        └─java
            └─com
                └─stellar
                    └─springboot
                        └─springbootfirstapplication
└─pom.xml

pom文件如下

?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.stellar.springboot</groupId>
    <artifactId>springboot-first-application</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot-first-application</name>
    <description>Demo project for Spring Boot</description>

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

spring-boot-starter-web的jar中包含了spring mvc相關(guān)的組件及默認(rèn)配置,還有各種組件的自動(dòng)配置钱反,因此在此種不需要配置項(xiàng)目通過(guò)springbootfirstapplication入口可以直接啟動(dòng)址儒。

添加controller訪問(wèn)

在啟動(dòng)入口做如下修改:

@RestController //配置返回JSON格式
@SpringBootApplication
public class SpringbootFirstApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootFirstApplication.class, args);
    }
    //添加一個(gè)controller方法
    @RequestMapping("/")
    public String helloTest(){
        return "This is the first springboot application";
    }
}

通過(guò)如可main方法啟動(dòng)服務(wù)演训,調(diào)用請(qǐng)求http://localhost:8080/返回結(jié)果如下:

This is the first springboot application
  • 此時(shí)tomcat使用的是內(nèi)置的tomcat
  • web.xml和springmvcd 配置用的是默認(rèn)配置

查看加載了哪些類

在入口類中添加如下代碼,打印啟動(dòng)加載了哪些類:

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx){
        return args -> {
            System.out.println("Let's inspect the beans provided by Spring Boot:");
            //啟動(dòng)打印加載的類
            String[] beanNames = ctx.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            for (String beanName : beanNames) {
                System.out.println(beanName);
            }
        };
    }

單元測(cè)試代碼入下

方式一:

package com.stellar.springboot.springbootfirstapplication;

import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

import java.net.MalformedURLException;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)//需要配置webEnviroment要不會(huì)報(bào)錯(cuò)
public class SpringbootFirstApplicationTests {

    @LocalServerPort
    private int port;

    private String baseUrl;

    @Autowired
    private TestRestTemplate testRestTemplate;

    @Before
    public void setUp() throws MalformedURLException {
        this.baseUrl="http://localhost:"+this.port+"/";
    }

    @Test
    public void testHello(){
        ResponseEntity<String> responseEntity = this.testRestTemplate.getForEntity(this.baseUrl,String.class);
        Assert.assertThat(responseEntity.getBody(), Matchers.equalTo("This is the first springboot application"));
    }
}

方式二:

import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class HelloTest {
    @Autowired
    private MockMvc mvc;

    @Test
    public void testHello() throws Exception {
        this.mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON)
        ).andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.content().string(Matchers.equalTo("This is the first springboot application")));
    }

}

來(lái)源:http://blog.csdn.net/forezp/article/details/70341651

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末汁咏,一起剝皮案震驚了整個(gè)濱河市亚斋,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌攘滩,老刑警劉巖帅刊,帶你破解...
    沈念sama閱讀 217,406評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異漂问,居然都是意外死亡赖瞒,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門蚤假,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)栏饮,“玉大人,你說(shuō)我怎么就攤上這事磷仰∨坻遥” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,711評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵灶平,是天一觀的道長(zhǎng)伺通。 經(jīng)常有香客問(wèn)我,道長(zhǎng)逢享,這世上最難降的妖魔是什么罐监? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,380評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮拼苍,結(jié)果婚禮上笑诅,老公的妹妹穿的比我還像新娘调缨。我一直安慰自己疮鲫,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布弦叶。 她就那樣靜靜地躺著俊犯,像睡著了一般。 火紅的嫁衣襯著肌膚如雪伤哺。 梳的紋絲不亂的頭發(fā)上燕侠,一...
    開(kāi)封第一講書(shū)人閱讀 51,301評(píng)論 1 301
  • 那天者祖,我揣著相機(jī)與錄音,去河邊找鬼绢彤。 笑死七问,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的茫舶。 我是一名探鬼主播械巡,決...
    沈念sama閱讀 40,145評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼饶氏!你這毒婦竟也來(lái)了讥耗?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,008評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤疹启,失蹤者是張志新(化名)和其女友劉穎古程,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體喊崖,經(jīng)...
    沈念sama閱讀 45,443評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡挣磨,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評(píng)論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了荤懂。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片趋急。...
    茶點(diǎn)故事閱讀 39,795評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖势誊,靈堂內(nèi)的尸體忽然破棺而出呜达,到底是詐尸還是另有隱情,我是刑警寧澤粟耻,帶...
    沈念sama閱讀 35,501評(píng)論 5 345
  • 正文 年R本政府宣布查近,位于F島的核電站,受9級(jí)特大地震影響挤忙,放射性物質(zhì)發(fā)生泄漏霜威。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評(píng)論 3 328
  • 文/蒙蒙 一册烈、第九天 我趴在偏房一處隱蔽的房頂上張望戈泼。 院中可真熱鬧,春花似錦赏僧、人聲如沸大猛。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,731評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)挽绩。三九已至,卻和暖如春驾中,著一層夾襖步出監(jiān)牢的瞬間唉堪,已是汗流浹背模聋。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,865評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留唠亚,地道東北人链方。 一個(gè)月前我還...
    沈念sama閱讀 47,899評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像灶搜,于是被迫代替她去往敵國(guó)和親侄柔。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容