springboot 入门小程序 helloworld简单示例

6 篇文章 0 订阅
2 篇文章 0 订阅

要求:浏览器发送hello请求,服务器接受请求并处理,相应hello world字符串。

1.创建一个maven工程(jar)

打开idea首页,点击create new project,然后选择maven,选好相应的jdk,然后next
在这里插入图片描述
继续next,填入项目名springboot-01-helloworld,另外选择项目存储位置,点击finish
在这里插入图片描述
出现该页面之后记得点击右下角的Enable Auto Import,之后每写入一个依赖idea都会帮我们自动导入。

2.导入springboot的相关依赖

在这里插入图片描述

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
 </parent>

3.编写主程序,启动应用

在这里插入图片描述

package com;

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

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

4.编写相关的Controller和Service

在这里插入图片描述

package com.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";
    }
}

*Application启动类的位置:要将Application类放在最外侧,即包含所有子包 ,spring-boot会自动加载启动类所在包下及其子包下的所有组件。
在这里插入图片描述

5.运行主程序,浏览器访问

浏览器网址输入http://localhost:8080/hello访问得到页面
在这里插入图片描述
访问成功,以上说明,springboot简单示例成功。
以上过程出现过的一个问题,就是把MainAppilication的位置放错了,导致最后请求无法得到相应,最后改正了位置,就成功了。

错误演示:
在这里插入图片描述
运行:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啊啊啊~~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值