第一个Spring Boot程序

上篇我们已经创建了一个Spring Boot项目,然后我们可以暂时用不到的文件删除,剩如图所示

在创建项目名的同级目录下创建一个controller类编写一个helloworld程序

package com.example;

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

@Controller
@RequestMapping("/hello")
public class controller {
    @GetMapping("/hello")
    @ResponseBody
    public String hello(){
        return "hello023";
    }
}

在main方法中点击【run】

 等待一会如果有以下信息,说明运行成功

运行成功后,浏览器访问 “http://localhost:8080/hello” 地址。

这样第一个SpringBoot的helloworld程序就运行成功了!!


 

注:在创建项目时如果没有勾选spring-web依赖模块,则我们可以在pom.xml中添加

<properties>
    <java.version>1.8</java.version>
</properties>
 
<dependencies>
    <!--starter-web依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

 

修改 properties 配置文件实例:

打开 resources 下的 application.properties

#配置端口和访问时的项目名
server.port=8081
server.servlet.context-path=/demo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值