根据官方文档https://docs.spring.io
-
版本
jdk:1.8
maven:3.3
java -version
mvn -version
-
修改maven的配置文件settings.xml
目的:改变镜像源,加快jar包的下载
这里我用的是idea自带的maven
maven下载页:https://maven.apache.org/download.cgi
<mirrors>
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
- 创建工程
- 添加springboot及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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ll</groupId>
<artifactId>myspringboot</artifactId>
<version>1.0-SNAPSHOT</version>
<!--引入springoot-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
</parent>
<!--开启web项目-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
- 代码编写
添加启动类MainApplication.java
package com.ll.controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* SpringBootApplication:这是一个springboot应用
*/
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class,args); // 固定写法
}
}
控制器HelloWord.java
package com.ll.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
* ResponseBody:代表返回值是以字符串方式写给页面而不是转到地址
* @ResponseBody+@Controller=@RestController
*/
@RestController
public class HelloWord {
@RequestMapping("/hello")
public String Hello(){
return "hello";
}
}
项目结构
-
运行效果
-
application.properties文件
能修改什么配置到官方文档查看
https://docs.spring.io/spring-boot/docs/2.4.3/reference/html/appendix-application-properties.html#common-application-properties
-
打包
打成的jar包有运行工程所需要的配置
添加xml配置
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
java -jar myspringboot-1.0-SNAPSHOT.jar