springboot(1)快速搭建WEB环境

1、建立项目

建议使用spring-tool-suite来建立编辑springboot项目,该版本的Eclipse比普通版本的Eclipse添加了对springboot的插件支持。
创建本地maven项目,并编辑pom.xml文件,添加如下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.7.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.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>
</dependencies>

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

2、创建springboot启动入口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.wxtx;

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

/**
* springboot默认值扫描启动入口所在的包及其子包,可用过scanBasePackages参数指定扫描的包路径
*/
@SpringBootApplication(scanBasePackages="com")
public class TXSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(TXSpringBootApplication.class, args);
}
}

3、创建Control控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.wxtx.action;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestAction {

@ResponseBody
@RequestMapping("/index")
public String index() {
return "hello world";
}
}

4、启动项目

java运行TXSpringBootApplication类,打开浏览器访问http://127.0.0.1:8080/index即可。

注:springboot内置了tomcat,所以springboot web项目启动时,不需要使用tomcat server来启动,直接java application运行即可。

>