spring boot學習1————搭建與簡單的使用

    spring boot的搭建非常簡單

先創建mavan項目,在java目錄下創建一個啓動類application(類名隨意),此處application文件必須在最外層,spring boot 只會去掃描application低層數文件中的方法wKiom1l_5KHRTd8_AAAT7HhC6pg002.png-wh_50


用SpringBootApplication註解標識爲項目啓動類:
Application {
   main(String[] args) {
      SpringApplication.(AccountApplication., args);
   }

}

然後添加依賴:

 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>1.3.5.RELEASE</version>
</dependency>
再是controller層:

用@Controller或@RestController指明爲controller
RestController註解的效果相當於@Controller+@ResponseBody,使用RestController時只需直接return字符串就會把數據返回給前端,
方法部分依舊使用@RequestMapping註解
比如:
@Controller
@RequestMapping(value="/test")
public class test{
    @ResponseBody
    @RequestMapping(value="/hello",method = RequestMethod.GET)
    public String  test(){
        System.out.print("11111");
        return "222222";
    }

}
等效於
@RestController
@RequestMapping(value="/test")
public class test{
    @RequestMapping(value="/hello",method = RequestMethod.GET)
    public String  test(){
        System.out.print("11111");
        return "222222";
    }

}

以上spring boot 就算搭好了,spring boot內嵌了tomcat,只需啓動main方法,web端就可以訪問項目中的方法了
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章