springboot系列3-helloworld實現
阿新 • • 發佈:2018-11-01
1.匯入springboot相關的依賴
開啟pom.xml檔案,新增如下內容
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
</parent>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2.編寫一個主程式,啟動SpringBoot應用
package com.air;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldMainApplication.class, args);
}
}
3.編寫相關的Controller、Service
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello() {
return "Hello world!";
}
}
4.測試
執行main方法:
2018-10-24 21:44:01.858 INFO 17944 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-10-24 21:44:01.861 INFO 17944 --- [ main] com.air.HelloWorldMainApplication : Started HelloWorldMainApplication in 1.659 seconds (JVM running for 3.059)
看到tomcat已經啟動
瀏覽器中輸入下面的地址測試:
http://localhost:8080/hello
瀏覽器會輸出Hello world!
至此,helloworld 工程已經成功完成