Day2——SpringBoot的helloworld

一. 环境准备

重要,若环境与以下不同可能会出现误差报错

  • jdk:1.8
  • maven: 3.3(3.3以上皆可,不推荐使用3.6以上)
  • IDEA:2019(2017以上皆可)
  • springboot:1.5.9(如果使用idea创建springboot项目的话,可能没有1.5.x版本选择,全都是2.x版本,有什么版本就用什么版本吧,但是2.x发布的版本有较多的修改,后面的某些操作可能与博客阐述的不符,需要自行百度解决遇到的问题

二. 步骤

  • new一个maven 的普通java工程,目录结构如下:
    在这里插入图片描述

  • 在pom文件中导入springboot的相关依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
</parent>
<dependencies>
   <dependency>
         <grouopId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
</dependencies>
  • 编写一个主程序:启动springboot应用
    HelloWorldMainApplication.java
package com.atguigu;

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

/**
  @SpringBootApplication 来标注这是一个主程序类,说明这是一个springboot应用
 */
@SpringBootApplication
public class HelloWorldMainApplication {
    public static void main(String[] args) {
        //Spring应用启动起来
        SpringApplication.run(HelloWorldMainApplication.class, args);
    }
}

  • 编写相关的controller、Service
    HelloController.java
package com.atguigu.controller;

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

@Controller
public class HelloController {

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

  • 启动程序
    在这里插入图片描述
  • 使用浏览器访问localhost:8080/hello

三. 简化部署

由上面步骤可知在springboot中无需配置tomcat即可进行访问。下面使用maven插件打包,使用java -jar命令来运行程序

  • 在pom文件中添加以下:
 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

点击:
在这里插入图片描述
在这里插入图片描述

  • 复制以下jar到桌面
    在这里插入图片描述

  • 在cmd中进入桌面路径,输入java -jar spring-boot01helloworld-1.0-SNAPSHOT.jar即可。使用浏览器访问localhost:8080/hello

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值