1.环境信息
MyEclipse2015,jdk1.8,tomcat8
2.新建maven项目
2.1 新建一个web项目
2.2 生成的项目结构如下
3.配置pom.xml文件
3.1 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.qfx</groupId>
<artifactId>springbootDemo01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>springbootDemo01</name>
<description>这里填写描述信息</description>
<!-- 设置父类,整合第三方常用框架依赖信息(各种依赖信息) -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</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>
<!-- 引入springboot核心包,整合SpringMVC Web组件 -->
<!-- 实现原理:Maven依赖继承关系,相当于把第三方常用Maven依赖信息,在parent项目中已经封装好了 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引入springboot测试包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- springboot外部tomcat支持,用于编译jsp -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- 打war包时加入此项, 告诉spring-boot tomcat相关jar包用外部的,不要打进去 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<!-- 指定war包名称,以此处为准,否则会带上版本号 -->
<finalName>springbootDemo01</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.2 修改完毕,如果你的项目名上出现一个红色的小叉号
请按照下图进行操作即可
4.创建springboot启动类DemoApp.java
代码:
package com.qfx.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
/**
* <h5>描述:添加外部tomcat支持,需要继承SpringBootServletInitializer,并重写configure方法</h5>
*
*/
@SpringBootApplication
public class DemoApp extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(DemoApp.class);
}
public static void main(String[] args) {
// 整个程序入口,启动springboot项目,创建内置tomcat服务器,使用tomct加载springmvc注解启动类
SpringApplication.run(DemoApp.class, args);
}
}
5.创建一个Conttoller
代码:
package com.qfx.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@RequestMapping("getUser")
public String getUser(){
return "张三";
}
}
6.编译项目
7.启动springboot,执行DemoApp类的main方法
8.测试访问:http://127.0.0.1:8080/getUser
使用springboot内置tomcat启动,默认省略项目名,端口默认8080,访问结果如下图,表示启动成功
9.最后附项目完整结构
原文地址:http://blog.51cto.com/1197822/2162063
时间: 2024-10-09 09:44:48