Use VS Code to develop Spring Boot, Zhenni Ma show!

Click on the blue font above and select "Star Official Account"

High-quality articles, delivered immediately

Follow the official account backstage to reply to pay or mall to obtain actual project information + video

Author: outside teacher

www.toutiao.com/i6903532226647179789/

I have a soft spot for VS Code. So I tried to build a Spring Boot development environment in VS Code on the Windows platform.

Necessary preparation

Spring Boot belongs to the Java framework and is a necessary condition for Java development. It must be installed first:

  • Install JDK

In this step, we can download the latest JDK installation directly on the Oracle official website:

Then double-click to install, after several next steps, complete the installation.

After the installation is complete, remember to configure the environment variables (I don't know why this environment variable of the JDK has not been automatically configured):

When your VS Code meets Spring Boot

After configuring the environment variables, in the console, execute the command to check whether the installation is successful:

java -version

If you execute the above command, you can see the version number of Java successfully. Congratulations, the installation of Java JDK is successful.


  • Install maven

As the most popular project management tool in the Java industry, maven is also a daily necessity in Spring Boot development and needs to be downloaded.

We can choose the pre-compiled zip binary package here:

After opening the maven package, unzip it, find a location you like, and save it. Remember where you saved it, because we need to configure maven-related parameters in VS Code later.

maven


  • Install VS Code

Install the necessary plugins

Now open VS Code and install the following two plugins:

Java Extension Pack
Spring Boot Extension Pack
Java Extension Pack Java Extension Pack installation is complete
Spring Boot Extension Pack installation is complete

Configure maven options

Development is not yet possible, and some configuration needs to be done, don’t worry:

Configure maven's settings.xml path

In order to improve the access speed of maven, we modify its source to aliyun maven:

Modify the mirror source of maven to aliyun

The partial code in maven's settings.xml is as follows:

    <mirror>
        <id>alimaven</id>
        <name>aliyun maven</name>
        <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
        <mirrorOf>central</mirrorOf>
    </mirror>
Configure the execution path of maven

Well, here, our environment configuration is complete. Restart VS Code, and you can start development in the next step!

Create a test demo project

Start VS Code again, press Ctrl + Shift + P shortcut, call up the command, enter spring, and then create a new maven project:

New maven project
Select Spring Boot version
Choose a language
Enter Group Id

Note: After entering Group Id, press Enter

Enter Artifact Id

Hit enter

Choose package type
Choose Java version
Select commonly used dependency packages

Hit enter

Introduction to dependency package functions:

Spring Boot DevTools: 支持代码修改热更新,无需重启
Spring Web: 集成Tomcat SpringMVC
Lombok: 智能生成 setter getter toString等
Thymeleaf: 模板引擎
Choose save location

Wait for the project to be created, and then open:

Open project
Project opened successfully

If you see the source code structure above, congratulations, you have succeeded!

Add test business code

The project is successfully created, next, add the test business code:

Add Rest

code show as below:

package alien.learn.ademo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestControllerRest{

    @RequestMapping("/test")
    public String testRest(){
        return "欢迎使用VS Code 和 Spring Boot";
    }
}

Add another html template:

Code:

<!DOCTYPE html>
<html lang="ch">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>主页</title>
</head>
<body>
    <h1>Spring Boot Page!</h1>
    <p th:text="${title}"></p>
</body>
</html>

Add another Controller:

Code:

package alien.learn.ademo.controller;

import java.util.HashMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;

@Controller
public class TestController{

    @RequestMapping("/hello")
    public String getPage(HashMap<String, String> map){
        map.put("title", "欢迎来到Spring Boot!");
        return "/index";
    }
}

Then F5 debugging service:

Commissioning service

Then try to access the service in the browser:

Visit hello again:

If you got here successfully, congratulations, you have experienced the basic process of Spring Boot development in VS Code! Start your pleasant development journey.




有热门推荐????
知乎高赞:拼多多和国家电网 Offer,选哪个?不要用 SELECT *
深度对比Jackson和Fastjson,最终我还是选择了...
Docker 入门终极指南,这是我见过最好的教程!再见,xShell,自己用Java撸一个Web版的,网友直呼:6662020 国内互联网公司的薪酬排名,加班时长排名 !IDEA这样 配置注释模板,让你高出一个逼格!!
Java后端线上问题排查常用命令收藏SpringBoot+Prometheus+Grafana实现应用监控和报警

点击阅读原文,前往学习SpringCloud实战项目

Guess you like

Origin blog.csdn.net/qq_17231297/article/details/115191852