SpringBoot——经典的Hello World【二】

前言

来创建个hello world 呗

步骤

首先肯定是要打开我们的IDEA来创建一个Maven的项目哈

创建项目

1. File->New->Project

**2.Maven->JDK版本选择->Next **

3. Groupld->Artifactld->Next

4. location->Finsh

**5. 开启自动导入 **

导入依赖

导入SpringBoot的相关依赖,进入SpringBoot的官网点击Quick start下的Spring Lnitializr
填写好具体的信息后,我们可以点击Explore- Ctrl + Space生成一个pom.xml文件,直接拷贝覆盖项目的配置。
生成pom配置
生成pom配置
覆盖pom配置

编写主程序

编写一个主程序用来启动SpringBoot的应用,请自行注意创建的位置。
**1. 创建类文件 **
创建

**2. 标注主程序 **
标明是个SpringBoot应用

**3. 主程序代码如下 **

package com.wangyang;


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

/**
 * @SpringBootApplication 标注一个主程序类,说明这是一个Spring Boot应用
 */
//DataSourceAutoConfiguration禁止自动加载,不然会产生报错
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
public class HelloWorldMainApplication {

    public static void main(String[] args) {
        //启动Spring应用
        SpringApplication.run(HelloWorldMainApplication.class,args);
    }
}

编写业务代码

1. 创建controller控制器

**2. 业务代码 **

package com.wangyang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloWorldController {

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

运行main方法

返回到主程序中,直接运行main方法即可
启动
启动

访问地址

访问

项目打包

运行jar包

访问

posted @ 2019-11-07 02:35  。思索  阅读(217)  评论(0编辑  收藏  举报