1. 程式人生 > >SpringBoot學習(一) ---- HelloWorld

SpringBoot學習(一) ---- HelloWorld

一、SpringBoot是什麼?能幹什麼?

Boot有引導之義,SpringBoot可以幫助開發者快速搭建Spring開發框架。

SpringBoot幫助開發者快速啟動web容器,簡化Spring使用過程。SpringBoot採用Java Config的方式對Spring進行配置,將原有Spring的配置簡化。

二、HelloWorld專案

1,本文使用STS開發工具(Spring Tool Suite)

new - Spring Start Project 新建一個springboot工程。

<?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>springboot</groupId>
	<artifactId>helloworld</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>springboot</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.6.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>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

2,新建controller包,編寫程式碼如下:

@RestController
public class HelloworldController {
	@RequestMapping("/")
	public String sayHello(){
		return "hello,world!";
	}
}

3,啟動類Application

@SpringBootApplication
public class SpringbootApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootApplication.class, args);
	}
}

4,controller測試類

public class HelloworldControllerTest {
	@Test
	public void testSayHello(){
		Assert.assertEquals("hello,world!", new HelloworldController().sayHello());
	}
}

三、執行

找到Application類,右鍵run as ,訪問http://localhsot:8080/,頁面上會顯示hello,world!

四、小結

1,pom配置

2,簡單web

引用資料:
1, https://www.bysocket.com/?p=1124