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

文章的示例工程的功能以文件的上传下载为例

源码地址:https://gitee.com/jlzhou/SpringBoot2.x

快速开始

​ 进入 https://start.spring.io 填写 Group/Artifact ,选择SpringBoot:2.1.0,选择依赖:web

生成基础工程并打包下载。

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

由于本人会编写多个工程,所以工程的基本配置统一存放在父工程内,父工程为当前工作空间根目录下的pom.xml。

<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则比较简单

<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依赖的时候的网络问题引起的,解决方案:

打开C:/Users/[Administrator]/.m2/settings.xml

如果不存在,到http://maven.apache.org/download.cgi下载Maven的zip,到里面找

建议添加aliyun和spring的maven中央仓库;如果在公司内部,添加公司私仓。

<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

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

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

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

打开浏览器,输入 http://127.0.0.1:8080

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

点击 hello

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

结束语

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微服务框架的搭建。计划中...

正式失业在家,总得做点什么吧!

转载于:https://my.oschina.net/jlzhou/blog/2961655