spring boot 一 HelloWorld

沒事偷著樂琅發表於2017-11-03

1、下載 intellij 安裝。
2、直接使用Spring Initializr建立Springboot這裡寫圖片描述
注意 Project SDK 需要配置 jdk

然後一路 next:
這裡寫圖片描述
勾選 Web Web就可以了。

建立完成後 ,maven開始 下載相關的 jar檔案,根據網速可能會下載 1個小時左右,完成後,

3、完成後,在 demo目錄下 建立一個 HelloController 檔案。

package com.example.demo;

import com.example.demo.bean.person;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(){
        return "hello world~~!";
    }
}

4,在demo目錄下有個 DemoApplication 類,裡面有main()方法。
進入該類 右鍵 run DemoApplication.main() 就啟動spring Boot
內建的tomcat 並將helloworld 部署成功,
在瀏覽器中:
輸入 :127.0.0.1/hello
就會顯示出 hello world~~!。

如果顯示的是 404 注意檢視 控制檯,報的錯。根據錯誤來修改。如果顯示 80 埠被佔用,請檢視你是否run 了兩次

小知識:
在Application 類中

/**
@SpringBootApplication //等價於 下面三個 註解(被合併在一起了)
/*
@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = {"com.mybatis","cn.zll"})//指定需要掃描的包。預設掃描 自己所在目錄下的所有元件。
*/

相關文章