Spring Boot的簡單入門

Spring Boot的主要優勢:web

  • 爲全部Spring開發者更快的入門
  • 開箱即用,提供各類默認配置來簡化項目配置
  • 內嵌式容器簡化Web項目
  • 沒有冗餘代碼生成和XML配置的要求
    建立基礎項目:
  • 首先建立maven項目
  • 配置maven環境
    <--在pom.xml中添加Spring Boot相關的父級依賴 spring-boot-starter-parent提供了項目相關的默認依賴,使用它以後,經常使用的包能夠省去version標籤-->

    org.springframework.boot
    spring-boot-starter-parent
    2.0.6.RELEASE





    org.springframework.boot
    spring-boot-starter-web

  • 建立控制器Controller

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;spring

@Controller
public class helloController {app

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

}maven

  • 建立一個引導類
    主要做用是做爲啓動Spring Boot項目的入口
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class helloMailApplication {
public static void main(String[] args) {
SpringApplication.run(helloMailApplication.class,args);
}
}spring-boot

  • 運行效果以下:
    code

  • 在web端訪問 http://localhost:8080/hello
    xml

相關文章
相關標籤/搜索