Spring Boot HelloWorld

2018-10-20  本文已影响0人  不知所措的STRANGER

1、Spring Boot HelloWorld

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

1、创建一个maven工程;(jar)

2、导入spring boot相关的依赖

<parent> 
 <groupId>org.springframework.boot</groupId> 
 <artifactId>spring-boot-starter-parent</artifactId> 
 <version>2.0.4.RELEASE</version> 
</parent>
<dependencies> 
  <dependency>
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-web</artifactId>
  </dependency> 
</dependencies> 

3、编写一个主程序;启动Spring Boot应用

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

4、编写相关的Controller、Service

@Controller 
public class HelloController {
  @ResponseBody 
  @RequestMapping("/hello") 
  public String hello(){ return "Hello World!"; }
}
上一篇下一篇

猜你喜欢

热点阅读