使用spring boot 去寫一個hello world
參考的內容:
1.官網-Spring Boot 基礎
2.騰訊課堂關于視頻教程-Java分布式互聯網架構/微服務/高性能/springboot/springcloud
一、Spring Boot 是什么?
Spring Boot 的目的是提供一組工具,以便快速構建容易配置的 Spring 應用程序。
二、通過idea編寫 Hello, World!
現在您已準備好開始直接使用 Spring Boot 了。本節中的示例基于一個名為 HelloWorldSpringBootMain 的簡單應用程序。您可以和我一起練習這個應用程序開發示例。
讓我們行動起來,創建一個新的 Maven 項目!
1. 創建 Maven 項目
在 IDEA 中,轉到 File > New Project 并選擇 Maven > Maven Project,如圖 1 所示。
圖 1. 選擇一個 SPRING 項目
單擊 Next,在隨后的對話框中(未給出)再次單擊 Next。
圖 2. 選擇 Maven quickstart 架構類型
勾選一下Create from archetype,選擇maven-archetype-quickstart
單擊 Next。
最后,輸入工件設置,如圖 3 所示。
圖 3. 選擇 Maven 工件設置
我為 HelloWorldSpringBootMain 應用程序使用了以下設置:
- Group Id:
cn.tx.springboot
- Artifact Id:
HelloWorldSpringBootMain
- Version:
1.0-SNAPSHOT
單擊 Finish 創建該項目。
2. 編輯POM
在原有的pom文件基礎上加上starter-parent。具體可參考下面官網的內容:
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-starter
修改POM.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.tx.springboot</groupId>
<artifactId>demo_01</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.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>
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3. 寫java文件
1)主文件HelloWorldSpringBootMain.java
package cn.tx.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Hello world!
*
*/
@SpringBootApplication
public class HelloWorldSpringBootMain
{
public static void main(String[] args )
{
SpringApplication.run(HelloWorldSpringBootMain.class,args);
}
}
2)controller文件TestController.java
package cn.tx.springboot;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class TestController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "helloworld";
}
}
然后在主程序頁面,開始運行,服務會啟動在8080端口
訪問:
http://localhost:8080/hello
4. 遇到的問題記錄
問題
Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:155) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:540) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at cn.tx.springboot.HelloWorldSpringBootMain.main(HelloWorldSpringBootMain.java:14) [classes/:na]
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:204) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:178) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:152) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
... 8 common frames omitted
解決
在主文件中,沒有加@SpringBootApplication
,導致了這個問題
錯誤代碼
package cn.tx.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
public class HelloWorldSpringBootMain
{
public static void main(String[] args )
{
SpringApplication.run(HelloWorldSpringBootMain.class,args);
}
}
正確代碼
package cn.tx.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloWorldSpringBootMain
{
public static void main(String[] args )
{
SpringApplication.run(HelloWorldSpringBootMain.class,args);
}
}
5. 通過jar包啟動
1)修改pom文件
加上這個項目的包的類型為jar
...
<groupId>cn.tx.springboot</groupId>
<artifactId>demo_01</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
...
2)打jar包
在idea的右上角,選擇lifecycle里的package,去生成jar包,生成的jar包會在項目文件中的target
里。
3)終端執行jar包
在終端中通過命令java -jar 包名
執行jar包
還是通過8080端口訪問到web服務
http://localhost:8080/hello
至此,第一個springboot的項目就通過了