spring boot 學習筆記(002) Hello world

1,在src/main/java下,新建類:

HelloWorld


如下代碼:

package springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;  

@Controller  
@EnableAutoConfiguration  	
@SpringBootApplication
public class HelloWorld {

	public static void main(String[] args) {
		//第一個簡單的應用,  
        SpringApplication.run(HelloWorld.class,args);
	}

	@RequestMapping("/hello")  
    @ResponseBody  
    public String hello(){  
  
        return "Hello World";  
    }  

	@RequestMapping("/rundemo")  
    @ResponseBody  
    public String demo(){  
  
        return "This is demo";  
    }  

}


2,右鍵工程,選擇“Run as” / "Maven build ..."

參數  -X clean install


然後 Run


編譯通過:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 16.605 s
[INFO] Finished at: 2016-06-28T13:41:28+08:00
[INFO] Final Memory: 24M/311M
[INFO] ------------------------------------------------------------------------


3,

對HelloWorld.java 右鍵,點擊“Run as ” "Java Appleation"

就啓動了


瀏覽器裏,輸入:

http://localhost:8080/rundemo

或者

http://localhost:8080/hello


都能看到效果。



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