[一]SpringBoot 之 HelloWorld

(1)新建一个Maven Java工程

(2)在pom.xml文件中添加Spring BootMaven依赖

  2.1在pom.xml中引入spring-boot-start-parent

  spring官方的解释叫什么staterpoms,它可以提供dependency management,也就是说依赖管理,引入以后在申明其它dependency的时候就不需要version了。

  

  <parent>

     <groupId>org.springframework.boot</groupId>

     <artifactId>spring-boot-starter-parent</artifactId>

     <version>1.3.4.RELEASE</version>

   </parent>

  2.2因为我们开发的是web工程,所以需要在pom.xml中引入spring-boot-starter-web,spring官方解释说spring-boot-start-web包含了spring webmvc和tomcat等web开发的特性。

  

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

 

  2.3 如果我们要直接Main启动spring,那么以下plugin必须要添加,否则是无法启动的。如果使用maven的spring-boot:run的话是不需要此配置的。(我在测试的时候,如果不配置下面的plugin也是直接在Main中运行的。)

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin </artifactId>
            </plugin>
        </plugins>    

 (3)编写启动类

  我们需要一个启动类,然后在启动类申明让springboot自动给我们配置spring需要的配置,比如:@SpringBootApplication,为了可以尽快让程序跑起来,我们简单写一个通过浏览器访问helloworld字样的例子

package me.shijunjie.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class TestHelloWorld {
    @RequestMapping("/")
      public String hello(){
        return"Hello world!";
      }
    
    public static void main(String[] args) {
        SpringApplication.run(TestHelloWorld.class, args);
    }
}

  (4)运行程序

运行我们的Application了,我们先介绍第一种运行方式。第一种方式特别简单:右键Run As -> Java Application。之后打开浏览器输入地址:http://127.0.0.1:8080/就可以看到Hello world!了。第二种方式右键project– Run as – Maven build –在Goals里输入spring-boot:run ,然后Apply,最后点击Run。

原文地址:https://www.cnblogs.com/s648667069/p/6398016.html