创建一个入门级的Spring Boot项目HelloWorld

本文详细介绍了使用Maven创建SpringBoot项目的步骤,包括项目创建、依赖包导入、Controller类编写、SpringBootApplication配置及运行,是SpringBoot入门者的实用指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

除过使用IDEA自带的一键式生成Sping Boot,第二个就是直接使用maven工具来创建并管理项目,对于一个Spring Boot项目,需要以下几个步骤:

1.创建maven项目

File -> new -> Project -> Maven -> Next -> 给自己的项目命名 一般的命名格式为GroupId = xx.xx,Artifactid =xx -> Next 创建完成;

2.在pom文件中导入两个Spring Boot的依赖包

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

3.编写Controller类及ResponseBody响应体

package com.google.sboot.model;

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

@Controller
public class User {
    @ResponseBody
    @RequestMapping("/hello")
    public String helloWorld(){
        return "Hello World";
    }
}

4.编写SpringBootApplication

package com.google.sboot;


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


@SpringBootApplication

public class SbootApplication {

    public static void main(String[] args) {

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

}

5.运行main方法,在浏览器中输入 localhost:8080/hello 用来请求SpringApplication

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值