环境准备:
IDE:Eclipse
JAVA:JDK1.8
一、新建MAVEN项目
File→New→Maven Project→New Maven project
Group Id: com.springboot
Artifacrt Id: SpBootDemo
Packaging:war
右键项目,选择Java EE Tools→Generate Deployment Descriptor Stub,自动创建web.xml
二、声明依赖(pom.xml)
<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.springboot</groupId>
<artifactId>SpBootDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
三.编写java文件并运行
运行项目还需两个java文件,如下:
TestController.java,这个类用来测试访问项目
package com.springboot;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class TestController {
@RequestMapping("/")
@ResponseBody
public String test() {
System.out.println("springboot项目启动成功!");
return "springboot项目启动成功!";
}
}
WebApplication.java,这个类用来负责启动项目
package com.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}
在WebApplication类中右键运行项目,如下:
运行结果:
浏览器访问localhost:8080
至此,一个简单的Spring-boot已经搭建完成。