SpringBoot-01-HelloWorld

SpringBoot快速上手

项目需求:从浏览器发送/hello请求,响应回Hello World!

开发环境要求

  • SpringBoot 2
  • jdk1.8
  • maven3.3+

检查maven配置

<!-- 配置阿里云镜像 -->
<mirrors>
      <mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>central</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
      </mirror>
  </mirrors>
 
 <!-- maven编译时配置 -->
  <profiles>
         <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>
  </profiles>

创建一个maven工程并引入对应依赖

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

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

创建主程序类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MainApplication {
  public static void main(String[] args) {
    SpringApplication.run(MainApplication.class, args);
  }
}

编写controller类

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

@RestController
public class DemoController {

  @RequestMapping("/hello")
  public String sayHello(){
    return "Hello World!";
  }
}

测试运行

直接运行主程序类的main方法即可,服务默认在本机的8080端口启动。
这样我们就快速搭建起了一个spring应用,是不是灰常简单呢!

posted @ 2021-05-29 22:08  codeloong  阅读(62)  评论(0)    收藏  举报