准备工作1
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
准备工作2
Eclipse Java EE IDE for Web Developers.
Version: Neon.1a Release (4.6.1)
Build id: 20161007-1200
该版本集成了MAVEN插件 无需安装
请配置好MAVEN的环境 (部分省略)
搭建spring-boot 只需几部 十分简单
1. 新建一个3.0的 Web Pro0ject
File - New - Dynamic Web Project
输入 ProjectName
Target Runtime 选择 Apache Tomcat 8.5 如果没有就 new 一个
Dynamic Web module version 选择3.0
Finish
2. 将工程转换为 Maven 工程
右键工程 Configure - Convert to Maven Project
在 GroupId 中输入包名 例如 com.avicsafety.webapp
Finish
在POM.XML中 拷贝以下代码并保存
<?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.avicsafety.webapp</groupId>
<artifactId>MyWebApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.6.RELEASE</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
右键工程 - Maven - Update Project
转换工程结束
3. 启用工程
右键工程中的 src - Build Path - Use as Source Folder
展开工程中的 Java Respource 右键 src 选择 New - Class
Package 中输入自己的包名 com.avicsafety.webapp
Name 中输入 MyApplication
Finish
在打开的MyApplication.java 中输入以下代码并保存
package com.avicsafety.webapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
@SpringBootApplication
public class MyApplication {
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(MyApplication.class, args);
}
}
右键该文件 选择 Run As - Java Application
控制台出现 Started MyApplication in 3.096 seconds (JVM running for 3.449)
恭喜 Spring boot 搭建完毕
打开浏览器输入 http://localhost:8080/ 可以看到 Hello World!