1. 程式人生 > >SpringBoot實戰開發(一)——HelloWord實現

SpringBoot實戰開發(一)——HelloWord實現

1、建立Maven project專案


點next,然後選擇quickstart模板



以上完成Maven專案的建立

2、引入Pom依賴

<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.scw.clear</groupId>
	<artifactId>sc-clear</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<!-- spring boot 父節點依賴,引入這個之後相關的引入就不需要新增version配置,spring boot會自動選擇最合適的版本進行新增。 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.9.RELEASE</version>
		<relativePath />
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<!-- 指定一下jdk的版本 ,這裡我們使用jdk 1.8 ,預設是1.6 -->
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<!--<version></version>由於我們在上面指定了 parent(spring boot) -->
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<!--<version></version>由於我們在上面指定了 parent(spring boot) -->
		</dependency>
		<!-- spring boot devtools 熱部署 依賴包. -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
			<scope>true</scope>
		</dependency>

	</dependencies>
	<build>
	    <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
                <!-- 跳過單元測試 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <skipTests>true</skipTests>
                    </configuration>
                </plugin>
            </plugins>
	</build>


</project>

3、建立啟動類

package com.sc.clear;

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

@SpringBootApplication(scanBasePackages="com.sc.clear")
public class ScApplication {

	
	public static void main(String[] args) {
		//專案入口
		SpringApplication.run(ScApplication.class, args);
	}
}

4、建立HelloController,測試HelloWorld

package com.sc.clear.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

	@RequestMapping(value="/hello",method=RequestMethod.GET)
	public String helloWorld() {
		return "Hello World";
	}

}
以上完成所有操作。通過啟動類的main方法右鍵Run As=====》Java Application進行啟動專案。通過http://localhost:8080/hello進行測試。