一、创建一个maven项目
二、pom文件引入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>tx.com</groupId>
<artifactId>springboot_maven</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</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-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 这个插件 将应用打包成可执行的jar包-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
三、创建springboot的应用启动类Application
package com.tx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
*
* @SpringBootApplication 用于标注本程序是主程序类,说明这是一个springboot应用
*
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
//启动springboot应用
SpringApplication.run(Application.class, args);
}
}
四、创建controller类
package com.tx.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by TDH on 2019/2/25.
*
* 使用 @RestController
* controller里面的方法都以json格式输出,不用再写jackjson配置
* 相当于同时添加@Controller和@ResponseBody注解
*/
@RestController
public class HelloWorldController {
@RequestMapping(value ="/hello", method = RequestMethod.GET)
public String index() {
return "Hello World";
}
}
五、启动项目
进入Application启动类中,右键run启动项目,浏览器输入localhost:8080/hello
六、项目解析
1、对pom的说明
pom文件中的jar包没有添加版本号,但是项目没有报错,原因增加了父pom:spring-boot-starter-parent,spring-boot-starter-parent包含了大量配置好的依赖管理,在自己项目添加这些依赖的时候不需要写版本号。如果想自己引入别的版本的jar,可以参考以下方式:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.2.3.RELEASE</version>
<type>pom</type>
<scope>import</scope><!—这个地方-->
</dependency>
</dependencies>
</dependencyManagement>
2、关于java.version属性
Spring默认使用jdk1.6,如果你想使用jdk1.8,你需要在pom.xml的属性里面添加java.version,如下:
<properties>
<java.version>1.8</java.version>
</properties>
3、spring-boot-maven-plugin插件
该插件支持多种功能,常用的有两种,第一种是打包项目为可执行的jar包。
在项目根目录下执行mvn package将会生成一个可执行的jar包,jar包中包含了所有依赖的jar包,只需要这一个jar包就可以运行程序,使用起来很方便。该命令执行后还会保留一个XXX.jar.original的jar包,包含了项目中单独的部分。idea使用package命令打包:
生成这个可执行的jar包后,在命令行执行java -jar xxxx.jar即可启动项目。
另外一个命令就是mvn spring-boot:run,可以直接使用tomcat(默认)启动项目。
在我们开发过程中,我们需要经常修改,为了避免重复启动项目,我们可以启用热部署。
4、Application.java的存放位置
Application.java 建议放到根目录下面,主要用于做一些框架配置,扫描注解。
如果不是根目录,如下图:
这样是加载不到HelloWorldController这个类。访问的时候会报: