SpringBoot2 入门“Hello World”编写

1、环境要求

  1. jdk版本8及以上
  2. Maven 3.3+
  3. idea 2020.2.1
    不知道自己jdk版本和Maven版本的小伙伴可以在dos命令窗口输入java -versionmvn -v命令来分别查看jdk版本和Maven版本
    在这里插入图片描述

1.1、Maven设置

一个是配置阿里云的镜像,一个是配置当前使用的jdk的版本设置

<mirrors>
      <mirror>
          <id>alimaven</id>
          <mirrorOf>central</mirrorOf>
          <name>aliyun maven</name>
                  <url>https://maven.aliyun.com/nexus/content/repositories/central/</url>
      </mirror>
  </mirrors>
 
  <profiles>
         <profile>
              <id>jdk-15</id>
              <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>15</jdk>
              </activation>
              <properties>
                <maven.compiler.source>15</maven.compiler.source>
                <maven.compiler.target>15</maven.compiler.target>
                <maven.compiler.compilerVersion>15</maven.compiler.compilerVersion>
              </properties>
         </profile>
  </profiles>

2、HelloWorld程序编写

程序需求:浏览发送/hello请求,响应 Hello Spring Boot 2 返回至浏览器上

2.1、创建maven工程

2.2、引入所需依赖

Spring官网地址: 链接.
引入依赖SpringBoot官网地址: 链接.

	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
    </parent>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

2.3、整体的结构图

整体的结构图

2.4、主程序的创建

/**
 * 主程序类
 * @author netXiaobao
 * @version 1.0
 * @date 2021/2/13 18:13
 * 这是一个SpringBoot应用
 */
@SpringBootApplication
public class MainApplication {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(MainApplication.class,args);
    }
}

2.5、业务的编写

/**
 * @author netXiaobao
 * @version 1.0
 * @date 2021/2/13 18:26
 */
@RestController
public class HelloController {
    
    

    @RequestMapping("/hello")
    public String handle01(){
    
    
        return "Hello Spring Boot2!";
    }
}

2.6、测试

直接运行main方法

2.6、简化配置

创建properties文件

server.port=8888

此为更改tomcat服务器的端口号
此为更改tomcat服务器的端口号

猜你喜欢

转载自blog.csdn.net/qq_45844443/article/details/113802936