SpringBoot 初探

xiaoxiao2021-02-28  114

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方法

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

写controller

@RestController @EnableAutoConfiguration public 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

效果如下:

转载请注明原文地址: https://www.6miu.com/read-61425.html

最新回复(0)