Spring Boot learning environment configuration and 1.1 - the minimum application HelloWorld

Ready development environment: JDK1.8 and above versions, IDEA development software, download Maven Kit.

First configure maven environment variables, create MAVEN_HOME variable, copy the path maven kit to the inside, then this string:% MAVEN_HOME% \ bin added to the path inside, enter mvn -v in cmd, to test whether the installation is successful .

After modifying the file in the maven setting.xml conf folder, the following code directly replace the original copy configuration information:

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <!-- 设置本地仓库位置 -->
  <localRepository>D:/apache-maven-3.6.1/m2/repository</localRepository>
  <pluginGroups></pluginGroups>
  <proxies></proxies>
  <servers></servers>
<mirrors>
<!--镜像mirrors里面的是maven默认的下载地址-->
	<mirror>
		<id>alimaven</id>
		<mirrorOf>central</mirrorOf>
		<name>aliyun maven</name>
		<url>http://maven.aliyun.com/nexus/content/repositories/central/</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>
  </mirrors>
	<!-- 配置Java环境 -->
	<profile>
		<id>jdk-1.8</id>
		<activation>
			<activeByDefault>true</activeByDefault>
			<jdk>1.8</jdk>
		</activation>
		<properties>
			<maven.compiler.source>1.8</maven.compiler.source>
			<maven.compiler.target>1.8</maven.compiler.target>
			<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
		</properties>
	</profile>
</settings>

After starting IDEA click maven option setting in, read the following before you install maven location, local repository is automatically selected:

 

 Then directly create Maven project, select the default, select the project path and look at the situation, but do not change the xml file.

Write the following code in the project .xml file project Tags:

    <groupId>SpringBootDemo01</groupId>
    <artifactId>SpringBootDemo01</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-parent</artifactId>
        <version>2.1.3.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>
    </dependencies>

According to the above idea will then download the required configuration package, the need to wait to see if there is a lot of external libraries in package

Create a package in the main-> java directory, create a file named HelloWorldApp of Java, write the following code:

package com.HelloWorldMain;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloWorldMainApp {
    public static void main(String[] args) {
        //Spring应用启动
        SpringApplication.run(HelloWorldMainApp.class, args);
    }
}

In this directory create a folder named controller class, create a controller class (Java files), write the following code:

package com.HelloWorldMain.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {

    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "Hello Wolrd";
    }
}

Click HelloWorldApp main program start button, wait for a series of print output information bar appears, enter localhost in the browser: 8080 / hello,

String hello world will appear on the screen:

Of course idea also has a more convenient way to create springboot project, created Spring Initializr clicking project, then the next, might network not fail, you can retry it, then you can modify the name of the Artifact, and then we click on one of the following components on the line, click finish you can create a complete spring boot project.

 Then you can see the basic structure of the idea has helped us to build good

Published 58 original articles · won praise 31 · views 40000 +

Guess you like

Origin blog.csdn.net/qq_37504771/article/details/94592860