第一步:使用eclipse建立一个maven的web项目 (在此推荐一下篇进行参考 在eclipse中,如何用maven创建一个web项目)
第二步:修改项目中的pom.xml 引入必要的jar
<?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.mySpringBoot</groupId>
<artifactId>TestSpringBoot</artifactId>
<version>1.1.0-RELEASE</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath></relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>TestSpringBoot</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
第三步:在src/main/resources目录下 新建一个application.properties 添加下面的配置
server.port=8080
server.session-timeout=30
server.tomcat.uri-encoding=UTF-8
第四步:新建一个Application类 编写SpringBoot启动类
package com.myboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
第五步:新建一个TestController类 编写一个测试方法
package com.myboot.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class TestController {
@RequestMapping(value = "/firstBoot")
@ResponseBody
public String firstBoot(){
return "Hello SpringBoot!";
}
}
第六步:选择启动类Application 按下图的方式运行
出现下图则启动成功
第七步:打开浏览器 输入localhost:8080/firstBoot
解释一下为什么地址是这个:localhost是服务器地址, 8080是第三步设置的端口,当然在第六步启动成功,也可以看见自己设置的端口号,/firstBoot是RequestMapping所设置的地址
可能会出现的bug
1.在org.apache.tomcat.util.scan.StandardJarScanner类中存在setJarScanFilter(Lorg/apache/tomcat/JarScanFilter;)方法,并且亲自测试,可以正常使用。而抛出异常却说没有此方法。
分析原因:1.存在重复的jar包。2.版本调整带来的影响。
经排查,发现 Tomcat v7.0 Runtime Libraries包,和Maven Dependencies包中均包含org.apache.tomcat.util.scan.StandardJarScanner类。
所以把Tomcat v7.0 Runtime Libraries去掉
好了,完