SpringBoot 初探

来源:互联网 发布:学mysql还是sql server 编辑:程序博客网 时间:2024/06/06 00:43

1.SpringBoot hellWorld

第一步: 新建MAVEN项目,目录结构如下:

第二步:pom.xml中增加springboot的引用

<parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.1.RELEASE</version>    </parent>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>    </dependencies>
第三步:
写一个项目启动入口main方法

@SpringBootApplicationpublic class ApplicationTest {    public static void main(String[] args) {        run(ApplicationTest.class,args);    }}
第四步:

写controller

@RestController@EnableAutoConfigurationpublic class Example {    @RequestMapping("/")    String home(){        return "Hello world";    }    @RequestMapping("/hello/{name}")    String index(@PathVariable("name") String name){        return "Hello "+name;    }}
第五步:

通过run ApplicationTest类中的main方法启动项目

浏览器访问http://localhost:8080/

                    http://localhost:8080/hello/1 ========>/hello/1=========>Example中index方法,name=1

效果如下: