[Turn] VsCode build Java development environment (Spring Boot project to create, run, debug)

Source Address: https://github.com/YANGKANG01/Spring-Boot-Demo

Install extensions

You can install the following two major expansion, which has been associated with two extensions extended maven java project development is mainly used, springboot and other needs.

First Steps:

  1. Open expanded view (Ctrl + Shift + X) in the Visual Studio Code.
  2. Enter "java" search store extension.
  3. Locate and install  Java Extension Pack (Java expansion pack) , if you have installed  Language Support for the Java (TM) by Red Hat , can also be found and installed separately  Java Debugger for Visual Studio Code  extension.
  4. Enter "Spring Boot Extension" search store extension.
  5. Locate and install "Spring Boot Extension Pack". The installation process may be slow, be patient.

Configuring Maven:

The lower left corner of the Settings icon -> Settings to open the content filtering settings box, enter maven, and then click the right side to open the json format setting:

Then the path of the executable file configuration maven, maven Setting path configuration, the java.home path configuration, copied to the user setting area and to the right of their actual path computer

Set as follows:

{
    "workbench.iconTheme": "vscode-icons",
    "workbench.startupEditor": "newUntitledFile",
    "java.errors.incompleteClasspath.severity": "ignore",
    "workbench.colorTheme": "Atom One Dark",
    "java.home":"D:\\software\\Java\\jdk1.8.0_60", "java.configuration.maven.userSettings": "D:\\software\\apache-maven-3.3.3-bin\\apache-maven-3.3.3\\conf\\settings.xml", "maven.executable.path": "D:\\software\\apache-maven-3.3.3-bin\\apache-maven-3.3.3\\bin\\mvn.cmd", "maven.terminal.useJavaHome": true, "maven.terminal.customEnv": [ { "environmentVariable": "JAVA_HOME", "value": "D:\\software\\Java\\jdk1.8.0_60" } ], }

If your mvn update package is very slow, it is recommended to use a mirror Ali cloud will quickly speed (modified setting maven configuration is as follows):

 <!-- 阿里云仓库 -->
        <mirror>
            <id>alimaven</id>
            <mirrorOf>central</mirrorOf>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
        </mirror>
        <mirror>
            <id>nexus-aliyun</id>
            <mirrorOf>*</mirrorOf>
            <name>Nexus aliyun</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
        </mirror>
        <!-- 中央仓库1 -->
        <mirror>
            <id>repo1</id>
            <mirrorOf>central</mirrorOf>
            <name>Human Readable Name for this Mirror.</name>
            <url>http://repo1.maven.org/maven2/</url>
        </mirror>
    
        <!-- 中央仓库2 -->
        <mirror>
            <id>repo2</id>
            <mirrorOf>central</mirrorOf>
            <name>Human Readable Name for this Mirror.</name>
            <url>http://repo2.maven.org/maven2/</url>
        </mirror>

Configuration is complete restart VSCode.

Create a Spring Boot project

Use the shortcut key (Ctrl + Shift + P) command window, enter Spring select Maven project. Results are as follows:

Select the language you want to use, Group Id, project name, etc., here select Java:

Select Spring Boot version:

Select package need to be introduced, the introduction of several packages to meet the following web development:

DevTools (hot update code changes, without having to restart), Web (integrated tomcat, SpringMVC), Lombok (Smart Generator setter, getter, toString and other interfaces without manual generation, code more concise), Thymeleaf (template engine).

After selecting the wrap to be imported directly enter, select the project path in a new pop-up window, bringing Spring Boot project is created.

Once you've created the lower right corner vscode will have the following prompts, click Open it to open Spring Boot project just created.

 Project run with debugging

DemoApplication.java file is automatically created after the project is created, the file directory under DemoApplication New Folder Controller, the new file HomeController.java. Results are as follows:

Ps: Bean SpringBoot default rule assembly according to package items where the scanning position DemoApplication class from top to bottom. It must be placed in the same directory as otherwise they will be unable to access the reported error as follows:

Before starting the project also needs to be configured at runtime, as shown below, point to the left of the small insect icon, and then point above the drop-down arrow, select Add configuration, when the first set VS Code will be prompted to select the language environment required to run, select the corresponding environment after the file is automatically created launch.json.

launch.json debug configuration file as follows, does not modify the default configuration can be used:

Selecting a corresponding configuration environment modal the following items, the default port 8080.

After the start-up information can be viewed on the console output after the start panel, shown below, visit: http: // localhost: 8080 to.

 The final results are as follows:

Access the HTML page

Access html in spring boot the need to introduce Thymeleaf (template engine) package, when you create a project that has references cited in the package need not be repeated here. In the resources - to create Index.html file> templates directory under the effect is as follows:

html content:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8"/>
    <title>第一个HTML页面</title>
</head>
<body>
<h1>Hello Spring Boot!!!</h1>
<p th:text="${hello}"></p>
</body>
</html>

 New TestController.java file in the controller directory, as follows:

@Controller
 public  class TestController { 

    / ** 
     * Local access content address: HTTP: // localhost : 8080 / the Hello 
     * @param the Map 
     * @return 
     * / 
    @ RequestMapping ( "/ the Hello" )
     public String helloHtml (HashMap <String, Object > Map) { 
        map.put ( "Hello", "Welcome to the HTML page" );
         return "/ index" ; 
    } 
}

Ps: If you want to access the html page annotated Controller must not be RestController. Otherwise inaccessible.

RestController and Controller difference:

@RestController is a stereotype annotation that combines @ResponseBody and @Controller.
Meaning:
@RestController annotation corresponds @ResponseBody + @Controller together effect.
1) If you just use @RestController annotation Controller, Controller of the method can not return jsp page, view resolver configuration InternalResourceViewResolver does not work, what is returned is the Return of the contents.

For example: should have to success.html page, it displays success.

2) If you need to return to a specific page, you need to use @Controller with the view resolver InternalResourceViewResolver job.

3) To return json or xml mediaType or custom content to a page, you need to add on a corresponding annotated @ResponseBody method

Results are shown below:

Around the end of the basic configuration, you can have fun playing Spring Boot!

 

 


---------------------
Author: ice.ko
Source: CNBLOGS
Original: https: //www.cnblogs.com/miskis/p/9816135.html
copyright notice : In this paper the author original article, reproduced, please attach Bowen link!
Content analysis By: CSDN, CNBLOG blog articles reprinted a key plug

Guess you like

Origin www.cnblogs.com/admans/p/11564802.html