首页 > 编程语言 > 详细

SpringBoot-01-HelloWorld

时间:2021-05-30 00:50:11      阅读:9      评论:0      收藏:0      [点我收藏+]

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应用,是不是灰常简单呢!

SpringBoot-01-HelloWorld

原文:https://www.cnblogs.com/codeloong/p/14826551.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!