Chapter1Application.java 是启动类,HelloController.java 是一个简单的控制层,application.properties是配置项, 最后是pom.xml
1.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>
<!--
这里引用了我自建的父类包,如果没有,可以把父类的pom里面的内容复制到这里来直接引包,
我建一个父类包是为了后面还有很多小项目不用每次都倒,这样把相同公共的包放父类里面
-->
<parent>
<artifactId>springBoot</artifactId>
<groupId>com.guilf</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>springBoot-1</artifactId>
<dependencies>
<!-- 父类包去掉了tomcat,这样每个项目有自建的tomcat,也可以好设置端口号. -->
<!--默认spring-boot-starter-web包含了tomcat ,所以打包成war包时排除tomcat.由容器提供. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<!--
maven编译时出现There are test failures 错误,其原因就是test 部分编译出错。解决方法如下:
1, 添加以下配置,使得测试出错不影响项目的编译。
2. mvn install -DskipTests 或者 mvn install -Dmaven.test.skip=true
-->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.application.properties
# tomcat端口号
server.port=8011
3.Chapter1Application.java
package com.guilf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
/**
* SpringBoot 启动类.
*
* 继承SpringBootServletInitializer 类,覆盖configure(),把启动类Chapter1Application 注册进去。
* 外部web应用服务器构建Web Application Context的时候,会把启动类添加进去。
*/
@SpringBootApplication
public class Chapter1Application { // extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Chapter1Application.class, args);
System.out.println("run spring ====");
}
// @Override
// protected SpringApplicationBuilder configure(SpringApplicationBuilder
// builder) {
// return builder.sources(Chapter1Application.class);
// }
}
4.HelloController.java
package com.guilf.springBoot_1;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 最简单的springBoot的应用
* @author DELL
*
*/
@RestController
public class HelloController {
@RequestMapping("/hello2")
public String index(){
return "hello Spring Boot33..";
}
}
5.启动Chapter1Application.java里面的main方法
6.页面查看