使用Maven创建一个基于Spring Boot的Web项目
前提说明:
安装好Maven, 使用 mvn -v 命令查看版本, 保证java的版本支持 Spring Boot.
创建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.example</groupId> <artifactId>myproject</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> </parent> <!-- Additional lines to be added here... --> </project>
这个是使用Maven构建项目的必要文件.
使用Maven命令 mvn package 执行, 并编译项目. 这个时候, 你会发现, 程序一直在下载一些需要的Spring Boot的jar文件.
查看项目目录, 可以使用命令 mvn dependency:tree 命令查看项目架构.
mvn dependency:tree
添加 web的支持.
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
添加依赖, 之后, 重新执行.
mvn dependency:tree
就可以看到如下信息:
[INFO] --- maven-dependency-plugin:2.10:tree (default-cli) @ myproject --- [INFO] com.example:myproject:jar:0.0.1-SNAPSHOT [INFO] \- org.springframework.boot:spring-boot-starter-web:jar:1.5.4.RELEASE:compile [INFO] +- org.springframework.boot:spring-boot-starter:jar:1.5.4.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot:jar:1.5.4.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-autoconfigure:jar:1.5.4.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-starter-logging:jar:1.5.4.RELEASE:compile [INFO] | | +- ch.qos.logback:logback-classic:jar:1.1.11:compile [INFO] | | | +- ch.qos.logback:logback-core:jar:1.1.11:compile [INFO] | | | \- org.slf4j:slf4j-api:jar:1.7.25:compile [INFO] | | +- org.slf4j:jcl-over-slf4j:jar:1.7.25:compile [INFO] | | +- org.slf4j:jul-to-slf4j:jar:1.7.25:compile [INFO] | | \- org.slf4j:log4j-over-slf4j:jar:1.7.25:compile [INFO] | +- org.springframework:spring-core:jar:4.3.9.RELEASE:compile [INFO] | \- org.yaml:snakeyaml:jar:1.17:runtime [INFO] +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.5.4.RELEASE:compile [INFO] | +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.15:compile [INFO] | +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.15:compile [INFO] | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.15:compile [INFO] +- org.hibernate:hibernate-validator:jar:5.3.5.Final:compile [INFO] | +- javax.validation:validation-api:jar:1.1.0.Final:compile [INFO] | +- org.jboss.logging:jboss-logging:jar:3.3.1.Final:compile [INFO] | \- com.fasterxml:classmate:jar:1.3.3:compile [INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.8.8:compile [INFO] | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.8.0:compile [INFO] | \- com.fasterxml.jackson.core:jackson-core:jar:2.8.8:compile [INFO] +- org.springframework:spring-web:jar:4.3.9.RELEASE:compile [INFO] | +- org.springframework:spring-aop:jar:4.3.9.RELEASE:compile [INFO] | +- org.springframework:spring-beans:jar:4.3.9.RELEASE:compile [INFO] | \- org.springframework:spring-context:jar:4.3.9.RELEASE:compile [INFO] \- org.springframework:spring-webmvc:jar:4.3.9.RELEASE:compile [INFO] \- org.springframework:spring-expression:jar:4.3.9.RELEASE:compile
创建需要的目录代码
创建目录: src/main/java
然后, 在该目录下创建 Example.java 文件 src/main/java/Example.java, 源码文件内容如下:
import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.stereotype.*; import org.springframework.web.bind.annotation.*; @RestController @EnableAutoConfiguration public class Example { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(Example.class, args); } }
执行程序查看效果
执行命令: mvn spring-boot:run
然后使用 http://localhost:8080 查看即可.