spring boot 输出简单的hello world(没用分层结构)

    • spring boot 概述
    • spring boot 输出Hello World
      • 新建一个spring boot 项目参考创建spring boot项目
      • 在pomxml中添加依赖
      • 编写启动类
      • 运行程序

spring boot 概述

我的理解是spring可以少配置,就是约定优于配置。并且直接复制spring boot项目到IDEA,就可以直接运行跑起来的。而不需要配置很多东西。

spring boot 输出Hello World

1. 新建一个spring boot 项目。参考创建spring boot项目

2.在pom.xml中添加依赖

默认创建好spring boot项目时,在pom.xml中已经默认生成了常用的dependency,build。

3. 编写启动类

当我们通过spring boot创建好项目后,默认生成了一个启动类。通常命名为artifactId+Application。

代码块

package com.yubai.springbootjson;

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 SpringBootJsonApplication {

    @RequestMapping("/")
    public String sayHello(){
        return "hello world";
    }

    public static void main(String[] args) {

        SpringApplication.run(SpringBootJsonApplication.class, args);
    }
}

@SpringBootApplication申明让spring boot自动给程序进行必要的配置,等价于以默认属性使用@Configuration,@EnableAutoConfiguration和@ComponentScan

@RestController返回json字符串的数据,直接可以编写RESTFul的接口;

4. 运行程序

第一种方式:右键鼠标Run -> 。之后打开浏览器输入地址:http://127.0.0.1:8080/就可以看到Hello world!了。

spring boot 输出简单的hello world(没用分层结构)_第1张图片

第二种方式: 在console左侧有个运行按钮,点击即可运行。
spring boot 输出简单的hello world(没用分层结构)_第2张图片

第三种方式: 在IDEA的右上角有个按钮,也可以运行。
spring boot 输出简单的hello world(没用分层结构)_第3张图片

第四种方式: 命令式:
mvn spring-boot:run

spring boot 输出简单的hello world(没用分层结构)_第4张图片

注意:一定要在此项目所在的位置下输命令。我的项目是在E:\EclipseCodeSpace\spring-boot-json>

你可能感兴趣的:(spring-boot)