SpringBoot 快速入门

使用SpringBoot实现一个简单功能,使用maven搭建:

  浏览器发送hello请求,服务器接受请求并处理,响应Hello World字符串;

1.创建Maven工程

2.在pom.xml里面引入springboot依赖

1 <parent>
2     <groupId>org.springframework.boot</groupId>
3     <artifactId>spring-boot-starter-parent</artifactId>
4     <version>2.1.3.RELEASE</version>
5  </parent>

3.添加spring-boot启动器依赖

1 <dependencies>
2     <dependency>
3         <groupId>org.springframework.boot</groupId>
4         <artifactId>spring-boot-starter-web</artifactId>
5     </dependency>
6 </dependencies>

 4.编写一个主程序;启动Spring Boot应用 

1 //@SpringBootApplication 来标注一个主程序类,说明这是一Spring Boot应用 
2 @SpringBootApplication 
3 public class HelloWorldMainApplication {        
4        public static void main(String[] args) {           
5        // Spring应用启动起来         
6 SpringApplication.run(HelloWorldMainApplication.class,args);     
7 }
8 }

5.编写相关的Controller、Service 

1 @Controller 
2 public class HelloController {       
3     @ResponseBody     
4     @RequestMapping("/hello")     
5     public String hello(){         
6          return "Hello World!";    
7     } 
8 }

6.运行主程序测试 ,启动HelloWorldMainApplication这个类

1 //在地址栏上面输入
2 localhost:8080/hello

小知识:

       第5个步骤上面的使用为@controller这个常规写法,在SpringBoot里面可以将@controller和@ResponseBody  结合使用,写成@RestController

原文地址:https://www.cnblogs.com/pcliu/p/11099079.html