spring boot学习1————搭建与简单的使用

    spring boot的搭建非常简单

先创建mavan项目,在java目录下创建一个启动类application(类名随意),此处application文件必须在最外层,spring boot 只会去扫描application低层数文件中的方法spring boot学习1————搭建与简单的使用


用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端就可以访问项目中的方法了