Java企业级信息系统开发学习笔记14 Spring Boot(使用Spring Initializr方式构建Spring Boot项目)

本文介绍了如何使用SpringInitializr创建一个SpringBoot项目,添加SpringWeb依赖,创建HelloController并设置响应,然后运行项目并访问Web页面。此外,还展示了如何修改访问映射路径。

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

一、使用Spring Initializr方式构建Spring Boot项目

(一)创建Spring Boot项目

  • 创建项目,选择项目类型 - Spring Initializr
    在这里插入图片描述
  • 添加Spring Web依赖
    在这里插入图片描述
    在这里插入图片描述
  • 查看自动生成的pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.11</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>net.huawei.boot</groupId>
    <artifactId>helloworld02</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>HelloWorld02</name>
    <description>Simple Spring Boot Application</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>


(二)创建控制器

在这里插入图片描述

package net.huawei.boot.controller;

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

/**
 * 功能:Hello控制器
 * 作者:
 * 日期:2023年05月10日
 */
@Controller
public class HelloController {
    @ResponseBody
    @GetMapping("/hello") 
    public String hello() {
        return "<h1 style='color: red; text-align: center'>你好,Spring Boot世界~</h1>";
    }
}

(三)运行入口类

在这里插入图片描述

(四)访问Web页面

  • 在浏览器里访问http://localhost:8080/hello
    在这里插入图片描述

(五)修改访问映射路径

  • 修改控制器HelloController
    在这里插入图片描述
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值