本項目主要是講創建一個SpringBoot的HelloWorld項目
項目地址:https://github.com/Chakid/SpringBoot-example
該項目直接以maven的方式創建
pom文件中導入相關包:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!--將應用打包成可執行的jar包-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
創建SpringBoot啓動類:
@SpringBootApplication
public class MainApplication {
/**
* Spring應用啓動起來
*/
public static void main(String[] args) {
SpringApplication.run(MainApplication.class,args);
}
}
創建一個控制類
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "Hello SpringBoot";
}
}
創建好相關類,具體代碼可見項目中的實現,運行啓動類;瀏覽器訪問 http://127.0.0.1:8080/hello
即可見到相關效果: