SpringBoot 2.x工程初始化(一)

文章的示例工程的功能以文件的上傳下載爲例html

源碼地址:https://gitee.com/jlzhou/SpringBoot2.xjava

快速開始

​ 進入 https://start.spring.io 填寫 Group/Artifact ,選擇SpringBoot:2.1.0,選擇依賴:webgit

生成基礎工程並打包下載。web

https://start.spring.io

因爲本人會編寫多個工程,因此工程的基本配置統一存放在父工程內,父工程爲當前工做空間根目錄下的pom.xml。spring

<groupId>top.jlzhou.boot</groupId>
	<artifactId>parent</artifactId>
	<version>0.1</version>
	<packaging>pom</packaging>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.0.RELEASE</version>
		<relativePath />
	</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-devtools</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<scope>provided</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>

		<dependency>
		    <groupId>com.alibaba</groupId>
		    <artifactId>fastjson</artifactId>
		    <version>1.2.51</version>
		</dependency>
	</dependencies>

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

我的工程的pom.xml則比較簡單apache

<artifactId>boot-war</artifactId>
	<packaging>jar</packaging>

	<parent>
		<groupId>top.jlzhou.boot</groupId>
		<artifactId>parent</artifactId>
		<version>0.1</version>
		<relativePath>../pom.xml</relativePath>
	</parent>

關於maven工程初始化失敗的問題。主要爲下載jar依賴的時候的網絡問題引發的,解決方案:json

打開C:/Users/[Administrator]/.m2/settings.xml瀏覽器

若是不存在,到http://maven.apache.org/download.cgi下載Maven的zip,到裏面找網絡

建議添加aliyun和spring的maven中央倉庫;若是在公司內部,添加公司私倉。app

<mirrors>
	<mirror>
      <id>aliyun</id>
      <name>aliyun maven</name>
      <mirrorOf>central</mirrorOf>
      <url>https://maven.aliyun.com/repository/central</url>
    </mirror>
    <mirror>
      <id>spring</id>
      <name>spring maven</name>
      <mirrorOf>central</mirrorOf>
      <url>https://repo.spring.io/release/</url>
    </mirror>
  </mirrors>

導入工程:

本人使用的是Eclipse+STS4

import

Hello World

建立靜態文件:/upload/src/main/resources/static/index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Spring Boot 2.x</title>
</head>
<body>
<h1>Spring Boot 2.x</h1>
<a href="hello">hello</a>
</body>
</html>

建立控制器:/upload/src/main/java/top/jlzhou/boot/controller/HelloController.java

@RestController
public class HelloController {

	@RequestMapping("hello")
	public String hello() {
		return "Hello world";
	}
}

運行SpringBoot:

右鍵工程 > Run as > Spring boot App

run-console

打開瀏覽器,輸入 http://127.0.0.1:8080

run-1

點擊 hello

run-2

結束語

SpringBoot2.1的工程基本的建立完成,計劃後面在講點什麼呢

  1. SpringBoot2.x的JSON支持:Jackson2與FastJSON
  2. SpringBoot2.x的JSON支持:Long類型
  3. SpringBoot2.x的日誌
  4. SpringBoot2.x的Servlet
  5. SpringBoot2.x的Filter
  6. ...

SpringCloud微服務框架的搭建。計劃中...

正式失業在家,總得作點什麼吧!

相關文章
相關標籤/搜索