开发工具:IntelliJ IDEA
代码管理:MAVEN
jdk版本:1.8
1.快速度搭建SpringBoot Maven项目
1.1在IDEA里选择file-->new project :
1.2 选择 Sping Initializr 选择JDK 1.8,点击下一步:
注意:这里TYPE选择的是maven pom。
2.spring-boot-config项目创建完了,现在创建该项目下的子工程:
2.1 选择spring-boot-config 右键new一个Modeule,选择Maven创建工程
在这个界面Create from archetype 这边不需要选择。点击下一步填写GroupId和Artifactid信息,点击下一步:
这里填写Module name,选择Finish。
3.项目创建完成。
3.1我这里修改了一下spring-boot-config的pom文件。没有用spring-boot-starter-parent这种方式。文件内容如下:
<?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>
<!--<parent>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-parent</artifactId>-->
<!--<version>2.1.3.RELEASE</version>-->
<!--<relativePath/> <!– lookup parent from repository –>-->
<!--</parent>-->
<groupId>com.jm.boot</groupId>
<artifactId>spring-boot-config</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<modules>
<module>com-jm-web</module>
</modules>
<name>spring-boot-config</name>
<description>spring-boot-config project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!--以下两项需要如果不配置,解析themleaft 会有问题-->
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.5</thymeleaf-layout-dialect.version>
</properties>
<dependencies>
<!-- 热部署模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> <!-- 这个需要为 true 热部署才有效 -->
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>Cairo-SR7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.2 修改spring-boot-web 下的pom文件,增加spring-boot-starter-web 信息:
<?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">
<parent>
<artifactId>spring-boot-config</artifactId>
<groupId>com.jm.boot</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.jm.boot</groupId>
<artifactId>com.jm.web</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
4.项目搭建完成,项目结构如下:
现在写第一个spring boot项目代码。先从最简单的开始。
4.1 创建Spring boot 项目的启动类WebApplicationService 内容如下:
@SpringBootApplication
public class WebApplicationService {
/**spring boot 项目启动入口,有点像java cs代码模式*/
public static void main(String[] args) {
SpringApplication.run(WebApplicationService.class,args);
}
}
启动类写好后,在这个时候启动项目会报一个错误
[2019-03-28 17:53:15.018] - 15428 信息 [restartedMain] --- com.jm.web.WebApplicationService: Starting WebApplicationService on PCOS-1604062057 with PID 15428 (D:\MyIdeaProject\spring-boot-config\com-jm-web\target\classes started by Administrator in D:\MyIdeaProject\spring-boot-config)
[2019-03-28 17:53:15.039] - 15428 信息 [restartedMain] --- com.jm.web.WebApplicationService: No active profile set, falling back to default profiles: default
[2019-03-28 17:53:15.185] - 15428 信息 [restartedMain] --- org.springframework.context.annotation.AnnotationConfigApplicationContext: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@4b1fb1ef: startup date [Thu Mar 28 17:53:15 CST 2019]; root of context hierarchy
[2019-03-28 17:53:16.365] - 15428 信息 [restartedMain] --- org.springframework.boot.devtools.autoconfigure.OptionalLiveReloadServer: LiveReload server is running on port 35729
[2019-03-28 17:53:16.392] - 15428 信息 [restartedMain] --- org.springframework.jmx.export.annotation.AnnotationMBeanExporter: Registering beans for JMX exposure on startup
[2019-03-28 17:53:16.421] - 15428 信息 [restartedMain] --- com.jm.web.WebApplicationService: Started WebApplicationService in 1.732 seconds (JVM running for 2.964)
Disconnected from the target VM, address: '127.0.0.1:43540', transport: 'socket'
这个错是启动的时候没有找到application.yml配置文件No active profile set, falling back to default profiles: default
现在新建一个application.yml,里面就配置一个端口和项目path
现在项目能够正常运行。
5. 写一个返回hello word的controller
@RestController
@RequestMapping("/hello")
public class HelloWordController {
@RequestMapping(value = "/get",method = RequestMethod.GET)
public String getHelloWord(){
String strString = "Hello Word!";
return strString;
}
}
运行结果