ToB企服应用市场:ToB评测及商务社交产业平台

标题: 二:动手实操SpringBoot-使用Spring Initializr创建项目 [打印本页]

作者: 曹旭辉    时间: 2022-8-9 14:40
标题: 二:动手实操SpringBoot-使用Spring Initializr创建项目
使用 Spring Initializr 初始化 Spring Boot 项目

Spring Initializr 从本质上说就是一个Web应用程序,它能为你构建Spring Boot项目结构。
虽然不能生成应用程序代码,但它能为你提供一个基本的项目结构,以及一个用于构件代码的Maven或者Gradle构建说明文件。
Spring Initializr 的几种用法

使用Web方式

要使用Spring Initializr,最直接的办法就是使用浏览器打开 https://start.spring.io,应该能看到一个类似于下图的一个表单。

配置

其中的内容,是需要我们选择的配置项,根据个人需求选择。
Project: 选择是使用Maven还是Gradle来创建项目。默认Maven,本次选择Maven。
Language: 选择使用的开发语言。默认Java,本次选择Java。
Spring Boot: 选择Spring Boot版本,默认最新版本(非里程碑和快照版本),也可以自由选择其他版本,本次选择最新的2.7.1.
Project Metadata: 指定项目的一些基本信息。最起码得提供项目的Group和Artfact 。点击Options展开,也可以配置一些额外的信息:
项目名称(name):本次为springboot_demo2.
项目描述(Description):本次默认
包名(Package Name):本次为com.tumbler.demo1
打包方式(Packaging):本次为jar方式
Java JDK版本号(Java):本次为1.8
当然这些额外信息都是后期可以修改的,Spring Boot的一大优势就是内嵌了Servlet容器,打成jar包后直接可以运行,所以建议打成jar包,当然开发者可以根据自己的需求打成war包。
Dependencies: 选择需要的依赖,输入关键字就有相应提示,我们选择需要的依赖即可,它会在创建项目时自动在生成的pom.xml(Maven)或者build.gradle(Gradle)引入依赖,当然也可以后期配置。本次选择web依赖即可。


导入IDEA

填好表单,选好依赖后,可以点击下方Generate the project 按钮或者使用快捷键Ctrl+Enter,Spring Initializr就会生成一个项目,浏览器将会以zip文件的形式(文件名取决于Artifact字段的内容)把这个项目下载下来。
解压后通过IDEA或者Eclipse将项目导入,导入后我们可以看到项目的基本目录结构如下

 项目里基本没有代码,除了几个空目录外,还包含了如下几样东西:
里面定义了Spring Boot版本,groupId、artifactId、项目描述、jdk版本以及web起步依赖
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.     <modelVersion>4.0.0</modelVersion>
  5.     <parent>
  6.         <groupId>org.springframework.boot</groupId>
  7.         <artifactId>spring-boot-starter-parent</artifactId>
  8.         <version>2.7.1</version>
  9.         <relativePath/>
  10.     </parent>
  11.     <groupId>cn.he</groupId>
  12.     <artifactId>springboot_demo2</artifactId>
  13.     <version>0.0.1-SNAPSHOT</version>
  14.     <name>springboot_demo2</name>
  15.     <description>Demo project for Spring Boot</description>
  16.     <properties>
  17.         <java.version>1.8</java.version>
  18.     </properties>
  19.     <dependencies>
  20.         <dependency>
  21.             <groupId>org.springframework.boot</groupId>
  22.             <artifactId>spring-boot-starter-web</artifactId>
  23.         </dependency>
  24.         <dependency>
  25.             <groupId>org.springframework.boot</groupId>
  26.             <artifactId>spring-boot-starter-test</artifactId>
  27.             <scope>test</scope>
  28.         </dependency>
  29.     </dependencies>
  30.     <build>
  31.         <plugins>
  32.             <plugin>
  33.                 <groupId>org.springframework.boot</groupId>
  34.                 <artifactId>spring-boot-maven-plugin</artifactId>
  35.             </plugin>
  36.         </plugins>
  37.     </build>
  38. </project>
复制代码
  1. package cn.he.springboot_demo2;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class SpringbootDemo2Application {
  6.     public static void main(String[] args) {
  7.         SpringApplication.run(SpringbootDemo2Application.class, args);
  8.     }
  9. }
复制代码
  1. package cn.he.springboot_demo2;
  2. import org.junit.jupiter.api.Test;
  3. import org.springframework.boot.test.context.SpringBootTest;
  4. @SpringBootTest
  5. class SpringbootDemo2ApplicationTests {
  6.     @Test
  7.     void contextLoads() {
  8.     }
  9. }
复制代码
还有几个空目录:static目录存放的是web应用程序的静态内容(JavaScript、样式表、图片等等),templates目录用于存放呈现模型数据的模板。
这样通过Spring Initializr 的Web方式已经成功创建一个Spring Boot的项目,我们写一个Controller进行测试,controller包一定要在启动类所在包的子包。
  1. package cn.he.springboot_demo2.Controller;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. /**
  5. * @description: Spring Boot 测试 Controller
  6. * @author: 小小赫下士
  7. * @createDate: 2022/6/24
  8. */
  9. @RestController
  10. public class HelloController {
  11.     @GetMapping("hello.action")
  12.     public String hello(){
  13.         return "Hello Spring Boot";
  14.     }
  15. }
复制代码
启动项目

控制台可以看到使用内置容器Tomcat,端口号为8080。

启动成功,在浏览器输入:http://localhost:8080/hello.action
可以看到成功返回了Hello Spring Boot:

IDEA 上使用Spring Initializr

IDEA上是自带 Spring Initializr 集成的,只需要在新建项目是选择 Spring Initializr。

其他配置方式也是一样的

也可以手动选择依赖

新建好的效果和在Spring Initializr 网站生成的是一样的,这样操作更省事。
遇到的一些问题

1、启动时报错:无效的源发行版


需要在File - Project Structure - Module 里修改 Language level 为 8 :

2、启动时报错:无效目标发行版


需要在 File - Settings - Build,Exec..... - Compiler - Java Compiler 中把Target bytecode version 改为8

 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4