Vscode + Java + Spring Boot开发环境配置

本文档详细介绍了在Ubuntu 18.04.4 LTS系统中,如何配置Vscode进行Java和Spring Boot开发。首先,通过官网下载并安装Vscode,接着安装JDK,然后在Vscode中安装必要的插件,包括Spring Boot相关插件。接着,按照Spring Boot官方教程创建项目,最后运行项目并验证其成功启动。文章还提及了该配置方法适用于Windows的WSL环境,以及插件的灵活选择。

0.环境

  • 操作系统:Ubuntu 18.04.4 LTS

1.安装Vscode

在vscode官网下载Ubuntu版本的vscode,会得到一个.deb格式的文件,如code_1.55.2-1618307277_amd64.deb
有UI的同学可以直接点击.deb文件安装,没有UI的同学可以参考这个StackExchange问题,在命令行使用如下命令:

sudo dpkg -i code_1.55.2-1618307277_amd64.deb

验证是否安装成功:

code -v

2.安装openjdk

检查是否已安装openjdk

java -version

如果见到类似如下的输出,可以跳过此步骤:

openjdk version "11.0.11" 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.18.04)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.18.04, mixed mode, sharing)

如果没有安装java,可以使用如下命令安装:

sudo apt-get install openjdk-11-jdk

openjdk8以上的版本应该都是没问题的,不需要一定是11。

3.安装Vscode插件

打开vscode,使用命令Ctrl+Shift+X打开插件视图,并在搜索框中输入java,如下图所示:在这里插入图片描述
点击安装Java Extension Pack插件,这是一个包含了多个java开发会用到的插件套装,如:

  • Language Support for Java(TM) by Red Hat
  • Debugger for Java
  • Java Test Runner
  • Maven for Java

搜索并安装Spring Boot插件套装Spring Boot Extension Pack
在这里插入图片描述

4.创建Spring Boot项目

以下内容参考Sprint Boot官网教程。使用Spring Initializr Java Support插件创建Spring Boot项目,使用命令Ctrl+Shift+P,并输入spring initializr在这里插入图片描述
选择Spring Initializr: Create a Maven Project...
-> 选择Spring Boot版本,如2.4.5
-> 选择项目语言,如Java
-> 输入项目Group Id,如com.example
-> 输入Artifact Id,如demo
-> 选择打包类型,如Jar
-> 选择Java版本,如11
-> 选择依赖,如Spring Boot DevToolsSpring Web
-> 指定保存项目的路径

在完成上述步骤后,会在指定路径产生一个文件夹demo,用vscode打开后会得到下图所示的文件结构:
在这里插入图片描述
DemoApplication.java是整个web应用的入口。

5.运行Spring Boot项目

src/main/java/com/example/demo/controllers文件夹中创建一个简单的controllerHelloController,该controller接受一个GET请求,并返回一个字符串"Greetings from Spring Boot!"在这里插入图片描述
在项目根目录下运行:

./mvnw spring-boot:run

会得到如下输出:

[INFO] Scanning for projects...
[INFO] 
[INFO] --------------------------< com.example:demo >--------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] >>> spring-boot-maven-plugin:2.4.5:run (default-cli) > test-compile @ demo >>>
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /home/heromanba/Desktop/projects/demo/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory /home/heromanba/Desktop/projects/demo/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/heromanba/Desktop/projects/demo/target/test-classes
[INFO] 
[INFO] <<< spring-boot-maven-plugin:2.4.5:run (default-cli) < test-compile @ demo <<<
[INFO] 
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.4.5:run (default-cli) @ demo ---
[INFO] Attaching agents: []

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.5)

2021-05-01 21:16:40.809  INFO 23720 --- [  restartedMain] com.example.demo.DemoApplication         : Starting DemoApplication using Java 11.0.11 on heromanba-MS-7C31 with PID 23720 (/home/heromanba/Desktop/projects/demo/target/classes started by heromanba in /home/heromanba/Desktop/projects/demo)
2021-05-01 21:16:40.811  INFO 23720 --- [  restartedMain] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2021-05-01 21:16:40.835  INFO 23720 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2021-05-01 21:16:40.836  INFO 23720 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2021-05-01 21:16:41.236  INFO 23720 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-05-01 21:16:41.241  INFO 23720 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-05-01 21:16:41.241  INFO 23720 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.45]
2021-05-01 21:16:41.267  INFO 23720 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-05-01 21:16:41.267  INFO 23720 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 431 ms
2021-05-01 21:16:41.368  INFO 23720 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-05-01 21:16:41.432  INFO 23720 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2021-05-01 21:16:41.448  INFO 23720 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-05-01 21:16:41.455  INFO 23720 --- [  restartedMain] com.example.demo.DemoApplication         : Started DemoApplication in 0.84 seconds (JVM running for 1.071)

使用curl验证web应用是否启动成功:

curl localhost:8080

会得到如下输出:

Greetings from Spring Boot!

Vscode + Java + Spring Boot开发环境配置成功,接下来会介绍如何使用已安装的插件套装来debug、写单元测试、重构代码、查看类继承结构等。

6.总结

  • 虽然以上教程是基于Ubuntu操作系统,但是应该可以套用在Windows的WSL (WIndows Subsystem For Linux)环境中;
  • 如果不想安装上述的插件套装,可以选择需要的插件单独安装;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值