我会详细讲解“只需两步实现Eclipse+Maven快速构建第一个Spring Boot项目”的完整攻略,过程中会包含两条示例,供大家参考。
1. 新建Maven工程
- 打开Eclipse,选择File -> New -> Maven Project
- 在弹出的窗口中,选择archetype,并在Search框中输入“spring-boot”,选择最新的版本,点击“Next”
- 输入“Group Id”和“Artifact Id”,点击“Finish”
- 稍等片刻,Maven将会自动下载并构建该项目。
示例1:
以Project name为“myProject”为例,输入Group Id为“com.example”,Artifact Id为“demo”,完成后的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>11</java.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. 构建Spring Boot应用
- 在Maven项目根目录下,新建一个“src/main/java”文件夹
- 在该文件夹下,新建一个Java类,例如“App.java”,并输入以下代码:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
- 选择项目,右击鼠标并选择Run As -> Maven build,然后在Goals框中输入“spring-boot:run”,点击“Run”
示例2:
还以前面的示例为例,以Post方法处理“/greeting”请求,并且在主类上使用@ControllerAdvice设置全局异常处理器,代码如下所示:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
@RequestMapping("/v1")
@ControllerAdvice
public class App {
@PostMapping("/greeting")
public ResponseEntity<String> post(@RequestBody String name) {
return ResponseEntity.ok("Hello, " + name);
}
@ExceptionHandler(value = RuntimeException.class)
public ResponseEntity<String> exceptionHandler(Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
然后在postman上发送一条post请求,Body部分的raw数据为“John”,可以得到如下响应:
Hello, John
这就是“只需两步实现Eclipse+Maven快速构建第一个Spring Boot项目”的完整攻略,希望对大家有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:只需两步实现Eclipse+Maven快速构建第一个Spring Boot项目 - Python技术站