Daily Plan
更新: 5/3/2025 字数: 0 字 时长: 0 分钟
- [ ]
Daily Study
更新: 5/3/2025 字数: 0 字 时长: 0 分钟
SpringBoot配置和简单应用
步骤
- 安装Java
- 安装Maven
- 创建Spring Boot项目
- 构建和运行 Spring Boot 应用
创建Spring Boot项目
创建一个新目录作为项目根目录,并进入该目录:
mkdir my-spring-boot-app && cd my-spring-boot-app
创建 Maven 项目结构:
mkdir -p src/main/java/hello
创建项目的 pom.xml
文件:在 my-spring-boot-app
目录下创建一个 pom.xml
文件,内容如下
<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>my-spring-boot-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>my-spring-boot-app</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<properties>
<java.version>8</java.version> # 注意这里的Java版本!!!
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
创建一个简单的 Spring Boot 应用程序: 在 src/main/java/hello
目录下创建一个名为 HelloApplication.java
的文件,内容如下:
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class, args);
}
}
在 src/main/java/hello
目录下创建一个名为 HelloController.java
的文件,内容如下:
package hello;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}
构建和运行 Spring Boot 应用,返回到项目根目录(my-spring-boot-app
),运行 Maven 打包命令:
mvn package
运行构建好的 JAR 文件:
java -jar target/my-spring-boot-app-0.0.1-SNAPSHOT.jar
在Docker虚拟机Linux3上碰见的BUG
BUG1
环境: java-version:OpenJDK11.0.20.1 Mvn-version:3.6.3 报错:
[ERROR] Error executing Maven.
[ERROR] java.lang.IllegalStateException: Unable to load cache item [ERROR] Caused by: Unable to load cache item [ERROR] Caused by: Could not initialize class com.google.inject.internal.cglib.core.$MethodWrapper[ERROR] Caused by: Exception com.google.inject.internal.cglib.core.$CodeGenerationException: java.lang.reflect.InaccessibleObjectException-->Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @365c30cc [in thread "main"]
解决办法: java - Maven crashes when trying to compile a project "Error executing Maven." - Stack Overflow Looks like that version of maven doesn't work on Java 9, but it does on Java 8.
Ensure that you have the relevant packages installed, e.g...
sudo apt install openjdk-8-jre openjdk-8-jdk
And then make sure that maven uses the correct version. The simplest way to do this is to add the following line to the end of ~/.mavenrc
(Creating it if needed):
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/
(You might need to look around in /usr/lib/jvm/
for the exact name you need as it might be different for your system)
BUG2
在进行了BUG1的修复后 再次运行报错
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project my-spring-boot-app: Fatal error compiling: invalid target release: 11 -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
解决办法: 修改pom.xml
文件中的java版本,找到下列内容修改
<properties>
<java.version>1.8</java.version>
</properties>
Daily Problem
更新: 5/3/2025 字数: 0 字 时长: 0 分钟