1. 程式人生 > >一、spring boot 2.x hello world

一、spring boot 2.x hello world

1、建立maven專案,結構如下:

2、在pom中引入相關依賴

<?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>com.ldy</groupId>
	<artifactId>boot_v2_hello</artifactId>
	<version>1.0.0</version>
	<packaging>jar</packaging>

	<name>boot_v2_hello</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>

3、建立入口類:BootV2App.java

package com.ldy.bootv2.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BootV2App {

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

4、編寫hello world 介面:HelloController.java

package com.ldy.bootv2.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

	@GetMapping("/hello")
	public String index(String name) {
		return "hello " + name;
	}

}

5、啟動專案:執行BootV2App.java中的main方法

滑鼠右鍵- ->Run As --> Java Application,啟動成功後控制檯顯示資訊如下:

6、測試hello world 介面:開啟瀏覽器,在位址列輸入:127.0.0.1:8080/hello?name=ldy,如圖

到此,spring boot 2.x構建結束!